Message.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2022/12/3 0003
  6. * Time: 9:49
  7. */
  8. namespace app\api\controller;
  9. use app\common\controller\Api;
  10. use DateTime;
  11. use onesignal\client\api\DefaultApi;
  12. use onesignal\client\Configuration;
  13. use onesignal\client\model\GetNotificationRequestBody;
  14. use onesignal\client\model\Notification;
  15. use onesignal\client\model\StringMap;
  16. use onesignal\client\model\Player;
  17. use onesignal\client\model\UpdatePlayerTagsRequestBody;
  18. use onesignal\client\model\ExportPlayersRequestBody;
  19. use onesignal\client\model\Segment;
  20. use onesignal\client\model\FilterExpressions;
  21. use GuzzleHttp;
  22. use app\common\model\User;
  23. use think\Db;
  24. class Message extends Api
  25. {
  26. protected $noNeedLogin = ['*'];
  27. protected $noNeedRight = ['*'];
  28. public function _initialize()
  29. {
  30. parent::_initialize();
  31. $config= Configuration::getDefaultConfiguration()
  32. ->setAppKeyToken('MDMwZThjYWMtOWQ4ZC00YzU3LWIwNjktOGYzNTA2NjA3NTBh')
  33. ->setUserKeyToken('YTdmZGE5MGEtN2Y3Yi00Mzk0LTgyOGQtMGMzNDkzYTcxYmJh');
  34. $this->apiInstance = new DefaultApi(
  35. new GuzzleHttp\Client(),
  36. $config
  37. );
  38. }
  39. public function getPlayers()
  40. {
  41. $limit = 10;
  42. $getPlayersResult = $this->apiInstance->getPlayers('6bbd561f-4d8e-4d04-a6c4-dbef1bf99694', $limit);
  43. print_r($getPlayersResult->getPlayers());
  44. }
  45. public function addPlayers()
  46. {
  47. $data=$this->request->post();
  48. $user = User::getByUserId($data['user_id']);
  49. if ($user) {
  50. $user->group_id = $data['group_id'];
  51. $user->token = $data['token'];
  52. $user->save();
  53. }else{
  54. User::create($data, true);
  55. }
  56. $this->success();
  57. }
  58. function createNotification($one_push){
  59. $template_dsc =$one_push['template_dsc'];
  60. $segment =$one_push['group_name'];
  61. $title =$one_push['template_name'];
  62. $type =$one_push['type'];
  63. $url =$one_push['para'];
  64. $content = new StringMap();
  65. $content->setEn($template_dsc);
  66. $notification = new Notification();
  67. $notification->setAppId('6bbd561f-4d8e-4d04-a6c4-dbef1bf99694');
  68. $notification->setContents($content);
  69. $notification->setIncludedSegments([$segment]);
  70. $data['push_para']=$url;
  71. $data['push_type']=$type;
  72. $notification->setData($data);
  73. $notification->setSubtitle($title);
  74. return $notification;
  75. }
  76. public function send($one_push){
  77. $notification = $this->createNotification($one_push);
  78. $result = $this->apiInstance->createNotification($notification);
  79. return $result;
  80. }
  81. public function sendMsg(){
  82. $wheres['sendtime']=array('lt',time());
  83. $wheres['status']=1;
  84. $one_push = Db::name('message')->where($wheres)->order('id')->find();
  85. if (empty($one_push)) {
  86. echo '没有查到推送队列';
  87. exit;
  88. } else {
  89. $where['id']=$one_push['id'];
  90. $data['status']=2;
  91. $data['uptime']=time();
  92. Db::name('message')->where($where)->update($data);
  93. }
  94. $result =$this->send($one_push);
  95. print_r($result);
  96. }
  97. public function getMessage(){
  98. $post=$this->request->post();
  99. $user = User::getByUserId($post['user_id']);
  100. $config['page']=$post['page'];
  101. $size=$post['size'];
  102. $data=array();
  103. if($user){
  104. $wheres['group_id']=$user->group_id;
  105. $wheres['status']=2;
  106. $list = Db::name('message')->where($wheres)->order('id')->paginate($size,'',$config);
  107. if($list){
  108. foreach ($list as $k=>$v){
  109. $data[$k]['message_id']=$v['id'];
  110. $data[$k]['imgUrl']='';
  111. $data[$k]['time']=date('Y-m-d H:i A');
  112. $data[$k]['title']=$v['template_name'];
  113. $data[$k]['des']=$v['template_dsc'];
  114. $data[$k]['push_type']=$v['type'];
  115. $data[$k]['push_para']=$v['para'];
  116. $data[$k]['isReaded']=0;
  117. $where['message_id']=$v['id'];
  118. $where['user_id']=$post['user_id'];
  119. $read = Db::name('message_read')->where($where)->find();
  120. if($read){
  121. $data[$k]['isReaded']=1;
  122. }
  123. }
  124. }
  125. }else{
  126. $data=null;
  127. }
  128. $this->success('',$data);
  129. }
  130. public function upRead(){
  131. $post=$this->request->post();
  132. if($post['type']==1){//单条阅读
  133. unset($post['type']);
  134. $wheres['message_id']=$post['message_id'];
  135. $wheres['user_id']=$post['user_id'];
  136. $list = Db::name('message_read')->where($wheres)->find();
  137. if($list){
  138. $this->success();
  139. }else{
  140. $post['addtime']=time();
  141. $re = Db::name('message_read')->insertGetId($post);
  142. if($re){
  143. $this->success();
  144. }else{
  145. $this->error();
  146. }
  147. }
  148. }else{
  149. $user = User::getByUserId($post['user_id']);
  150. $wheres['m.group_id']=$user->group_id;
  151. $wheres['m.status']=2;
  152. $user_id=$post['user_id'];
  153. $list =Db::name('message')->alias('m')
  154. ->field('m.id,mr.message_id')
  155. ->join('message_read mr',"m.id =mr.message_id and mr.user_id='{$user_id}'",'left')
  156. ->where($wheres)
  157. ->select();
  158. $res=array();
  159. if($list){
  160. foreach ($list as $k => $v){
  161. if(empty($v['message_id'])){
  162. $arr['message_id'] = $v['id'];
  163. $arr['user_id'] = $post['user_id'];
  164. $arr['addtime']=time();
  165. $res[] = $arr;
  166. }
  167. };
  168. $num = 100;//每次导入条数
  169. $limit = ceil(count($res)/$num);
  170. for($i=1;$i<=$limit;$i++){
  171. $offset=($i-1)*$num;
  172. $data=array_slice($res,$offset,$num);
  173. Db::name('message_read')->insertAll($data);
  174. };
  175. }
  176. $this->success();
  177. }
  178. }
  179. }