123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- <?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 Apmessage extends Api
- {
- protected $noNeedLogin = ['*'];
- protected $noNeedRight = ['*'];
- public function _initialize()
- {
- parent::_initialize();
- $config= Configuration::getDefaultConfiguration()
- ->setAppKeyToken('NzRhMTYyODUtYzczYi00Yjg5LWI3NzktODFmMmY0MGUyODIx')
- ->setUserKeyToken('NWU3OGMxNzgtZjEzNS00M2JmLThmNzItZjY5YzIxOWU5YTQ3');
- $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('b3124d44-7bc7-4965-95dc-0ecb502fdaea');
- $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 = $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->success();
- }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';
- $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='success';
- if($list['coupon']){
- $msg = 'New coupons are in your account! Shop now!';
- }
- $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);
- }
- }
|