Browse Source

短信验证码

zac3533 1 year ago
parent
commit
e1cac70da1
2 changed files with 86 additions and 0 deletions
  1. 25 0
      application/api/common.php
  2. 61 0
      application/api/controller/Sms.php

+ 25 - 0
application/api/common.php

@@ -1 +1,26 @@
 <?php
+
+
+/**
+ * POST请求
+ * @param $postdata
+ * @return bool|string
+ */
+function xcurl($url, $postdata) {
+    $ch = curl_init();
+
+    curl_setopt($ch,CURLOPT_URL,$url); //支付请求地址
+    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
+        'Content-Type: application/json; charset=utf-8',
+        'User-Agent: uni-php-sdk/0.1.0',
+    ));
+    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
+    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
+    curl_setopt($ch, CURLOPT_POST, true);
+    curl_setopt($ch, CURLOPT_HEADER, false);
+    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postdata));
+    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+    $response=curl_exec($ch);
+    curl_close($ch);
+    return $response;
+}

+ 61 - 0
application/api/controller/Sms.php

@@ -6,6 +6,12 @@ use app\common\controller\Api;
 use app\common\library\Sms as Smslib;
 use app\common\model\User;
 use think\Hook;
+use fast\Random;
+use Exception;
+use think\Cache;
+use think\exception\PDOException;
+use think\exception\ValidateException;
+
 
 /**
  * 手机短信接口
@@ -101,4 +107,59 @@ class Sms extends Api
             $this->error(__('验证码不正确'));
         }
     }
+
+
+    /**
+     * 发送短信验证码
+     *
+     * @ApiMethod (POST)
+     * @param string $phone     手机号
+     * @param string $scene     事件名称     
+     * @param string $countryCode 区号
+     * @param string $len       验证码长度
+     */
+    public  function sendCodeSMS($phone, $scene = 'verify',$countryCode = '86', $len = '')
+    {
+
+        $key = $countryCode.$scene . ':' . $phone;
+        $code = Random::numeric($len);
+        //$content = $content == '' ? sprintf(SMSTemplates($scene,$countryCode),$code) : sprintf($content,$code);
+        //$phone = $countryCode . $phone;
+        $phone = '+' . $countryCode . $phone;//拼接国际区号
+        try {
+            $url1 = "https://uni.apistd.com";
+            $query = [
+                'action' => 'sms.message.send',
+                'accessKeyId' => 'SGnbSrqzJikDxx4PuU83kD9oTTmv7o34unZ2bPX8FqsgCrQkp'
+            ];
+            $url = $url1.'/?'.http_build_query($query);
+            $data = [
+                'signature'=>'AEXBTC',
+                'to'=>$phone,
+                //'content'=>$content,
+                'templateId' => 'd33f1f90',
+                'templateData' => ['code' => $code]
+            ];
+            $result = xcurl($url,$data);
+            $result = json_decode($result, true);
+    //        var_dump($result);
+
+    //        if($result['code'] == 0){
+    //            $result = 1;
+    //        }else {
+    //            $result = 0;
+    //        }
+        } catch (ValidateException|PDOException|Exception $e){
+            $this->error($e->getMessage());
+        }
+        if ($result['code'] == 0){
+            Cache::set($key, $code,300);
+            return true;
+        }else{
+            return $result['message'];
+        }
+    }
+
+
+
 }