1.优化设备只能登录一个

This commit is contained in:
2026-05-29 14:19:24 +08:00
parent 1df1396b44
commit 4324c19d30
8 changed files with 217 additions and 2 deletions

View File

@@ -7,6 +7,7 @@ namespace app\api\controller;
use app\common\facade\Token;
use app\common\library\Auth as UserAuth;
use app\common\model\User;
use app\common\service\MobileAuthDeviceService;
use ba\Random;
use support\think\Db;
use Webman\Http\Request;
@@ -83,6 +84,8 @@ class Auth extends MobileBase
return $this->mobileError(2000, 'Registered successfully but login failed');
}
$this->bindMobileDeviceSession($request);
return $this->mobileSuccess($this->buildLoginPayload());
}
@@ -106,6 +109,9 @@ class Auth extends MobileBase
if (!$ok) {
return $this->mobileError(1101, 'Incorrect account or password');
}
$this->bindMobileDeviceSession($request);
return $this->mobileSuccess($this->buildLoginPayload());
}
@@ -126,14 +132,41 @@ class Auth extends MobileBase
return $this->mobileError(1101, 'Login status has expired');
}
$authToken = trim((string) $request->header('auth-token', ''));
$userId = filter_var($tokenData['user_id'] ?? 0, FILTER_VALIDATE_INT);
if ($userId === false || $userId <= 0) {
return $this->mobileError(1101, 'Login status has expired');
}
$deviceError = MobileAuthDeviceService::validateUserDeviceSession($authToken, (int) $userId);
if ($deviceError !== null) {
return $this->mobileError(1101, $deviceError);
}
$newToken = Random::uuid();
Token::set($newToken, UserAuth::TOKEN_TYPE, $tokenData['user_id'], config('buildadmin.user_token_keep_time', 259200));
Token::set($newToken, UserAuth::TOKEN_TYPE, (int) $userId, config('buildadmin.user_token_keep_time', 259200));
return $this->mobileSuccess([
'user-token' => $newToken,
'expires_in' => config('buildadmin.user_token_keep_time', 259200),
]);
}
private function bindMobileDeviceSession(Request $request): void
{
if (!$this->auth->isLogin()) {
return;
}
$authToken = trim((string) $request->header('auth-token', ''));
if ($authToken === '') {
return;
}
MobileAuthDeviceService::onUserLogin(
(int) $this->auth->id,
$authToken,
$this->auth->getToken(),
$this->auth->getRefreshToken()
);
}
private function buildLoginPayload(): array
{
$userInfo = $this->auth->getUserInfo();