Asmessage.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 Asmessage extends Api
  27. {
  28. protected $noNeedLogin = ['*'];
  29. protected $noNeedRight = ['*'];
  30. public function _initialize()
  31. {
  32. parent::_initialize();
  33. $config= Configuration::getDefaultConfiguration()
  34. ->setAppKeyToken('NGU4ZTU0OGUtMGU2Yy00NjNmLTgwNWItZTVmYjQxODg5Yzhh')
  35. ->setUserKeyToken('NjQwYWRlYWEtODlmZS00ZDFhLThmZmMtN2E0ZTQwZWI2NmY5');
  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('618fe580-bc97-4bf4-b2bb-5039f9dbbc82');
  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=array();
  95. for($i=0;$i<=11;$i++){
  96. $dt = new DateTime();
  97. $minute = 1+$i ;
  98. $dt->modify("+$minute minute");
  99. $notification->setSendAfter($dt);
  100. $val=$i;
  101. if($i==10){
  102. $val='-';
  103. }
  104. if($i==11){
  105. $filters = [ // 可选,基于特定条件过滤用户
  106. ['field' => 'tag', 'key' => 'SufixUid', 'relation' => 'not_exists']
  107. ];
  108. }else{
  109. $filters = [ // 可选,基于特定条件过滤用户
  110. ['field' => 'tag', 'key' => 'SufixUid', 'relation' => '=', 'value' => $val]
  111. ];
  112. }
  113. $notification->setFilters($filters);
  114. $result[] = $this->apiInstance->createNotification($notification);
  115. }
  116. return $result;
  117. }
  118. public function sendMsg(){
  119. $wheres['sendtime']=array('lt',time());
  120. $wheres['status']=1;
  121. $one_push = Db::name('message')->where($wheres)->order('id')->find();
  122. if (empty($one_push)) {
  123. echo '没有查到推送队列';
  124. exit;
  125. } else {
  126. $where['id']=$one_push['id'];
  127. $data['status']=2;
  128. $data['uptime']=time();
  129. Db::name('message')->where($where)->update($data);
  130. }
  131. $result =$this->send($one_push);
  132. print_r($result);
  133. }
  134. public function getMessage(){
  135. $post=$this->request->post();
  136. $user = User::getByUserId($post['user_id']);
  137. $config['page']=empty($post['page'])?1:$post['page'];
  138. $size=empty($post['size'])?10:$post['size'];
  139. $data=array();
  140. if($user){
  141. $wheres['status']=2;
  142. $list = Db::name('message')->where($wheres)->where("group_id=3 or group_id={$user->group_id}")->order('sendtime desc')->paginate($size,'',$config);
  143. if($list){
  144. foreach ($list as $k=>$v){
  145. $data[$k]['message_id']=$v['id'];
  146. $data[$k]['imgUrl']='';
  147. $data[$k]['time']=date('Y-m-d H:i A',$v['sendtime']);
  148. $data[$k]['title']=$v['template_name'];
  149. $data[$k]['des']=$v['template_dsc'];
  150. $data[$k]['push_type']=$v['type'];
  151. $data[$k]['push_para']=$v['para'];
  152. $data[$k]['isReaded']=0;
  153. $where['message_id']=$v['id'];
  154. $where['user_id']=$post['user_id'];
  155. $read = Db::name('message_read')->where($where)->find();
  156. if($read){
  157. $data[$k]['isReaded']=1;
  158. }
  159. }
  160. }
  161. }else{
  162. $data=null;
  163. }
  164. $this->success('',$data);
  165. }
  166. public function upRead(){
  167. $post=$this->request->post();
  168. if($post['type']==1){//单条阅读
  169. unset($post['type']);
  170. $where['id']=$post['message_id'];
  171. $list =Db::name('message')->where($where)->find();
  172. if(empty($list)){
  173. $this->error('not exit!');
  174. }
  175. $wheres['message_id']=$post['message_id'];
  176. $wheres['user_id']=$post['user_id'];
  177. $lists = Db::name('message_read')->where($wheres)->find();
  178. if($lists){
  179. $this->error();
  180. }else{
  181. $isPushedres=true;
  182. if($list['coupon']){
  183. $isPushedres=false;
  184. $jobname = 'app\job\Sendcoupon';
  185. $jobQueueName = "createSendCouponJob";
  186. $jobData['user_id']=$post['user_id'];
  187. $jobData['coupon']=$list['coupon'];
  188. $jobData['url']='https://www.alipearlhair.com';
  189. //$jobData['url']='https://www.boifit.com';
  190. $jobData['message_id']=$post['message_id'];
  191. $isPushed = Queue::push($jobname , $jobData , $jobQueueName );
  192. if ($isPushed !== false) {
  193. $isPushedres=true;
  194. }
  195. }
  196. if ($isPushedres) {
  197. $post['addtime']=time();
  198. $re = Db::name('message_read')->insertGetId($post);
  199. if($re){
  200. $msg='';
  201. if($list['coupon']){
  202. $msg = 'Coupons will be in your account within 5mins!';
  203. }
  204. $this->success($msg);
  205. }else{
  206. $this->error();
  207. }
  208. } else {
  209. $this->error();
  210. }
  211. }
  212. }else{
  213. $user = User::getByUserId($post['user_id']);
  214. $wheres['m.status']=2;
  215. $user_id=$post['user_id'];
  216. $list =Db::name('message')->alias('m')
  217. ->field('m.id,mr.message_id')
  218. ->join('message_read mr',"m.id =mr.message_id and mr.user_id='{$user_id}'",'left')
  219. ->where($wheres)
  220. ->where("group_id=3 or group_id={$user->group_id}")
  221. ->select();
  222. $res=array();
  223. if($list){
  224. foreach ($list as $k => $v){
  225. if(empty($v['message_id'])){
  226. $arr['message_id'] = $v['id'];
  227. $arr['user_id'] = $post['user_id'];
  228. $arr['addtime']=time();
  229. $res[] = $arr;
  230. }
  231. }
  232. $num = 100;//每次导入条数
  233. $limit = ceil(count($res)/$num);
  234. for($i=1;$i<=$limit;$i++){
  235. $offset=($i-1)*$num;
  236. $data=array_slice($res,$offset,$num);
  237. Db::name('message_read')->insertAll($data);
  238. }
  239. }
  240. $this->success();
  241. }
  242. }
  243. public function getUnRead(){
  244. $post=$this->request->post();
  245. $user = User::getByUserId($post['user_id']);
  246. $count1=0;
  247. $count2=0;
  248. if($user){
  249. $where['status']=2;
  250. $count1 =Db::name('message')
  251. ->where($where)
  252. ->where("group_id=3 or group_id={$user->group_id}")
  253. ->count();
  254. if($count1){
  255. unset($where);
  256. $where['user_id']=$post['user_id'];
  257. $count2 = Db('message_read')->where($where)->count();
  258. }
  259. }
  260. $count=$count1-$count2;
  261. $data['unread']=$count;
  262. $this->success('',$data);
  263. }
  264. }