Browse Source

7-14前端登录逻辑2

Home 4 năm trước cách đây
mục cha
commit
f5f7572d57

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

@@ -34,7 +34,10 @@ class Login extends BaseController
         }
 
         $result = (new UserBusiness())->login($data);
+        if ($result) {
+            return show(config('status.success'), "登陆成功");
+        }
 
-        return show(config('status.success'), "登陆成功");
+        return show(config('status.error'), "登录失败");
     }
 }

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

@@ -26,7 +26,32 @@ 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
+        // 生成token
+        $user = $this->userObj->getUserByPhoneNumber($data['phoneNumber']);
+
+        if (!$user) {
+            $username = "singewa粉-{$data['phoneNumber']}";
+            $userData = [
+                'username' => $username,
+                'phone_number' => $data['phoneNumber'],
+                'type' => $data['type'],
+                'status' => config('status.mysql.table_normal'),
+            ];
+            try {
+                $this->userObj->save($userData);
+                $userId = $this->userObj->id;
+            }catch (\Exception $e){
+                throw new \think\Exception('数据库内部异常');
+            }
+        } else {
+            // 用户存在,更新表数据
+
+        }
+
+        return true;
     }
 }

+ 11 - 5
app/common/model/mysql/User.php

@@ -15,21 +15,27 @@ use think\Model;
 class User extends Model
 {
     /**
-     * 根据用户名获取后端用户表的数据
-     * @param $username
+     * 自动生成写入时间
+     * @var bool
+     */
+    protected $autoWriteTimestamp = true;
+
+    /**
+     * 根据手机号获取用户信息
+     * @param $phoneNumber
      * @return array|bool|Model|null
      * @throws \think\db\exception\DataNotFoundException
      * @throws \think\db\exception\DbException
      * @throws \think\db\exception\ModelNotFoundException
      */
-    public function getAdminUserByUsername($username)
+    public function getUserByPhoneNumber($phoneNumber)
     {
-        if (empty($username)) {
+        if (empty($phoneNumber)) {
             return false;
         }
 
         $where = [
-            'username' => trim($username),
+            'phone_number' => $phoneNumber
         ];
         return $this->where($where)->find();
     }