Bladeren bron

消息优惠券

bianjunhui 1 jaar geleden
bovenliggende
commit
9b382adfeb

+ 7 - 0
application/admin/view/message/add.html

@@ -24,6 +24,13 @@
             <input id="c-para" class="form-control" name="row[para]" type="text" value="">
         </div>
     </div>
+    <div class="form-group">
+        <label for="c-para" class="control-label col-xs-12 col-sm-2">{:__('优惠券')}:</label>
+        <div class="col-xs-12 col-sm-8">
+            <input id="c-coupon" class="form-control" name="row[coupon]" type="text" value="">
+        </div>
+        <span class="extend" >填写优惠券ID,多个优惠券用英文,隔开</span>
+    </div>
     <div class="form-group">
         <label class="control-label col-xs-12 col-sm-2">{:__('Sendtime')}:</label>
         <div class="col-xs-12 col-sm-8">

+ 7 - 0
application/admin/view/message/edit.html

@@ -24,6 +24,13 @@
             <input id="c-para" class="form-control" name="row[para]" type="text" value="{$row.para}">
         </div>
     </div>
+    <div class="form-group">
+        <label for="c-para" class="control-label col-xs-12 col-sm-2">{:__('优惠券')}:</label>
+        <div class="col-xs-12 col-sm-8">
+            <input id="c-coupon" class="form-control" name="row[coupon]" type="text" value="{$row.coupon}">
+        </div>
+        <span class="extend" >填写优惠券ID,多个优惠券用英文,隔开</span>
+    </div>
     <div class="form-group">
         <label class="control-label col-xs-12 col-sm-2">{:__('Sendtime')}:</label>
         <div class="col-xs-12 col-sm-8">

+ 34 - 7
application/api/controller/Apmessage.php

