diff --git a/app/entity/SysUserClient.php b/app/entity/SysUserClient.php new file mode 100644 index 0000000..9b8854d --- /dev/null +++ b/app/entity/SysUserClient.php @@ -0,0 +1,14 @@ +scene($clientName . 'Login') ->check(compact('username', 'password', 'remember', 'code')); - $user = SysUser::where(['username' => $username])->find(); - if (empty($user)) { + $userModel = SysUser::where(['username' => $username])->find(); + if (empty($userModel)) { $this->saveLoginRecord($request, $username, UserLoginEnum::USERNAME_ERROR, "用户名错误"); throw new ValidateException("用户名或密码错误"); } - if (!password_verify($password, $user->password)) { + if (!password_verify($password, $userModel->password)) { $this->saveLoginRecord($request, $username, UserLoginEnum::PASSWORD_ERROR, "密码错误"); throw new ValidateException("用户名或密码错误"); } - if ($user->status == UserStatusEnum::Disable->value) { + if ($userModel->status == UserStatusEnum::Disable->value) { $this->saveLoginRecord($request, $username, UserLoginEnum::STATUS_DISABLED, "账号被禁用"); throw new ValidateException("账户不可用"); } try { - $access_token = $user->getAccessToken(); - $user = $user->append(['authorities', 'roles'])->toArray(); + $access_token = $userModel->getAccessToken(); + $user = $userModel->append(['authorities', 'roles'])->toArray(); + + // 保存客户端信息 + $this->saveClientRecord($client, $userModel); + // 保存登录日志 $this->saveLoginRecord($request, $username, UserLoginEnum::LOGIN_SUCCESS, "登录成功"); + } catch (Exception $e) { $this->saveLoginRecord($request, $username, UserLoginEnum::LOGIN_EXCEPTION, $e->getMessage()); throw $e; @@ -122,4 +127,23 @@ class LoginService { return new HttpAuth(-1, UserTypeEnum::VISITOR); } + + private function saveClientRecord(HttpClient $client, SysUser|\app\model\SysUser $userModel) + { + $clientModel = SysUserClient::where([ + 'client_id'=> $client->id, + 'client_name'=> $client->name, + 'user_id' => $userModel->user_id + ])->findOrEmpty(); + $clientUpdate = []; + if($clientModel->isEmpty()) { + $clientUpdate['client_id'] = $client->id; + $clientUpdate['user_id'] = $userModel->user_id; + $clientUpdate['client_name'] = $client->name; + $clientUpdate['create_time'] = date('Y-m-d H:i:s'); + } + $clientUpdate['client_version'] = $client->version; + $clientUpdate['last_online_time'] = date('Y-m-d H:i:s'); + $clientModel->save($clientUpdate); + } } \ No newline at end of file