소스 검색

短信SDK

bianjunhui 1 년 전
부모
커밋
d0996bf53f
5개의 변경된 파일131개의 추가작업 그리고 2개의 파일을 삭제
  1. 34 0
      application/api/controller/Assms.php
  2. 56 0
      application/common/model/Assms.php
  3. 38 0
      application/job/Sendsms.php
  4. 2 1
      composer.json
  5. 1 1
      public/assets/js/backend/sms/log.js

+ 34 - 0
application/api/controller/Assms.php

@@ -86,4 +86,38 @@ class Assms extends Api
         $message=str_replace($search,$name,$template_body);
         $message=str_replace($search,$name,$template_body);
         return $message;
         return $message;
     }
     }
+
+
+    public function  addLogQueue(){
+        $model = new \app\admin\model\sms\Log;
+        $where['sendtime']=array('lt',time());
+        $where['status']=1;
+        $one_push = Db::name('sms_log')->where($where)->order('id')->limit(10)->select();
+        if (empty($one_push)) {
+            echo '没有查到待发送短信';
+            exit;
+        }
+        // 1.当前任务将由哪个类来负责处理。
+        //   当轮到该任务时,系统将生成一个该类的实例,并调用其 fire 方法
+        $jobHandlerClassName  ='as\app\job\Smssend';
+        // 2.当前任务归属的队列名称,如果为新队列,会自动创建
+        $jobQueueName     = "createSmsJob";
+        $new=array();
+        foreach ($one_push as $k=>$v){
+            $new[$k]['log_id']=$v['log_id'];
+            $new[$k]['status']=2;
+            $jobData['log_id']=$v['log_id'];
+            $jobData['mobile']=$v['mobile'];
+            $jobData['template_body']=$v['template_body'];
+            $isPushed = Queue::push($jobHandlerClassName , $jobData , $jobQueueName);
+            // database 驱动时,返回值为 1|false  ;   redis 驱动时,返回值为 随机字符串|false
+            if ($isPushed !== false) {
+                echo $v['mobile']." 队列加入成功";
+            } else {
+                file_get_contents('test.txt',$v['mobile'],FILE_APPEND);
+                echo "队列加入失败";
+            }
+        }
+        $model->saveAll($new);
+    }
 }
 }

+ 56 - 0
application/common/model/Assms.php

@@ -0,0 +1,56 @@
+<?php
+
+namespace app\common\model;
+
+use think\Model;
+
+
+class Assms Extends Model
+{
+
+    public function sendSms($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; // 返回数据
+    }
+
+    public static function httpGet($url)
+    {
+        // 初始化cURL会话
+        $ch = curl_init();
+// 设置cURL选项
+        curl_setopt($ch, CURLOPT_URL, $url); // 你要访问的URL
+        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 将curl_exec()获取的信息以字符串返回,而不是直接输出
+        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//curl 取消 ssl 检查
+// 执行cURL会话
+        $response = curl_exec($ch);
+// 检查是否有错误发生
+        if(curl_errno($ch)){
+            echo 'cURL error: ' . curl_error($ch);
+        }
+// 关闭cURL会话
+        curl_close($ch);
+// 打印响应内容
+        return $response;
+    }
+
+}

+ 38 - 0
application/job/Sendsms.php

@@ -0,0 +1,38 @@
+<?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 Sendsms
+{
+    public function fire(Job $job, $data)
+    {
+        $send =new Assms();
+        $result = $send->sendSms($data);
+        if (isset($result['msg'])&&!empty($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);
+            $job->delete(); // 删除任务
+        }else{
+            if ($job->attempts() > 3) {
+                $job->delete(); // 删除任务
+            }else{
+                //如果任务执行失败, 重发 100秒后在执行
+                $job->release(100);
+            }
+        }
+    }
+}

+ 2 - 1
composer.json

@@ -29,7 +29,8 @@
         "ext-curl": "*",
         "ext-curl": "*",
         "ext-pdo": "*",
         "ext-pdo": "*",
         "ext-bcmath": "*",
         "ext-bcmath": "*",
-        "onesignal/onesignal-php-api": "*@dev"
+        "onesignal/onesignal-php-api": "*@dev",
+        "clicksend/clicksend-php": "^5.0"
     },
     },
     "config": {
     "config": {
         "preferred-install": "dist",
         "preferred-install": "dist",

+ 1 - 1
public/assets/js/backend/sms/log.js

@@ -36,7 +36,7 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
                         {field: 'quote_id', title: __('购物车ID')},
                         {field: 'quote_id', title: __('购物车ID')},
                         {field: 'addtime', title: __('添加时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                         {field: 'addtime', title: __('添加时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                         {field: 'uptime', title: __('发送时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                         {field: 'uptime', title: __('发送时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
-                        {field: 'status', title: __('Status'), searchList: {"1":__('待发送'),"2":__('已发送')}, formatter: Table.api.formatter.status},
+                        {field: 'status', title: __('Status'), searchList: {"1":__('待发送'),"2":__('发送中'),"3":__('已发送')}, 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, events: Table.api.events.operate, formatter: Table.api.formatter.operate}