浏览代码

短信接口

linwu 2 周之前
父节点
当前提交
ffecf71346

+ 39 - 1
app/common/service/SmsService.php

@@ -11,7 +11,7 @@ use think\facade\Log;
 class SmsService
 {
 
-    public function send($mobile, $code, $content_param = [])
+    /*public function send($mobile, $code, $content_param = [])
     {
         $message = MessageTemplate::where('code', $code)->find();
         if (empty($message)) {
@@ -29,6 +29,44 @@ class SmsService
         $sms->send($mobile, ['message' => $msg]);
 
         return ['code' => 0];
+    }*/
+
+    public function send($mobile, $code, $content_param = [])
+    {
+        if (env('APP_DEBUG')) {
+            return ['code' => 0];
+        }
+        $url = "https://lw_test.jinjianghc.com/api/sms/send";
+        $token = "JC9hFYJw68be4bbab6a96";
+//        $url = "http://bd.lwtest.com/api/sms/send";
+//        $token = "7ec987cb2daf4c44a84ff1f145c51f2b";
+        $postArr = ['mobile'=>$mobile,'template_code'=>$code,'template_param'=>$content_param];
+        $postFields = json_encode($postArr);
+
+        $ch         = curl_init();
+        curl_setopt($ch, CURLOPT_URL, $url);
+        curl_setopt($ch, CURLOPT_HTTPHEADER, [
+                'Content-Type: application/json; charset=utf-8',   //json版本需要填写  Content-Type: application/json;
+                'token:'.$token
+            ]
+        );
+        curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); //若果报错 name lookup timed out 报错时添加这一行代码
+        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+        curl_setopt($ch, CURLOPT_POST, 1);
+        curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
+        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
+        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
+        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
+        $ret = curl_exec($ch);
+        curl_close($ch);
+        $rsp = json_decode($ret, true);
+
+        if ($rsp['code'] != 0) {
+            Log::record('短信发送失败:' . json_encode($rsp) . "。原始参数:" . json_encode($postArr));
+            return ['code' => 1, 'msg' => $rsp['errorMsg']];
+        } else {
+            return ['code' => 0];
+        }
     }
 
     public function examineSend($code, $content_param = [])

+ 7 - 1
app/mainapp/controller/Login.php

@@ -11,6 +11,7 @@ use app\common\model\UserPart as UserPartModel;
 
 use alisms\SignatureHelper;
 use app\common\service\IntegralService;
+use app\common\service\SmsService;
 use chuanglan\Chuanglan;
 use echowx\WxProgram;
 
@@ -397,5 +398,10 @@ class Login
         page_result(0, "", ['smscodepass' => md5($identifier . $smscode)]);
     }
 
-
+    public function t1()
+    {
+        $sms = new SmsService();
+        $res = $sms->send('13313826760', 'verification_code', ['code' => '123456']);
+        halt($res);
+    }
 }

+ 4 - 4
app/mobile/controller/Login.php

@@ -178,7 +178,7 @@ class Login
             $open_id    = session('user.open_id');
             $nickname   = session('user.nickname');
             $headimgurl = session('user.headimgurl');
-            $user_data = [
+            $user_data  = [
                 'groupsid'       => 7,
                 'brokerid'       => 0,
                 'nickname'       => $nickname,
@@ -211,8 +211,8 @@ class Login
                 'user_tags'      => [],
             ];
 
-            $user       = User::create($user_data);
-            $auth       = UserAuthsModel::create([
+            $user = User::create($user_data);
+            $auth = UserAuthsModel::create([
                 'userid'       => $user['id'],
                 'identitytype' => 'weixin',
                 'identifier'   => $open_id,
@@ -252,7 +252,7 @@ class Login
         } else {
             if (preg_match($preg_phone, $mobile)) {
                 $sms = new SmsService();
-                $res = $sms->send($mobile, 'verification', [$rand]);
+                $res = $sms->send($mobile, 'verification_code', ['code' => $rand]);
 
                 if ($res['code'] == 0) {
                     $rtn['code'] = 0;

+ 1 - 1
app/worker/controller/Login.php

@@ -127,7 +127,7 @@ class Login
         } else {
             if (preg_match($preg_phone, $mobile)) {
                 $sms = new SmsService();
-                $res = $sms->send($mobile, 'verification', [$rand]);
+                $res = $sms->send($mobile, 'verification_code', ['code' => $rand]);
 
                 if ($res['code'] == 0) {
                     $rtn['code'] = 0;