Browse Source

奇讯云短信

lizhen_gitee 2 ngày trước cách đây
mục cha
commit
aef3609527

+ 13 - 1
application/common.php

@@ -1014,10 +1014,22 @@ if (!function_exists('now_month')) {
 }
 
 if (!function_exists('dd')) {
-    function dd(...$vars){
+    function dd($vars){
         foreach ($vars as $v) {
             dump($v);
         }
         exit();
     }
+}
+
+/**
+ * 毫秒时间戳
+ * @return int
+ */
+if (!function_exists('ms_time')) {
+    function ms_time()
+    {
+        list($ms, $sec) = explode(' ', microtime());
+        return intval((floatval($ms) + floatval($sec)) * 1000);
+    }
 }

+ 6 - 2
application/common/library/Sms.php

@@ -3,6 +3,7 @@
 namespace app\common\library;
 
 use think\Hook;
+use app\utils\Service\QiXunCloud\SmsService;
 
 /**
  * 短信验证码类
@@ -55,7 +56,7 @@ class Sms
         $time = time();
         $ip = request()->ip();
         $sms = \app\common\model\Sms::create(['event' => $event, 'mobile' => $mobile, 'code' => $code, 'ip' => $ip, 'createtime' => $time]);
-        $result = Hook::listen('sms_send', $sms, null, true);
+        //$result = Hook::listen('sms_send', $sms, null, true);
 
         //阿里短信
         /*$params['mobile'] = $mobile;
@@ -63,10 +64,13 @@ class Sms
 
         $result = self::smsSend($params,86);*/
 
-        if (!$result) {
+        // 奇讯云短信
+        $qxySms = new SmsService();
+        if (!$qxySms->send($mobile,$code)){
             $sms->delete();
             return false;
         }
+
         return true;
     }
 

+ 64 - 0
application/utils/Service/Library.php

@@ -0,0 +1,64 @@
+<?php
+
+namespace app\utils\Service;
+
+use app\utils\CurlUtil;
+
+/**
+ * 扩展返回工具
+ */
+class Library
+{
+    protected $host = '';//请求地址
+    protected $message = 'error';
+    protected $data    = [];
+
+    protected function postJson(string $url, array $params, array $header = [], int $timeout=30)
+    {
+        return CurlUtil::postJson($this->host.$url,$params,$header,$timeout);
+    }
+
+    /**
+     * 返回成功结果
+     * @param string $message
+     * @param mixed $data
+     * @return bool
+     */
+    protected function success(string $message = 'success', $data = [])
+    {
+        $this->message = $message;
+        $this->data    = $data;
+        return true;
+    }
+
+    /**
+     * 返回失败结果
+     * @param string $message
+     * @param mixed $data
+     * @return bool
+     */
+    protected function error(string $message = 'error', $data = [])
+    {
+        $this->message = $message;
+        $this->data    = $data;
+        return false;
+    }
+
+    /**
+     * 获取成功数据
+     * @return mixed
+     */
+    public function getData()
+    {
+        return $this->data;
+    }
+
+    /**
+     * 获取消息
+     * @return string
+     */
+    public function getMessage()
+    {
+        return $this->message;
+    }
+}

+ 43 - 0
application/utils/Service/QiXunCloud/SmsService.php

@@ -0,0 +1,43 @@
+<?php
+
+namespace app\utils\Service\QiXunCloud;
+
+use app\utils\Service\Library;
+
+class SmsService extends Library
+{
+    protected $host      = "http://sms.qxyai.com:9090";
+    private string $appKey    = "3qumdN";
+    private string $appSecret = "rwL6cm";
+    private string $appCode   = "1000";
+    private string $signName  = "新邵县金蟾电子商务";// 新邵县金蟾电子商务有限公司
+
+    /**
+     * 发送验证码
+     * @param string $mobile
+     * @param $code
+     * @return bool
+     */
+    public function send(string $mobile, $code)
+    {
+
+        $timestamp = ms_time();
+        $response  = $this->postJson('/sms/batch/v1', [
+            'sign'      => md5("{$this->appKey}{$this->appSecret}{$timestamp}"),
+            'timestamp' => $timestamp,
+            'phone'     => $mobile,
+            'appcode'   => $this->appCode,
+            'appkey'    => $this->appKey,
+            'msg'       => "【{$this->signName}】您的验证码是{$code},请妥善保管"
+        ]);
+        if (!$response) {
+            return false;
+        }
+
+        if (empty($response['code']) || $response['code'] != "00000"){
+            return false;
+        }
+
+        return true;
+    }
+}