Browse Source

用户注册,发短信

lizhen_gitee 4 months ago
parent
commit
5e3f34e25a

+ 3 - 3
application/api/controller/Sms.php

@@ -52,14 +52,14 @@ class Sms extends Api
                 $this->error(__('未注册'));
             }
         }
-        if (!Hook::get('sms_send')) {
+        /*if (!Hook::get('sms_send')) {
             $this->error(__('请在后台插件管理安装短信验证插件'));
-        }
+        }*/
         $ret = Smslib::send($mobile, null, $event);
         if ($ret) {
             $this->success(__('发送成功'));
         } else {
-            $this->error(__('发送失败,请检查短信配置是否正确'));
+            $this->error(__('发送失败,请联系客服'));
         }
     }
 

+ 1 - 2
application/api/controller/User.php

@@ -57,8 +57,7 @@ class User extends Api
         }
         if ($ret) {
             Sms::flush($mobile, 'mobilelogin');
-            $data = ['userinfo' => $this->auth->getUserinfo()];
-            $this->success(__('Logged in successful'), $data);
+            $this->success(__('Logged in successful'), $this->auth->getUserinfo());
         } else {
             $this->error($this->auth->getError());
         }

+ 170 - 0
application/common/library/Alisms.php

@@ -0,0 +1,170 @@
+<?php
+
+namespace app\common\library;
+
+/**
+ * 阿里大于SMS短信发送
+ */
+class Alisms
+{
+    private $_params = [];
+    public $error = '';
+    protected $config = [];
+    protected static $instance;
+
+    public function __construct($options = [])
+    {
+        if ($config = config('alisms')) {
+            $this->config = array_merge($this->config, $config);
+        }
+        $this->config = array_merge($this->config, is_array($options) ? $options : []);
+    }
+
+    /**
+     * 单例
+     * @param array $options 参数
+     * @return Alisms
+     */
+    public static function instance($options = [])
+    {
+        if (is_null(self::$instance)) {
+            self::$instance = new static($options);
+        }
+
+        return self::$instance;
+    }
+
+    /**
+     * 设置签名
+     * @param string $sign
+     * @return Alisms
+     */
+    public function sign($sign = '')
+    {
+        $this->_params['SignName'] = $sign;
+        return $this;
+    }
+
+    /**
+     * 设置参数
+     * @param array $param
+     * @return Alisms
+     */
+    public function param(array $param = [])
+    {
+        foreach ($param as $k => &$v) {
+            $v = (string)$v;
+        }
+        unset($v);
+        $param = array_filter($param);
+        $this->_params['TemplateParam'] = $param ? json_encode($param) : '{}';
+        return $this;
+    }
+
+    /**
+     * 设置模板
+     * @param string $code 短信模板
+     * @return Alisms
+     */
+    public function template($code = '')
+    {
+        $this->_params['TemplateCode'] = $code;
+        return $this;
+    }
+
+    /**
+     * 接收手机
+     * @param string $mobile 手机号码
+     * @return Alisms
+     */
+    public function mobile($mobile = '')
+    {
+        $this->_params['PhoneNumbers'] = $mobile;
+        return $this;
+    }
+
+    /**
+     * 立即发送
+     * @return boolean
+     */
+    public function send()
+    {
+        $this->error = '';
+        $params = $this->_params();
+        $params['Signature'] = $this->_signed($params);
+        $response = $this->_curl($params);
+        if ($response !== false) {
+            $res = (array)json_decode($response, true);
+            if (isset($res['Code']) && $res['Code'] == 'OK') {
+                return true;
+            }
+            $this->error = isset($res['Message']) ? $res['Message'] : 'InvalidResult';
+        } else {
+            $this->error = 'InvalidResult';
+        }
+        return false;
+    }
+
+    /**
+     * 获取错误信息
+     * @return string
+     */
+    public function getError()
+    {
+        return $this->error;
+    }
+
+    private function _params()
+    {
+        return array_merge([
+            'AccessKeyId'      => $this->config['key'],
+            'SignName'         => isset($this->config['sign']) ? $this->config['sign'] : '',
+            'Action'           => 'SendSms',
+            'Format'           => 'JSON',
+            'Version'          => '2017-05-25',
+            'SignatureVersion' => '1.0',
+            'SignatureMethod'  => 'HMAC-SHA1',
+            'SignatureNonce'   => uniqid(),
+            'Timestamp'        => gmdate('Y-m-d\TH:i:s\Z'),
+        ], $this->_params);
+    }
+
+    private function percentEncode($string)
+    {
+        $string = urlencode($string);
+        $string = preg_replace('/\+/', '%20', $string);
+        $string = preg_replace('/\*/', '%2A', $string);
+        $string = preg_replace('/%7E/', '~', $string);
+        return $string;
+    }
+
+    private function _signed($params)
+    {
+        $sign = $this->config['secret'];
+        ksort($params);
+        $canonicalizedQueryString = '';
+        foreach ($params as $key => $value) {
+            $canonicalizedQueryString .= '&' . $this->percentEncode($key) . '=' . $this->percentEncode($value);
+        }
+        $stringToSign = 'GET&%2F&' . $this->percentencode(substr($canonicalizedQueryString, 1));
+        $signature = base64_encode(hash_hmac('sha1', $stringToSign, $sign . '&', true));
+        return $signature;
+    }
+
+    private function _curl($params)
+    {
+        $uri = 'http://dysmsapi.aliyuncs.com/?' . http_build_query($params);
+        $ch = curl_init();
+        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
+        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
+        curl_setopt($ch, CURLOPT_URL, $uri);
+        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
+        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.98 Safari/537.36");
+        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
+        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
+        $reponse = curl_exec($ch);
+        curl_close($ch);
+        return $reponse;
+    }
+}

