zilong 4 лет назад
Родитель
Сommit
b7a2d771ec

+ 40 - 0
app/Helpers/functions.php

xqd
@@ -302,3 +302,43 @@ if (!function_exists('aly_sms_fetch_content')) {
         return $rtn;
     }
 }
+
+//检测重复请求 超过就禁止访问 有用户flag就针对用户flag 没有flag就针对ip地址(ip的话注意反代情况,可能每个用户请求的ip都是反代服务器的ip,当然可以配置一波反代服务器使得业务服务器获取到真实用户ip) 最小只能设置1s一次请求 不支持1s以下 如果开启了redis可以改写支持毫秒级的方法
+if (!function_exists('check_repeat_request')) {
+    function check_repeat_request($time, $limit, $flag = '')
+    {
+        $action = request()->getPathInfo();
+        if (!empty($flag)){
+            $key = $action.$flag;
+        }
+        else {
+            $ip = request()->ip();
+            $key = $action.$ip;
+        }
+
+        $time = $time < 1 ? 1 : $time;
+        $time = round($time);
+        if (Cache::has($key)){
+            Cache::increment($key);
+            $count = Cache::get($key);
+            if($count > $limit){
+                exit_out(null, 11003, '操作过于频繁,请稍后重试~');
+            }
+        }
+        else {
+            Cache::set($key, 1, $time);
+        }
+
+        return true;
+    }
+}
+
+//随机生成验证码
+if (!function_exists('generate_code')) {
+    function generate_code($length = 6)
+    {
+        $min = pow(10, ($length - 1));
+        $max = pow(10, $length) - 1;
+        return rand($min, $max);
+    }
+}

+ 45 - 0
app/Http/Controllers/Api/V1/CommonController.php

xqd xqd
@@ -12,6 +12,7 @@ use App\Http\Controllers\Controller;
 use App\Models\Area;
 use App\Models\User;
 use EasyWeChat\Factory;
+use Cache;
 
 class CommonController extends Controller
 {
@@ -114,6 +115,50 @@ class CommonController extends Controller
         return out($data1);
     }
 
+    public function sendVerifyCode()
+    {
+        //防止恶意刷验证码接口,一分钟最多10次
+        check_repeat_request(60, 10);
+        $req = request()->post();
+        $this->validate(request(), [
+            'type' => 'required|integer',
+            'phone|手机号' => 'required',
+        ]);
+
+        //注册验证码
+        if ($req['type'] == 1){
+            if (User::where('phone', $req['phone'])->exists()){
+                return out(null, 30006, '该手机号已注册,请登录');
+            }
+        }
+        //登录验证码
+        elseif ($req['type'] == 2){
+            if (!User::where('phone', $req['phone'])->exists()){
+                return out(null, 30006, '该手机号还未注册,请先注册');
+            }
+        }
+        //修改支付密码验证码
+        elseif($req['type'] == 3){
+            $user = User::getUserByToken();
+            if ($user['phone'] != $req['phone']){
+                return out(null, 30007, '该手机号不是注册时的手机号');
+            }
+        }
+        else {
+            return out(null, 30011, '验证码类型不存在');
+        }
+
+        $verify_code = generate_code();
+        $result = send_sms($req['phone'], 'verify_template_code', ['code' => $verify_code]);
+        if (!$result){
+            return out(null, 30010, '验证码发送失败,请稍后重试');
+        }
+
+        Cache::set($req['phone'].'-'.$req['type'], $verify_code, config('config.aly_sms.sms_verify_code_expire'));
+
+        return out();
+    }
+
     public function doc()
     {
         $database = env('DB_DATABASE');

+ 2 - 2
app/Http/Controllers/Api/V1/UserController.php

xqd
@@ -93,13 +93,13 @@ class UserController extends AuthController
         ]);
         $user = $this->user;
 
-        $verify_code = Cache::get($user['phone'].'-1');
+        $verify_code = Cache::get($user['phone'].'-3');
         if($verify_code != $req['verify_code']){
             if (env('APP_ENV') == 'online' || $req['verify_code'] != '111111') {
                 return out(null, 10001, '验证码错误');
             }
         }
-        Cache::delete($user['phone'].'-1');
+        Cache::delete($user['phone'].'-3');
 
         User::where('id', $user['id'])->update(['pay_password' => sha1(md5($req['pay_password']))]);
 

+ 5 - 4
config/config.php

xqd
@@ -26,9 +26,10 @@ return [
     'product_type_map' => [1 => '电话咨询', 2 => '图文咨询', 3 => '门诊预约', 4 => '疫苗接种预约', 5 => '儿保预约', 6 => '服务包', 7 => '充值'],
 
     'aly_sms' => [
-        'access_key' => '',
-        'access_secret' => '',
-        'sign_name' => '',
-        'register_template_code' => '',
+        'access_key' => 'LTAI4FgJdnfwsj5Bb6ioWSD9',
+        'access_secret' => 'S4Tp2eNVKHROuNOLjTvAICTnjqXFFR',
+        'sign_name' => '思维定制',
+        'verify_template_code' => 'SMS_185242509',
+        'sms_verify_code_expire' => 360,
     ],
 ];