@@ -22,6 +22,7 @@ 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
 {
@@ -154,17 +155,43 @@ class Apmessage extends Api
         $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'];
-            $list = Db::name('message_read')->where($wheres)->find();
-            if($list){
+            $lists = Db::name('message_read')->where($wheres)->find();
+            if($lists){
                 $this->success();
             }else{
-                $post['addtime']=time();
-                $re = Db::name('message_read')->insertGetId($post);
-                if($re){
-                    $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();
                 }
             }

+ 35 - 10
application/api/controller/Message.php

@@ -22,6 +22,7 @@ use onesignal\client\model\FilterExpressions;
 use GuzzleHttp;
 use app\common\model\User;
 use think\Db;
+use think\Queue;
 use think\Request;
 class Message extends Api
 {
@@ -154,20 +155,44 @@ class Message extends Api
         $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'];
-            $list = Db::name('message_read')->where($wheres)->find();
-            if($list){
+            $lists = Db::name('message_read')->where($wheres)->find();
+            if($lists){
                 $this->success();
             }else{
-
-
-
-                $post['addtime']=time();
-                $re = Db::name('message_read')->insertGetId($post);
-                if($re){
-                    $this->success();
-                }else{
+                $isPushedres=true;
+                if($list['coupon']){
+                    $isPushedres=false;
+                    $jobname = 'app\job\Sendcoupon';
+                    $jobQueueName     = "createSendCouponJob";
+                    $jobData['user_id']=$post['user_id'];
+                    $jobData['message_id']=$post['message_id'];
+                    $jobData['coupon']=$list['coupon'];
+                    $jobData['url']='https://www.sukerle.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();
                 }
             }

+ 99 - 0
application/api/controller/Test.php

@@ -0,0 +1,99 @@
+<?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 Test extends Api
+{
+    protected $noNeedLogin = ['*'];
+    protected $noNeedRight = ['*'];
+
+    public function _initialize()
+    {
+        parent::_initialize();
+
+    }
+    public function test()
+    {
+        $data =input();
+        $id=$data['ids'];
+        $wheres['id']=$id;
+        $one_push = Db::name('message')->where($wheres)->find();
+        if (empty($one_push)) {
+            echo '没有查到推送队列';
+            exit;
+        }
+        $result =$this->sendTest($one_push);
+        $this->success('',$result);
+    }
+
+    function createNotification($one_push){
+        $template_dsc =$one_push['template_dsc'];
+        $title =$one_push['template_name'];
+        $type =$one_push['type'];
+        $url =$one_push['para'];
+        $content = new StringMap();
+        $content->setEn($template_dsc);
+        $notification = new Notification();
+        $notification->setContents($content);
+        $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 sendTest($one_push){
+        $appId =$this->getAppId();
+        $notification = $this->createNotification($one_push);
+        $notification->setAppId($appId);
+        $notification->setIncludedSegments(['TestUsers']);
+        $dt = new DateTime();
+        $dt->modify('+1 hour');
+        $notification->setSendAfter($dt);
+        return $this->apiInstance->createNotification($notification);
+    }
+    public function getAppId(){
+        $url = $_SERVER['HTTP_HOST'];
+        if (strpos($url, 'westkiss') !== false) {
+            $appId ='6bbd561f-4d8e-4d04-a6c4-dbef1bf99694';
+        }else{
+            $appId ='b3124d44-7bc7-4965-95dc-0ecb502fdaea';
+        }
+        return $appId;
+    }
+}

+ 36 - 0
application/common/model/Coupon.php

@@ -0,0 +1,36 @@
+<?php
+
+namespace app\common\model;
+
+use think\Model;
+
+
+class Coupon Extends Model
+{
+
+    public function sendCoupon($data){
+        $url =$data['url'].'/app-api/raffle/getMessageAddCoupon';
+        $re = $this->httpPost($url,$data);
+        return json_decode($re,true);
+    }
+
+
+    public function httpPost($url,$data){
+        $curl = curl_init(); // 启动一个CURL会话
+        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
+        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
+        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
+        curl_setopt($curl, CURLOPT_POST, true); // 发送一个常规的Post请求
+        curl_setopt($curl, CURLOPT_POSTFIELDS,  $data); // Post提交的数据包
+        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
+        curl_setopt($curl, CURLOPT_HEADER, false); // 显示返回的Header区域内容
+        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
+        $result = curl_exec($curl); // 执行操作
+        if (curl_errno($curl)) {
+            return 'Error POST'.curl_error($curl);
+        }
+        curl_close($curl); // 关键CURL会话
+        return $result; // 返回数据
+    }
+
+}

+ 9 - 1
application/extra/queue.php

@@ -10,5 +10,13 @@
 // +----------------------------------------------------------------------
 
 return [
-    'connector' => 'Sync'
+    'connector'  => 'Redis',
+    'expire'     => 3600,
+    'default'    => 'REDIS_QUEUE',
+    'host'       => '127.0.0.1',
+    'port'       => 6379,
+    'password'   => '',
+    'select'     => 15,
+    'timeout'    => 0,
+    'persistent' => false,
 ];

+ 37 - 0
application/job/Sendcoupon.php

@@ -0,0 +1,37 @@
+<?php
+/***
+ * User: jun_hy
+ * Date: 2022/7/14
+ * Time: 10:48
+ */
+
+namespace app\job;
+use think\queue\Job;
+use app\common\model\Coupon;
+use think\Db;
+class Sendcoupon
+{
+    public function fire(Job $job, $data)
+    {
+        $send =new Coupon();
+        $result = $send->sendCoupon($data);
+        if (isset($result['msg'])) {
+            $isJobDone = true;
+        }else{
+            $isJobDone = false;
+        }
+        $data['addtime']=date('Y-m-d H:i:s',time());
+        if ($isJobDone) {
+            //如果任务执行成功, 删除任务
+            $data['code']=$result['code'];
+            Db::name('send_coupon_log')->insertGetId($data);
+        }else{
+            if ($job->attempts() > 3) {
+                $job->delete(); // 删除任务
+            }else{
+                //如果任务执行失败, 重发 100秒后在执行
+                $job->release(100);
+            }
+        }
+    }
+}

+ 12 - 1
public/assets/js/backend/message.js

@@ -37,7 +37,18 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
                         {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                         {field: 'sendtime', title: __('Sendtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                         {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
-                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
+                        {
+                            field: 'operate', title: __('Operate'), table: table,
+                            buttons: [{
+                                name: 'detail',
+                                text: __('发送测试'),
+                                icon: 'fa fa-list',
+                                classname: 'btn btn-info btn-xs btn-detail btn-ajax',
+                                url: '/api/test/test',
+                                refresh:true
+                            }],
+                            events: Table.api.events.operate,formatter: Table.api.formatter.operate
+                        }
                     ]
                 ]
             });