+ 3 - 3
application/common/library/Auth.php

@@ -26,7 +26,7 @@ class Auth
     //默认配置
     protected $config = [];
     protected $options = [];
-    protected $allowFields = ['id', 'username', 'nickname', 'mobile', 'avatar', 'idcard_status'];
+    protected $allowFields = ['id', 'nickname', 'mobile', 'avatar'];
 
     public function __construct($options = [])
     {
@@ -166,8 +166,8 @@ class Auth
         try {
             $user = User::create($params, true);
 
-            /*$this->_user = User::get($user->id);
-            $this->_user->username = 'u' . (10000 + $user->id);
+            $this->_user = User::get($user->id);
+            /*$this->_user->username = 'u' . (10000 + $user->id);
             $this->_user->save();*/
 
             //设置Token

+ 27 - 1
application/common/library/Sms.php

@@ -55,7 +55,13 @@ 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;
+        $params['code'] = $code;
+        //阿里短信
+        $result = self::smsSend($params);
+
         if (!$result) {
             $sms->delete();
             return false;
@@ -64,6 +70,26 @@ class Sms
     }
 
     /**
+     * 短信发送行为
+     * @param array $params 必须包含mobile,event,code
+     * @return  boolean
+     */
+    public static function smsSend($params)
+    {
+        $config = config('alisms');
+
+        $template = $config['template_cn'];  //默认国内模板
+
+
+        $alisms = new Alisms();
+        $result = $alisms->mobile($params['mobile'])
+            ->template($template)
+            ->param(['code' => $params['code']])
+            ->send();
+        return $result;
+    }
+
+    /**
      * 发送通知
      *
      * @param   mixed  $mobile   手机号,多个以,分隔

+ 9 - 0
application/config.php

@@ -333,6 +333,15 @@ return [
             'votes'   => '投票与答题设置',
     ],
 
+    //阿里云短信配置, 简讯正式
+    'alisms' =>[
+        'template_cn' => 'SMS_302821495', //国内
+        'template_guoji' => '', //国际
+        'sign' => '简讯同城',
+        'key' => 'LTAI5tBa9nd43kopxaGWi3yx',   //AccessKey ID
+        'secret' => 'pXZS00YCjjf5WWDDy8w34R6ReGkirj',  //AccessKey Secret
+    ],
+
 
     //微信小程序 客户正式
     'wxMiniProgram' => [