Apmessage.php 8.8 KB

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