Message.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. use think\Request;
  25. class Message extends Api
  26. {
  27. protected $noNeedLogin = ['*'];
  28. protected $noNeedRight = ['*'];
  29. public function _initialize()
  30. {
  31. parent::_initialize();
  32. $config= Configuration::getDefaultConfiguration()
  33. ->setAppKeyToken('MDMwZThjYWMtOWQ4ZC00YzU3LWIwNjktOGYzNTA2NjA3NTBh')
  34. ->setUserKeyToken('YTdmZGE5MGEtN2Y3Yi00Mzk0LTgyOGQtMGMzNDkzYTcxYmJh');
  35. $this->apiInstance = new DefaultApi(
  36. new GuzzleHttp\Client(),
  37. $config
  38. );
  39. }
  40. public function getPlayers()
  41. {
  42. $limit = 10;
  43. $getPlayersResult = $this->apiInstance->getPlayers('6bbd561f-4d8e-4d04-a6c4-dbef1bf99694', $limit);
  44. print_r($getPlayersResult->getPlayers());
  45. }
  46. public function addPlayers()
  47. {
  48. $data=$this->request->post();
  49. $user = User::getByUserId($data['user_id']);
  50. if ($user) {
  51. $user->group_id = $data['group_id'];
  52. $user->token = $data['token'];
  53. $user->save();
  54. }else{
  55. User::create($data, true);
  56. }
  57. $this->success();
  58. }
  59. function createNotification($one_push){
  60. $template_dsc =$one_push['template_dsc'];
  61. $segment =$one_push['group_name'];
  62. $title =$one_push['template_name'];
  63. $type =$one_push['type'];
  64. $url =$one_push['para'];
  65. $content = new StringMap();
  66. $content->setEn($template_dsc);
  67. $notification = new Notification();
  68. $notification->setAppId('6bbd561f-4d8e-4d04-a6c4-dbef1bf99694');
  69. $notification->setContents($content);
  70. $notification->setIncludedSegments([$segment]);
  71. $notification->setIosBadgeType('Increase');
  72. $notification->setIosBadgeCount(1);
  73. $notification->setIsIos(true);
  74. $notification->setContentAvailable(true);
  75. $data['push_para']=$url;
  76. $data['push_type']=$type;
  77. $data['title']=$title;
  78. $data['message_id']=$one_push['id'];
  79. $notification->setData($data);
  80. $notification->setMutableContent(true);
  81. $apns_alert['title']=$title;
  82. $notification->setApnsAlert($apns_alert);
  83. if($one_push['template_image']){
  84. $request = Request::instance();
  85. $domain=$request->domain();
  86. $images['id'] = $domain.$one_push['template_image'];
  87. $notification->setIosAttachments($images);
  88. }
  89. return $notification;
  90. }
  91. public function send($one_push){
  92. $notification = $this->createNotification($one_push);
  93. $result = $this->apiInstance->createNotification($notification);
  94. return $result;
  95. }
  96. public function sendMsg(){
  97. $wheres['sendtime']=array('lt',time());
  98. $wheres['status']=1;
  99. $one_push = Db::name('message')->where($wheres)->order('id')->find();
  100. if (empty($one_push)) {
  101. echo '没有查到推送队列';
  102. exit;
  103. } else {
  104. $where['id']=$one_push['id'];
  105. $data['status']=2;
  106. $data['uptime']=time();
  107. Db::name('message')->where($where)->update($data);
  108. }
  109. $result =$this->send($one_push);
  110. print_r($result);
  111. }
  112. public function getMessage(){
  113. $post=$this->request->post();
  114. $user = User::getByUserId($post['user_id']);
  115. $config['page']=empty($post['page'])?1:$post['page'];
  116. $size=empty($post['size'])?10:$post['size'];
  117. $data=array();
  118. if($user){
  119. $wheres['status']=2;
  120. $list = Db::name('message')->where($wheres)->where("group_id=3 or group_id={$user->group_id}")->order('sendtime desc')->paginate($size,'',$config);
  121. if($list){
  122. foreach ($list as $k=>$v){
  123. $data[$k]['message_id']=$v['id'];
  124. $data[$k]['imgUrl']='';
  125. $data[$k]['time']=date('Y-m-d H:i A',$v['sendtime']);
  126. $data[$k]['title']=$v['template_name'];
  127. $data[$k]['des']=$v['template_dsc'];
  128. $data[$k]['push_type']=$v['type'];
  129. $data[$k]['push_para']=$v['para'];
  130. $data[$k]['isReaded']=0;
  131. $where['message_id']=$v['id'];
  132. $where['user_id']=$post['user_id'];
  133. $read = Db::name('message_read')->where($where)->find();
  134. if($read){
  135. $data[$k]['isReaded']=1;
  136. }
  137. }
  138. }
  139. }else{
  140. $data=null;
  141. }
  142. $this->success('',$data);
  143. }
  144. public function upRead(){
  145. $post=$this->request->post();
  146. if($post['type']==1){//单条阅读
  147. unset($post['type']);
  148. $wheres['message_id']=$post['message_id'];
  149. $wheres['user_id']=$post['user_id'];
  150. $list = Db::name('message_read')->where($wheres)->find();
  151. if($list){
  152. $this->success();
  153. }else{
  154. $post['addtime']=time();
  155. $re = Db::name('message_read')->insertGetId($post);
  156. if($re){
  157. $this->success();
  158. }else{
  159. $this->error();
  160. }
  161. }
  162. }else{
  163. $user = User::getByUserId($post['user_id']);
  164. $wheres['m.status']=2;
  165. $user_id=$post['user_id'];
  166. $list =Db::name('message')->alias('m')
  167. ->field('m.id,mr.message_id')
  168. ->join('message_read mr',"m.id =mr.message_id and mr.user_id='{$user_id}'",'left')
  169. ->where($wheres)
  170. ->where("group_id=3 or group_id={$user->group_id}")
  171. ->select();
  172. $res=array();
  173. if($list){
  174. foreach ($list as $k => $v){
  175. if(empty($v['message_id'])){
  176. $arr['message_id'] = $v['id'];
  177. $arr['user_id'] = $post['user_id'];
  178. $arr['addtime']=time();
  179. $res[] = $arr;
  180. }
  181. }
  182. $num = 100;//每次导入条数
  183. $limit = ceil(count($res)/$num);
  184. for($i=1;$i<=$limit;$i++){
  185. $offset=($i-1)*$num;
  186. $data=array_slice($res,$offset,$num);
  187. Db::name('message_read')->insertAll($data);
  188. }
  189. }
  190. $this->success();
  191. }
  192. }
  193. public function getUnRead(){
  194. $post=$this->request->post();
  195. $user = User::getByUserId($post['user_id']);
  196. $count1=0;
  197. $count2=0;
  198. if($user){
  199. $where['status']=2;
  200. $count1 =Db::name('message')
  201. ->where($where)
  202. ->count();
  203. if($count1){
  204. unset($where);
  205. $where['user_id']=$post['user_id'];
  206. $count2 = Db('message_read')->where($where)->count();
  207. }
  208. }
  209. $count=$count1-$count2;
  210. $data['unread']=$count;
  211. $this->success('',$data);
  212. }
  213. }