userObj = new UserModel(); } public function login(array $data) { $redisCode = cache(config('redis.code_pre') . $data['phoneNumber']); if (empty($redisCode) || $redisCode !== $data['code']) { 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 { // 用户存在,更新表数据 $userId = $user->id; $username = $user->username; } $token = Str::getLoginToken($data['phoneNumber']); $redisData = [ 'id' => $userId, 'username' => $username, ]; $res = cache(config('redis.token_pre') . $token, $redisData, Time::userLoginExpiresTime($data['type'])); return $res ? ['token' => $token, 'username' => $username] : false; } }