Browse Source

用户登录代码优化

Home 4 years ago
parent
commit
216807d76b
2 changed files with 10 additions and 2 deletions
  1. 9 1
      app/api/controller/Login.php
  2. 1 1
      app/common/business/User.php

+ 9 - 1
app/api/controller/Login.php

@@ -19,6 +19,9 @@ class Login extends BaseController
 {
     public function index(): object
     {
+        if (!$this->request->param('phone_number', '', 'trim')) {
+            return show(config('status.error'), '非法请求');
+        }
         $phoneNumber = $this->request->param("phone_number", '', 'trim');
         $code = input('param.code', 0, 'intval');
         $type = input('param.type', 0, 'intval');
@@ -33,7 +36,12 @@ class Login extends BaseController
             return show(config('status.error'), $validate->getError());
         }
 
-        $result = (new UserBusiness())->login($data);
+        try {
+            $result = (new UserBusiness())->login($data);
+        } catch (\Exception $e) {
+            return show($e->getCode(), $e->getMessage());
+        }
+
         if ($result) {
             return show(config('status.success'), "登陆成功", $result);
         }

+ 1 - 1
app/common/business/User.php

@@ -28,7 +28,7 @@ class User
     {
         $redisCode = cache(config('redis.code_pre') . $data['phoneNumber']);
         if (empty($redisCode) || $redisCode !== $data['code']) {
-//            throw new \think\Exception('验证码不存在', -1009);
+            throw new \think\Exception('验证码不存在', -1009);
         }
 
         // 需要去判断表 是否有 用户记录phoneNumber