up. 添加lock锁屏功能
This commit is contained in:
parent
a932f316cf
commit
6fceb65aa7
14
app/entity/SysUserClient.php
Normal file
14
app/entity/SysUserClient.php
Normal file
@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace app\entity;
|
||||
|
||||
use think\Entity;
|
||||
|
||||
/**
|
||||
* 系统用户客户端实体
|
||||
* @see \app\model\SysUserClient
|
||||
*/
|
||||
class SysUserClient extends Entity
|
||||
{
|
||||
|
||||
}
|
||||
17
app/model/SysUserClient.php
Normal file
17
app/model/SysUserClient.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace app\model;
|
||||
|
||||
use app\BaseModel;
|
||||
|
||||
/***
|
||||
* 系统用户客户端模型
|
||||
* @property int $id
|
||||
* @property int $user_id
|
||||
* @property string $client_id
|
||||
*/
|
||||
class SysUserClient extends BaseModel
|
||||
{
|
||||
protected $name = "sys_user_client";
|
||||
protected $pk = "id";
|
||||
}
|
||||
@ -7,7 +7,7 @@ use Firebase\JWT\JWT;
|
||||
use Firebase\JWT\Key;
|
||||
use Hashids\Hashids;
|
||||
use stdClass;
|
||||
use app\entity\{SysLoginRecord, SysUser};
|
||||
use app\entity\{SysLoginRecord, SysUser, SysUserClient};
|
||||
use UnexpectedValueException;
|
||||
use app\enum\{UserLoginEnum, UserStatusEnum, UserTypeEnum};
|
||||
use app\http\HttpClient;
|
||||
@ -38,23 +38,28 @@ class LoginService
|
||||
validate(LoginValidate::class)
|
||||
->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);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user