scene('login')->check($param); // 获取用户 $info = AdminModel::getByUserName($param['user_name']); if(empty($info)){ return $this->toData('200300', 'Incorrect account or password.1', []); } if($info->status!==1){ return $this->toData('200300', '该账户已被禁用', []); } //查看角色状态 $role=AuthRoleModel::find($info->role_id); if(!$role||$role->status!==1){ return $this->toData('200300', '该账户所在的群组已被禁用', []); } // 校验密码 $checkPasswordBool = (new UnqId())->checkPassword($param['password'], $info->password,env('ENCRYPT.ADMINSALT')); if(!$checkPasswordBool){ return $this->toData('200300', 'Incorrect account or password.3', []); } $token = (new Jwt())->generateToken($info->id); if(empty($token)){ return $this->toData('200400', 'The system is busy. Please try again later.1', []); } //保存token $info->token=$token; $info->save(); // $key = 'TOKEN:USER:'.$token; // $expired = 3600*24; // Cache::store('redis')->set($key, 9999999999, $expired); $info=$info->toArray(); $this->setUserTokenCache($token, $info['id']); // 返回结果以及用户信息 return $this->toData('0', 'Request successful.', [ 'id' => $info['id'], 'nickname' => $info['nick_name'], 'email' => $info['email'], 'token' => $token, ]); }catch (ValidateException $validateException) { // 参数校验失败 异常类 $message = $validateException->getError(); return $this->toData('100400', $message); }catch (\Exception $exception){ return $this->toData('200500', 'The system is busy. Please try again later.', [$exception->getMessage(), $exception->getTrace()]); } } public function setUserTokenCache($token, $userId) { // 清除之前的token 设置新的token $userTokenKey = 'USER:TOKEN:'.$userId; // 根据用户id 查找token $oldToken = Cache::store('redis')->get($userTokenKey); if($oldToken){ $oldTokenKey = 'TOKEN:USER:'.$oldToken; Cache::store('redis')->delete($oldTokenKey); } //trace('---设置用户登陆凭证--'.$userId.'----'.$token, 'error'); $tokenKey = 'TOKEN:USER:'.$token; // 根据token查找用户id $expired = 30 * 24 * 60 * 60; // 由中间件自动续期 Cache::store('redis')->set($tokenKey, $userId, $expired); Cache::store('redis')->set($userTokenKey, $token, $expired); } }