更新
This commit is contained in:
@@ -6,6 +6,8 @@ namespace app\mcp\service;
|
||||
use app\adminapi\service\AdminTokenService;
|
||||
use app\common\cache\AdminTokenCache;
|
||||
use app\common\model\auth\AdminSession;
|
||||
use app\common\model\auth\Admin;
|
||||
use think\facade\Db;
|
||||
use think\cache\driver\File as FileCache;
|
||||
|
||||
/**
|
||||
@@ -37,9 +39,16 @@ class ConsoleService
|
||||
if (!$identity->can(self::PERMISSION)) {
|
||||
throw new McpException('当前账号没有“允许 AI 使用后台浏览器”权限(ai.mcp/console),请联系管理员在角色里勾选', 'no_console_permission', 403);
|
||||
}
|
||||
$result = Db::transaction(static function () use ($identity): array {
|
||||
// TTL调整也必须和签发处于同一个账号锁内,避免改密撤销后旧open请求把会话复活。
|
||||
$admin = Admin::where('id', $identity->adminId)->lock(true)->findOrEmpty();
|
||||
if ($admin->isEmpty() || (int) $admin['disable'] !== 0) {
|
||||
throw new McpException('后台账号不可用', 'console_failed', 403);
|
||||
}
|
||||
// 第三个参数 1:同一终端已有未过期的会话就沿用,不轮换令牌(不影响其他终端)
|
||||
AdminTokenService::setToken($identity->adminId, self::TERMINAL, 1);
|
||||
$session = AdminSession::where(['admin_id' => $identity->adminId, 'terminal' => self::TERMINAL])->findOrEmpty();
|
||||
$issued = AdminTokenService::setToken($identity->adminId, self::TERMINAL, 1);
|
||||
$session = AdminSession::where(['admin_id' => $identity->adminId, 'terminal' => self::TERMINAL])
|
||||
->where('token', $issued['token'] ?? '')->where('expire_time', '>', time())->lock(true)->findOrEmpty();
|
||||
if ($session->isEmpty()) {
|
||||
throw new McpException('后台会话创建失败,请稍后再试', 'console_failed', 500);
|
||||
}
|
||||
@@ -47,8 +56,6 @@ class ConsoleService
|
||||
$session->expire_time = $now + McpConfig::consoleTtlMinutes() * 60;
|
||||
$session->update_time = $now;
|
||||
$session->save();
|
||||
// setToken 按本次请求的 IP 写了登录缓存;删掉,让浏览器第一次请求时按它自己的 IP 重建
|
||||
self::forgetLogin((string) $session->token);
|
||||
return [
|
||||
'token' => (string) $session->token,
|
||||
'expire_time' => (int) $session->expire_time,
|
||||
@@ -56,6 +63,10 @@ class ConsoleService
|
||||
'local_storage' => [self::STORAGE_KEY => json_encode(['expire' => '', 'value' => (string) $session->token])],
|
||||
'start_path' => '/admin/',
|
||||
];
|
||||
});
|
||||
// 数据提交后清登录缓存,让真实浏览器第一次请求时按它自己的IP建立原绑定。
|
||||
self::forgetLogin($result['token']);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/** 作废该账号的 AI 浏览器会话;没有有效会话时返回 false */
|
||||
|
||||
Reference in New Issue
Block a user