|
@@ -0,0 +1,229 @@
|
|
|
|
+<?php
|
|
|
|
+/**
|
|
|
|
+ * Created by PhpStorm.
|
|
|
|
+ * User: Administrator
|
|
|
|
+ * Date: 2022/12/3 0003
|
|
|
|
+ * Time: 9:49
|
|
|
|
+ */
|
|
|
|
+namespace app\api\controller;
|
|
|
|
+
|
|
|
|
+use app\common\controller\Api;
|
|
|
|
+use DateTime;
|
|
|
|
+use onesignal\client\api\DefaultApi;
|
|
|
|
+use onesignal\client\Configuration;
|
|
|
|
+use onesignal\client\model\GetNotificationRequestBody;
|
|
|
|
+use onesignal\client\model\Notification;
|
|
|
|
+use onesignal\client\model\StringMap;
|
|
|
|
+use onesignal\client\model\Player;
|
|
|
|
+use onesignal\client\model\UpdatePlayerTagsRequestBody;
|
|
|
|
+use onesignal\client\model\ExportPlayersRequestBody;
|
|
|
|
+use onesignal\client\model\Segment;
|
|
|
|
+use onesignal\client\model\FilterExpressions;
|
|
|
|
+use GuzzleHttp;
|
|
|
|
+use app\common\model\User;
|
|
|
|
+use think\Db;
|
|
|
|
+use think\Request;
|
|
|
|
+class Message extends Api
|
|
|
|
+{
|
|
|
|
+ protected $noNeedLogin = ['*'];
|
|
|
|
+ protected $noNeedRight = ['*'];
|
|
|
|
+ public function _initialize()
|
|
|
|
+ {
|
|
|
|
+ parent::_initialize();
|
|
|
|
+ $config= Configuration::getDefaultConfiguration()
|
|
|
|
+ ->setAppKeyToken('NzRhMTYyODUtYzczYi00Yjg5LWI3NzktODFmMmY0MGUyODIx')
|
|
|
|
+ ->setUserKeyToken('NWU3OGMxNzgtZjEzNS00M2JmLThmNzItZjY5YzIxOWU5YTQ3');
|
|
|
|
+ $this->apiInstance = new DefaultApi(
|
|
|
|
+ new GuzzleHttp\Client(),
|
|
|
|
+ $config
|
|
|
|
+ );
|
|
|
|
+ }
|
|
|
|
+ public function getPlayers()
|
|
|
|
+ {
|
|
|
|
+ $limit = 10;
|
|
|
|
+ $getPlayersResult = $this->apiInstance->getPlayers('6bbd561f-4d8e-4d04-a6c4-dbef1bf99694', $limit);
|
|
|
|
+ print_r($getPlayersResult->getPlayers());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public function addPlayers()
|
|
|
|
+ {
|
|
|
|
+ $data=$this->request->post();
|
|
|
|
+ $user = User::getByUserId($data['user_id']);
|
|
|
|
+ if ($user) {
|
|
|
|
+ $user->group_id = $data['group_id'];
|
|
|
|
+ $user->token = $data['token'];
|
|
|
|
+ $user->save();
|
|
|
|
+ }else{
|
|
|
|
+ User::create($data, true);
|
|
|
|
+ }
|
|
|
|
+ $this->success();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ function createNotification($one_push){
|
|
|
|
+ $template_dsc =$one_push['template_dsc'];
|
|
|
|
+ $segment =$one_push['group_name'];
|
|
|
|
+ $title =$one_push['template_name'];
|
|
|
|
+ $type =$one_push['type'];
|
|
|
|
+ $url =$one_push['para'];
|
|
|
|
+ $content = new StringMap();
|
|
|
|
+ $content->setEn($template_dsc);
|
|
|
|
+ $notification = new Notification();
|
|
|
|
+ $notification->setAppId('6bbd561f-4d8e-4d04-a6c4-dbef1bf99694');
|
|
|
|
+ $notification->setContents($content);
|
|
|
|
+ $notification->setIncludedSegments([$segment]);
|
|
|
|
+ $notification->setIosBadgeType('Increase');
|
|
|
|
+ $notification->setIosBadgeCount(1);
|
|
|
|
+ $notification->setIsIos(true);
|
|
|
|
+ $notification->setContentAvailable(true);
|
|
|
|
+ $data['push_para']=$url;
|
|
|
|
+ $data['push_type']=$type;
|
|
|
|
+ $data['title']=$title;
|
|
|
|
+ $data['message_id']=$one_push['id'];
|
|
|
|
+ $notification->setData($data);
|
|
|
|
+ $notification->setMutableContent(true);
|
|
|
|
+ $apns_alert['title']=$title;
|
|
|
|
+ $notification->setApnsAlert($apns_alert);
|
|
|
|
+ if($one_push['template_image']){
|
|
|
|
+ $request = Request::instance();
|
|
|
|
+ $domain=$request->domain();
|
|
|
|
+ $images['id'] = $domain.$one_push['template_image'];
|
|
|
|
+ $notification->setIosAttachments($images);
|
|
|
|
+ }
|
|
|
|
+ return $notification;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public function send($one_push){
|
|
|
|
+ $notification = $this->createNotification($one_push);
|
|
|
|
+ $result = $this->apiInstance->createNotification($notification);
|
|
|
|
+ return $result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public function sendMsg(){
|
|
|
|
+ $wheres['sendtime']=array('lt',time());
|
|
|
|
+ $wheres['status']=1;
|
|
|
|
+ $one_push = Db::name('message')->where($wheres)->order('id')->find();
|
|
|
|
+ if (empty($one_push)) {
|
|
|
|
+ echo '没有查到推送队列';
|
|
|
|
+ exit;
|
|
|
|
+ } else {
|
|
|
|
+ $where['id']=$one_push['id'];
|
|
|
|
+ $data['status']=2;
|
|
|
|
+ $data['uptime']=time();
|
|
|
|
+ Db::name('message')->where($where)->update($data);
|
|
|
|
+ }
|
|
|
|
+ $result =$this->send($one_push);
|
|
|
|
+ print_r($result);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public function getMessage(){
|
|
|
|
+ $post=$this->request->post();
|
|
|
|
+ $user = User::getByUserId($post['user_id']);
|
|
|
|
+ $config['page']=empty($post['page'])?1:$post['page'];
|
|
|
|
+ $size=empty($post['size'])?10:$post['size'];
|
|
|
|
+
|
|
|
|
+ $data=array();
|
|
|
|
+ if($user){
|
|
|
|
+ $wheres['status']=2;
|
|
|
|
+ $list = Db::name('message')->where($wheres)->where("group_id=3 or group_id={$user->group_id}")->order('sendtime desc')->paginate($size,'',$config);
|
|
|
|
+ if($list){
|
|
|
|
+ foreach ($list as $k=>$v){
|
|
|
|
+ $data[$k]['message_id']=$v['id'];
|
|
|
|
+ $data[$k]['imgUrl']='';
|
|
|
|
+ $data[$k]['time']=date('Y-m-d H:i A',$v['sendtime']);
|
|
|
|
+ $data[$k]['title']=$v['template_name'];
|
|
|
|
+ $data[$k]['des']=$v['template_dsc'];
|
|
|
|
+ $data[$k]['push_type']=$v['type'];
|
|
|
|
+ $data[$k]['push_para']=$v['para'];
|
|
|
|
+ $data[$k]['isReaded']=0;
|
|
|
|
+ $where['message_id']=$v['id'];
|
|
|
|
+ $where['user_id']=$post['user_id'];
|
|
|
|
+ $read = Db::name('message_read')->where($where)->find();
|
|
|
|
+ if($read){
|
|
|
|
+ $data[$k]['isReaded']=1;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }else{
|
|
|
|
+ $data=null;
|
|
|
|
+ }
|
|
|
|
+ $this->success('',$data);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public function upRead(){
|
|
|
|
+ $post=$this->request->post();
|
|
|
|
+ if($post['type']==1){//单条阅读
|
|
|
|
+ unset($post['type']);
|
|
|
|
+ $wheres['message_id']=$post['message_id'];
|
|
|
|
+ $wheres['user_id']=$post['user_id'];
|
|
|
|
+ $list = Db::name('message_read')->where($wheres)->find();
|
|
|
|
+ if($list){
|
|
|
|
+ $this->success();
|
|
|
|
+ }else{
|
|
|
|
+ $post['addtime']=time();
|
|
|
|
+ $re = Db::name('message_read')->insertGetId($post);
|
|
|
|
+ if($re){
|
|
|
|
+ $this->success();
|
|
|
|
+ }else{
|
|
|
|
+ $this->error();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }else{
|
|
|
|
+ $user = User::getByUserId($post['user_id']);
|
|
|
|
+ $wheres['m.status']=2;
|
|
|
|
+ $user_id=$post['user_id'];
|
|
|
|
+ $list =Db::name('message')->alias('m')
|
|
|
|
+ ->field('m.id,mr.message_id')
|
|
|
|
+ ->join('message_read mr',"m.id =mr.message_id and mr.user_id='{$user_id}'",'left')
|
|
|
|
+ ->where($wheres)
|
|
|
|
+ ->where("group_id=3 or group_id={$user->group_id}")
|
|
|
|
+ ->select();
|
|
|
|
+
|
|
|
|
+ $res=array();
|
|
|
|
+ if($list){
|
|
|
|
+ foreach ($list as $k => $v){
|
|
|
|
+ if(empty($v['message_id'])){
|
|
|
|
+ $arr['message_id'] = $v['id'];
|
|
|
|
+ $arr['user_id'] = $post['user_id'];
|
|
|
|
+ $arr['addtime']=time();
|
|
|
|
+ $res[] = $arr;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ $num = 100;//每次导入条数
|
|
|
|
+ $limit = ceil(count($res)/$num);
|
|
|
|
+ for($i=1;$i<=$limit;$i++){
|
|
|
|
+ $offset=($i-1)*$num;
|
|
|
|
+ $data=array_slice($res,$offset,$num);
|
|
|
|
+ Db::name('message_read')->insertAll($data);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ $this->success();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public function getUnRead(){
|
|
|
|
+ $post=$this->request->post();
|
|
|
|
+ $user = User::getByUserId($post['user_id']);
|
|
|
|
+ $wheres['m.status']=2;
|
|
|
|
+ $user_id=$post['user_id'];
|
|
|
|
+ $list =Db::name('message')->alias('m')
|
|
|
|
+ ->field('m.id,mr.message_id')
|
|
|
|
+ ->join('message_read mr',"m.id =mr.message_id and mr.user_id='{$user_id}'",'left')
|
|
|
|
+ ->where($wheres)
|
|
|
|
+ ->where("group_id=3 or group_id={$user->group_id}")
|
|
|
|
+ ->select();
|
|
|
|
+ $count=0;
|
|
|
|
+ if($list){
|
|
|
|
+ foreach ($list as $k => $v){
|
|
|
|
+ if(empty($v['message_id'])){
|
|
|
|
+ $count++;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ $data['unread']=$count;
|
|
|
|
+ $this->success('',$data);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+}
|