Apmessage.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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=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 addRead(){
  167. $post=$this->request->post();
  168. $where['id']=$post['message_id'];
  169. $list =Db::name('message')->where($where)->find();
  170. if(empty($list)){
  171. $this->error('not exit!');
  172. }
  173. $data['page_view']=$list['page_view']+1;
  174. $data['uptime']=time();
  175. Db::name('message')->where($where)->update($data);
  176. $this->success();
  177. }
  178. public function upRead(){
  179. $post=$this->request->post();
  180. if($post['type']==1){//单条阅读
  181. unset($post['type']);
  182. $where['id']=$post['message_id'];
  183. $list =Db::name('message')->where($where)->find();
  184. if(empty($list)){
  185. $this->error('not exit!');
  186. }
  187. $wheres['message_id']=$post['message_id'];
  188. $wheres['user_id']=$post['user_id'];
  189. $lists = Db::name('message_read')->where($wheres)->find();
  190. if($lists){
  191. $this->error();
  192. }else{
  193. $isPushedres=true;
  194. if($list['coupon']){
  195. $isPushedres=false;
  196. $jobname = 'app\job\Sendcoupon';
  197. $jobQueueName = "createSendCouponJob";
  198. $jobData['user_id']=$post['user_id'];
  199. $jobData['coupon']=$list['coupon'];
  200. $jobData['url']='https://www.alipearlhair.com';
  201. //$jobData['url']='https://www.boifit.com';
  202. $jobData['message_id']=$post['message_id'];
  203. $isPushed = Queue::push($jobname , $jobData , $jobQueueName );
  204. if ($isPushed !== false) {
  205. $isPushedres=true;
  206. }
  207. }
  208. if ($isPushedres) {
  209. $post['addtime']=time();
  210. $re = Db::name('message_read')->insertGetId($post);
  211. if($re){
  212. $msg='';
  213. if($list['coupon']){
  214. $msg = 'Coupons will be in your account within 5mins!';
  215. }
  216. $this->success($msg);
  217. }else{
  218. $this->error();
  219. }
  220. } else {
  221. $this->error();
  222. }
  223. }
  224. }else{
  225. $user = User::getByUserId($post['user_id']);
  226. $wheres['m.status']=2;
  227. $user_id=$post['user_id'];
  228. $list =Db::name('message')->alias('m')
  229. ->field('m.id,mr.message_id')
  230. ->join('message_read mr',"m.id =mr.message_id and mr.user_id='{$user_id}'",'left')
  231. ->where($wheres)
  232. ->where("group_id=3 or group_id={$user->group_id}")
  233. ->select();
  234. $res=array();
  235. if($list){
  236. foreach ($list as $k => $v){
  237. if(empty($v['message_id'])){
  238. $arr['message_id'] = $v['id'];
  239. $arr['user_id'] = $post['user_id'];
  240. $arr['addtime']=time();
  241. $res[] = $arr;
  242. }
  243. }
  244. $num = 100;//每次导入条数
  245. $limit = ceil(count($res)/$num);
  246. for($i=1;$i<=$limit;$i++){
  247. $offset=($i-1)*$num;
  248. $data=array_slice($res,$offset,$num);
  249. Db::name('message_read')->insertAll($data);
  250. }
  251. }
  252. $this->success();
  253. }
  254. }
  255. public function getUnRead(){
  256. $post=$this->request->post();
  257. $user = User::getByUserId($post['user_id']);
  258. $count1=0;
  259. $count2=0;
  260. if($user){
  261. $where['status']=2;
  262. $count1 =Db::name('message')
  263. ->where($where)
  264. ->where("group_id=3 or group_id={$user->group_id}")
  265. ->count();
  266. if($count1){
  267. unset($where);
  268. $where['user_id']=$post['user_id'];
  269. $count2 = Db('message_read')->where($where)->count();
  270. }
  271. }
  272. $count=$count1-$count2;
  273. if($count<0){
  274. $count=0;
  275. }
  276. $data['unread']=$count;
  277. $this->success('',$data);
  278. }
  279. }