setAppKeyToken('MDMwZThjYWMtOWQ4ZC00YzU3LWIwNjktOGYzNTA2NjA3NTBh') ->setUserKeyToken('YTdmZGE5MGEtN2Y3Yi00Mzk0LTgyOGQtMGMzNDkzYTcxYmJh'); $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($enContent,$segment){ $content = new StringMap(); $content->setEn($enContent); $notification = new Notification(); $notification->setAppId('6bbd561f-4d8e-4d04-a6c4-dbef1bf99694'); $notification->setContents($content); $notification->setIncludedSegments([$segment]); return $notification; } public function send($enContent,$segment){ $notification = $this->createNotification($enContent,$segment); $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); } $template_dsc =$one_push['template_dsc']; $segment =$one_push['group_name']; $result =$this->send($template_dsc,$segment); print_r($result); } public function getMessage(){ $post=$this->request->post(); $user = User::getByUserId($post['user_id']); $config['page']=$post['page']; $size=$post['size']; $data=array(); if($user){ $wheres['group_id']=$user->group_id; $wheres['status']=2; $list = Db::name('message')->where($wheres)->order('id')->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'); $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.group_id']=$user->group_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) ->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(); } } }