Kirin 3 年之前
父节点
当前提交
26dc092ea0
共有 1 个文件被更改,包括 56 次插入23 次删除
  1. 56 23
      app/api/controller/AuthController.php

+ 56 - 23
app/api/controller/AuthController.php

@@ -127,21 +127,25 @@ class AuthController
      */
     public function verify(Request $request)
     {
-        list($phone, $type, $key, $code) = UtilService::postMore([['phone', 0], ['type', ''], ['key', ''], ['code', '']], $request, true);
+        list($phone, $type
+//            , $key, $code
+            ) = UtilService::postMore([['phone', 0], ['type', ''],
+//            ['key', ''], ['code', '']
+        ], $request, true);
 
-        $keyName = 'sms.key.' . $key;
+//        $keyName = 'sms.key.' . $key;
         $nowKey = 'sms.' . date('YmdHi');
 
-        if (!Cache::has($keyName))
-            return app('json')->make(401, '发送验证码失败');
-
-        if (($num = Cache::get($keyName)) > 2) {
-            if (!$code)
-                return app('json')->make(402, '请输入验证码');
-
-            if (!$this->checkCaptcha($key, $code))
-                return app('json')->fail('验证码输入有误');
-        }
+//        if (!Cache::has($keyName))
+//            return app('json')->make(401, '发送验证码失败');
+//
+//        if (($num = Cache::get($keyName)) > 2) {
+//            if (!$code)
+//                return app('json')->make(402, '请输入验证码');
+//
+//            if (!$this->checkCaptcha($key, $code))
+//                return app('json')->fail('验证码输入有误');
+//        }
 
         $total = 1;
         if ($has = Cache::has($nowKey)) {
@@ -149,35 +153,64 @@ class AuthController
             if ($total > Config::get('sms.maxMinuteCount', 20))
                 return app('json')->success('已发送');
         }
-
+        if ($type != 'register' && $type != 'login' && $type != 'reset' && $type != 'BDING_CODE') {
+            $phone = $request->user()['account'];
+        }
         try {
-            validate(RegisterValidates::class)->scene('code')->check(['phone' => $phone]);
+            validate(RegisterValidates::class)->scene('code')->check([(mobile_check($phone) ? 'phone' : 'email') => $phone]);
         } catch (ValidateException $e) {
             return app('json')->fail($e->getError());
         }
-        if (User::checkPhone($phone) && $type == 'register') return app('json')->fail('手机号已注册');
-        if (!User::checkPhone($phone) && $type == 'login') return app('json')->fail('账号不存在!');
-        $default = Config::get('sms.default', 'yunxin');
+        if (User::checkPhone($phone) && ($type == 'register')) return app('json')->fail('帐号已注册');
+        if (!User::checkPhone($phone) && ($type == 'login' || $type == 'reset')) return app('json')->fail('账号不存在!');
+
+        if (mobile_check($phone))
+            $default = Config::get('sms.default', 'aliyun');
+        else
+            $default = Config::get('sms.default_email', 'email');
         $defaultMaxPhoneCount = Config::get('sms.maxPhoneCount', 10);
         $defaultMaxIpCount = Config::get('sms.maxIpCount', 50);
         $maxPhoneCount = Config::get('sms.stores.' . $default . '.maxPhoneCount', $defaultMaxPhoneCount);
         $maxIpCount = Config::get('sms.stores.' . $default . '.maxIpCount', $defaultMaxIpCount);
         if (SmsRecord::where('phone', $phone)->where('add_ip', $request->ip())->whereDay('add_time')->count() >= $maxPhoneCount) {
-            return app('json')->fail('您今日发送得短信次数已经达到上限');
+            return app('json')->fail('您今日发送验证码次数已经达到上限');
         }
         if (SmsRecord::where('add_ip', $request->ip())->whereDay('add_time')->count() >= $maxIpCount) {
             return app('json')->fail('此IP今日发送次数已经达到上限');
         }
-        $time = 60;
+        $time = mobile_check($phone) ? 300 : 300;
         if (CacheService::get('code_' . $phone))
             return app('json')->fail($time . '秒内有效');
         $code = rand(100000, 999999);
         $data['code'] = $code;
-        $res = ShortLetterRepositories::NewSmsSend($phone, $data, 'VERIFICATION_CODE');
-        if ($res !== true)
-            return app('json')->fail('短信平台验证码发送失败' . $res);
+
+        //发短信
+        $temp = function ($item) {
+            switch ($item) {
+                case "register":
+                    return 'REGISTER';
+                case "login":
+                    return 'LOGIN';
+                case "reset":
+                    return 'RESET';
+                case "reset_2":
+                    return 'RESET_2';
+                case "trade":
+                    return 'TRADE';
+                default:
+                    return 'DEFAULT';
+            }
+        };
+        $res = ShortLetterRepositories::NewSmsSend($phone, $data, $temp($type));
+
+        //发短信
+        if ($res !== true) {
+            if (strpos($res, 'User not found') !== false)
+                $res = '邮箱不存在';
+            return app('json')->fail('验证码发送失败:' . $res);
+        }
         CacheService::set('code_' . $phone, $code, $time);
-        Cache::set($keyName, $num + 1, 300);
+//        Cache::set($keyName, $num + 1, 300);
         Cache::set($nowKey, $total, 61);
 
         return app('json')->success('发送成功');