Message.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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($enContent,$segment){
  59. $content = new StringMap();
  60. $content->setEn($enContent);
  61. $notification = new Notification();
  62. $notification->setAppId('6bbd561f-4d8e-4d04-a6c4-dbef1bf99694');
  63. $notification->setContents($content);
  64. $notification->setIncludedSegments([$segment]);
  65. return $notification;
  66. }
  67. public function send($enContent,$segment){
  68. $notification = $this->createNotification($enContent,$segment);
  69. $result = $this->apiInstance->createNotification($notification);
  70. return $result;
  71. }
  72. public function sendMsg(){
  73. $wheres['sendtime']=array('lt',time());
  74. $wheres['status']=1;
  75. $one_push = Db::name('message')->where($wheres)->order('id')->find();
  76. if (empty($one_push)) {
  77. echo '没有查到推送队列';
  78. exit;
  79. } else {
  80. $where['id']=$one_push['id'];
  81. $data['status']=2;
  82. $data['uptime']=time();
  83. Db::name('message')->where($where)->update($data);
  84. }
  85. $template_dsc =$one_push['template_dsc'];
  86. $segment =$one_push['group_name'];
  87. $result =$this->send($template_dsc,$segment);
  88. print_r($result);
  89. }
  90. public function getMessage(){
  91. $post=$this->request->post();
  92. $user = User::getByUserId($post['user_id']);
  93. $data=array();
  94. if($user){
  95. $wheres['group_id']=$user->group_id;
  96. $wheres['status']=2;
  97. $list = Db::name('message')->where($wheres)->order('id')->select();
  98. if($list){
  99. foreach ($list as $k=>$v){
  100. $data[$k]['message_id']=$v['id'];
  101. $data[$k]['imgUrl']='';
  102. $data[$k]['time']=$v['sendtime'];
  103. $data[$k]['title']=$v['template_name'];
  104. $data[$k]['des']=$v['template_dsc'];
  105. $data[$k]['push_type']=$v['type'];
  106. $data[$k]['push_para']=$v['para'];
  107. $data[$k]['isReaded']=0;
  108. $where['message_id']=$v['id'];
  109. $where['user_id']=$post['user_id'];
  110. $read = Db::name('message_read')->where($where)->find();
  111. if($read){
  112. $data[$k]['isReaded']=1;
  113. }
  114. }
  115. }
  116. }else{
  117. $data=null;
  118. }
  119. $this->success('',$data);
  120. }
  121. public function upRead(){
  122. $post=$this->request->post();
  123. if($post['type']==1){//单条阅读
  124. unset($post['type']);
  125. $wheres['message_id']=$post['message_id'];
  126. $wheres['user_id']=$post['user_id'];
  127. $list = Db::name('message_read')->where($wheres)->find();
  128. if($list){
  129. $this->success();
  130. }else{
  131. $post['addtime']=time();
  132. $re = Db::name('message_read')->insertGetId($post);
  133. if($re){
  134. $this->success();
  135. }else{
  136. $this->error();
  137. }
  138. }
  139. }else{
  140. $user = User::getByUserId($post['user_id']);
  141. $wheres['m.group_id']=$user->group_id;
  142. $wheres['m.status']=2;
  143. $user_id=$post['user_id'];
  144. $list =Db::name('message')->alias('m')
  145. ->field('m.id,mr.message_id')
  146. ->join('message_read mr',"m.id =mr.message_id and mr.user_id='{$user_id}'",'left')
  147. ->where($wheres)
  148. ->select();
  149. $res=array();
  150. if($list){
  151. foreach ($list as $k => $v){
  152. if(empty($v['message_id'])){
  153. $arr['message_id'] = $v['id'];
  154. $arr['user_id'] = $post['user_id'];
  155. $arr['addtime']=time();
  156. $res[] = $arr;
  157. }
  158. };
  159. $num = 100;//每次导入条数
  160. $limit = ceil(count($res)/$num);
  161. for($i=1;$i<=$limit;$i++){
  162. $offset=($i-1)*$num;
  163. $data=array_slice($res,$offset,$num);
  164. Db::name('message_read')->insertAll($data);
  165. };
  166. }
  167. $this->success();
  168. }
  169. }
  170. }