Message.php 8.6 KB

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