1.优化设备只能登录一个
This commit is contained in:
@@ -12,6 +12,7 @@ use app\common\model\UserScoreLog;
|
||||
use app\common\model\UserMoneyLog;
|
||||
use app\common\controller\Frontend;
|
||||
use app\common\facade\Token as TokenFacade;
|
||||
use app\common\service\MobileAuthDeviceService;
|
||||
use support\think\Db;
|
||||
use support\validation\Validator;
|
||||
use support\validation\ValidationException;
|
||||
@@ -44,6 +45,10 @@ class Account extends Frontend
|
||||
|
||||
$user = $this->auth->getUser();
|
||||
$userId = intval(strval($user->id));
|
||||
$deviceError = MobileAuthDeviceService::validateUserDeviceSession($authToken, $userId);
|
||||
if ($deviceError !== null) {
|
||||
return $this->mobileResult(1101, $deviceError);
|
||||
}
|
||||
$coinBalance = WithdrawFlow::amountString($user->coin ?? '0');
|
||||
|
||||
// 打码量 / 提现配额快照
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -6,6 +6,7 @@ namespace app\api\controller;
|
||||
|
||||
use app\common\controller\Frontend;
|
||||
use app\common\facade\Token;
|
||||
use app\common\service\MobileAuthDeviceService;
|
||||
use support\Response;
|
||||
use Webman\Http\Request;
|
||||
use function response;
|
||||
@@ -28,6 +29,7 @@ abstract class MobileBase extends Frontend
|
||||
$parts = explode('/', $path);
|
||||
$action = $parts[array_key_last($parts)] ?? '';
|
||||
$needAuthToken = !action_in_arr($this->noNeedAuthToken, $action);
|
||||
$authToken = '';
|
||||
if ($needAuthToken) {
|
||||
$authToken = trim((string) $request->header('auth-token', ''));
|
||||
if ($authToken === '') {
|
||||
@@ -41,7 +43,20 @@ abstract class MobileBase extends Frontend
|
||||
}
|
||||
}
|
||||
|
||||
return $this->initializeFrontend($request);
|
||||
$response = $this->initializeFrontend($request);
|
||||
if ($response !== null) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
$needLogin = !action_in_arr($this->noNeedLogin, $action);
|
||||
if ($needAuthToken && $needLogin && $this->auth->isLogin()) {
|
||||
$deviceError = MobileAuthDeviceService::validateUserDeviceSession($authToken, (int) $this->auth->id);
|
||||
if ($deviceError !== null) {
|
||||
return $this->mobileError(1101, $deviceError);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
protected function mobileSuccess(array $data = [], string $message = 'ok'): Response
|
||||
|
||||
@@ -6,6 +6,7 @@ namespace app\api\controller;
|
||||
|
||||
use app\common\controller\Api;
|
||||
use app\common\facade\Token;
|
||||
use app\common\service\MobileAuthDeviceService;
|
||||
use ba\Random;
|
||||
use Webman\Http\Request;
|
||||
use support\Response;
|
||||
@@ -64,6 +65,7 @@ class V1 extends Api
|
||||
$token = Random::uuid();
|
||||
$expire = 60 * 60 * 24;
|
||||
Token::set($token, 'auth-token', 0, $expire);
|
||||
MobileAuthDeviceService::bindAuthTokenDevice($token, $deviceId, $expire);
|
||||
|
||||
return $this->mobileResult(1, 'ok', [
|
||||
'auth_token' => $token,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
<?php
|
||||
return [
|
||||
'Login expired, please login again.' => 'Login expired, please login again.',
|
||||
'Logged in on another device, please login again.' => 'Your account was logged in on another device. Please sign in again.',
|
||||
'Account not exist' => 'Account does not exist',
|
||||
'Account disabled' => 'Account is disabled',
|
||||
'Token login failed' => 'Token login failed',
|
||||
|
||||
@@ -31,6 +31,7 @@ return [
|
||||
'Mobile' => '手机号',
|
||||
'Password' => '密码',
|
||||
'Login expired, please login again.' => '登录过期,请重新登录。',
|
||||
'Logged in on another device, please login again.' => '您的账号已在其他设备登录,请重新登录',
|
||||
'Account not exist' => '帐户不存在',
|
||||
'Account disabled' => '帐户已禁用',
|
||||
'Token login failed' => '令牌登录失败',
|
||||
|
||||
Reference in New Issue
Block a user