bianjunhui 1 yıl önce
ebeveyn
işleme
a50a2f3ecb
1 değiştirilmiş dosya ile 279 ekleme ve 0 silme
  1. 279 0
      application/api/controller/Asmessage.php

+ 279 - 0
application/api/controller/Asmessage.php

@@ -0,0 +1,279 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: Administrator
+ * Date: 2022/12/3 0003
+ * Time: 9:49
+ */
+namespace app\api\controller;
+
+use app\common\controller\Api;
+use DateTime;
+use onesignal\client\api\DefaultApi;
+use onesignal\client\Configuration;
+use onesignal\client\model\GetNotificationRequestBody;
+use onesignal\client\model\Notification;
+use onesignal\client\model\StringMap;
+use onesignal\client\model\Player;
+use onesignal\client\model\UpdatePlayerTagsRequestBody;
+use onesignal\client\model\ExportPlayersRequestBody;
+use onesignal\client\model\Segment;
+use onesignal\client\model\FilterExpressions;
+use GuzzleHttp;
+use app\common\model\User;
+use think\Db;
+use think\Queue;
+use think\Request;
+class Asmessage extends Api
+{
+    protected $noNeedLogin = ['*'];
+    protected $noNeedRight = ['*'];
+    public function _initialize()
+    {
+        parent::_initialize();
+        $config= Configuration::getDefaultConfiguration()
+            ->setAppKeyToken('NGU4ZTU0OGUtMGU2Yy00NjNmLTgwNWItZTVmYjQxODg5Yzhh')
+            ->setUserKeyToken('NjQwYWRlYWEtODlmZS00ZDFhLThmZmMtN2E0ZTQwZWI2NmY5');
+        $this->apiInstance = new DefaultApi(
+            new GuzzleHttp\Client(),
+            $config
+        );
+    }
+    public function getPlayers()
+    {
+        $limit = 10;
+        $getPlayersResult = $this->apiInstance->getPlayers('6bbd561f-4d8e-4d04-a6c4-dbef1bf99694', $limit);
+        print_r($getPlayersResult->getPlayers());
+    }
+
+    public function addPlayers()
+    {
+        $data=$this->request->post();
+        $user = User::getByUserId($data['user_id']);
+        if ($user) {
+            $user->group_id = $data['group_id'];
+            $user->token = $data['token'];
+            $user->save();
+        }else{
+             User::create($data, true);
+        } 
+        $this->success();
+    }
+
+
+    function createNotification($one_push){
+        $template_dsc =$one_push['template_dsc'];
+        $segment =$one_push['group_name'];
+        $title =$one_push['template_name'];
+        $type =$one_push['type'];
+        $url =$one_push['para'];
+        $content = new StringMap();
+        $content->setEn($template_dsc);
+        $notification = new Notification();
+        $notification->setAppId('618fe580-bc97-4bf4-b2bb-5039f9dbbc82');
+        $notification->setContents($content);
+        $notification->setIncludedSegments([$segment]);
+        $notification->setIosBadgeType('Increase');
+        $notification->setIosBadgeCount(1);
+        $notification->setIsIos(true);
+        $notification->setContentAvailable(true);
+        $data['push_para']=$url;
+        $data['push_type']=$type;
+        $data['title']=$title;
+        $data['message_id']=$one_push['id'];
+        $notification->setData($data);
+        $notification->setMutableContent(true);
+        $apns_alert['title']=$title;
+        $notification->setApnsAlert($apns_alert);
+        if($one_push['template_image']){
+            $request = Request::instance();
+            $domain=$request->domain();
+            $images['id'] = $domain.$one_push['template_image'];
+            $notification->setIosAttachments($images);
+        }
+        return $notification;
+    }
+
+    public function send($one_push){
+        $notification = $this->createNotification($one_push);
+        $result=array();
+        for($i=0;$i<=11;$i++){
+            $dt = new DateTime();
+            $minute = 1+$i ;
+            $dt->modify("+$minute minute");
+            $notification->setSendAfter($dt);
+            $val=$i;
+            if($i==10){
+                $val='-';
+            }
+            if($i==11){
+                $filters = [ // 可选,基于特定条件过滤用户
+                    ['field' => 'tag', 'key' => 'SufixUid', 'relation' => 'not_exists']
+                ];
+            }else{
+                $filters = [ // 可选,基于特定条件过滤用户
+                    ['field' => 'tag', 'key' => 'SufixUid', 'relation' => '=', 'value' => $val]
+                ];
+            }
+            $notification->setFilters($filters);
+            $result[] = $this->apiInstance->createNotification($notification);
+        }
+
+        return $result;
+    }
+
+    public function sendMsg(){
+        $wheres['sendtime']=array('lt',time());
+        $wheres['status']=1;
+        $one_push = Db::name('message')->where($wheres)->order('id')->find();
+        if (empty($one_push)) {
+            echo '没有查到推送队列';
+            exit;
+        } else {
+            $where['id']=$one_push['id'];
+            $data['status']=2;
+            $data['uptime']=time();
+            Db::name('message')->where($where)->update($data);
+        }
+        $result =$this->send($one_push);
+        print_r($result);
+    }
+
+    public function getMessage(){
+        $post=$this->request->post();
+        $user = User::getByUserId($post['user_id']);
+        $config['page']=empty($post['page'])?1:$post['page'];
+        $size=empty($post['size'])?10:$post['size'];
+
+        $data=array();
+        if($user){
+            $wheres['status']=2;
+            $list = Db::name('message')->where($wheres)->where("group_id=3 or group_id={$user->group_id}")->order('sendtime desc')->paginate($size,'',$config);
+            if($list){
+                foreach ($list as $k=>$v){
+                    $data[$k]['message_id']=$v['id'];
+                    $data[$k]['imgUrl']='';
+                    $data[$k]['time']=date('Y-m-d  H:i A',$v['sendtime']);
+                    $data[$k]['title']=$v['template_name'];
+                    $data[$k]['des']=$v['template_dsc'];
+                    $data[$k]['push_type']=$v['type'];
+                    $data[$k]['push_para']=$v['para'];
+                    $data[$k]['isReaded']=0;
+                    $where['message_id']=$v['id'];
+                    $where['user_id']=$post['user_id'];
+                    $read = Db::name('message_read')->where($where)->find();
+                    if($read){
+                        $data[$k]['isReaded']=1;
+                    }
+                }
+            }
+        }else{
+            $data=null;
+        }
+        $this->success('',$data);
+    }
+
+    public function upRead(){
+        $post=$this->request->post();
+        if($post['type']==1){//单条阅读
+            unset($post['type']);
+            $where['id']=$post['message_id'];
+            $list =Db::name('message')->where($where)->find();
+            if(empty($list)){
+                $this->error('not exit!');
+            }
+            $wheres['message_id']=$post['message_id'];
+            $wheres['user_id']=$post['user_id'];
+            $lists = Db::name('message_read')->where($wheres)->find();
+            if($lists){
+                $this->error();
+            }else{
+                $isPushedres=true;
+                if($list['coupon']){
+                    $isPushedres=false;
+                    $jobname = 'app\job\Sendcoupon';
+                    $jobQueueName     = "createSendCouponJob";
+                    $jobData['user_id']=$post['user_id'];
+                    $jobData['coupon']=$list['coupon'];
+                    $jobData['url']='https://www.alipearlhair.com';
+                    //$jobData['url']='https://www.boifit.com';
+                    $jobData['message_id']=$post['message_id'];
+                    $isPushed = Queue::push($jobname , $jobData , $jobQueueName );
+                    if ($isPushed !== false) {
+                        $isPushedres=true;
+                    }
+                }
+                if ($isPushedres) {
+                    $post['addtime']=time();
+                    $re = Db::name('message_read')->insertGetId($post);
+                    if($re){
+                        $msg='';
+                        if($list['coupon']){
+                            $msg = 'Coupons will be in your account within 5mins!';
+                        }
+                        $this->success($msg);
+                    }else{
+                        $this->error();
+                    }
+                } else {
+                    $this->error();
+                }
+            }
+        }else{
+            $user = User::getByUserId($post['user_id']);
+            $wheres['m.status']=2;
+            $user_id=$post['user_id'];
+            $list =Db::name('message')->alias('m')
+                ->field('m.id,mr.message_id')
+                ->join('message_read mr',"m.id =mr.message_id and mr.user_id='{$user_id}'",'left')
+                ->where($wheres)
+                ->where("group_id=3 or group_id={$user->group_id}")
+                ->select();
+
+            $res=array();
+            if($list){
+                foreach ($list as $k => $v){
+                    if(empty($v['message_id'])){
+                        $arr['message_id'] = $v['id'];
+                        $arr['user_id'] = $post['user_id'];
+                        $arr['addtime']=time();
+                        $res[] = $arr;
+                    }
+                }
+                $num = 100;//每次导入条数
+                $limit = ceil(count($res)/$num);
+                for($i=1;$i<=$limit;$i++){
+                    $offset=($i-1)*$num;
+                    $data=array_slice($res,$offset,$num);
+                    Db::name('message_read')->insertAll($data);
+                }
+            }
+            $this->success();
+        }
+    }
+
+
+    public function getUnRead(){
+        $post=$this->request->post();
+        $user = User::getByUserId($post['user_id']);
+        $count1=0;
+        $count2=0;
+        if($user){
+            $where['status']=2;
+            $count1 =Db::name('message')
+                ->where($where)
+                ->where("group_id=3 or group_id={$user->group_id}")
+                ->count();
+            if($count1){
+                unset($where);
+                $where['user_id']=$post['user_id'];
+                $count2 = Db('message_read')->where($where)->count();
+            }
+        }
+        $count=$count1-$count2;
+        $data['unread']=$count;
+        $this->success('',$data);
+    }
+
+
+}