igengx
This commit is contained in:
@@ -42,7 +42,13 @@ class AdminTokenService
|
||||
(int) $terminal === AdminTerminalEnum::BCR ? 86400 * 150 : 3600);
|
||||
}
|
||||
|
||||
/** 与改密同一事务调用;只撤销 BCR,会话有效性由 DB 决定,旧缓存不能续活。 */
|
||||
/** 改密撤销原生终端的独立凭据。 */
|
||||
public static function revokeNativeSessions(int $adminId): void
|
||||
{
|
||||
self::revokeBcrSessions($adminId);
|
||||
DesktopSessionService::closeForAdmin($adminId, 'revoked');
|
||||
}
|
||||
|
||||
public static function revokeBcrSessions(int $adminId): void
|
||||
{
|
||||
AdminSession::where('admin_id', $adminId)->where('terminal', AdminTerminalEnum::BCR)
|
||||
@@ -63,6 +69,9 @@ class AdminTokenService
|
||||
*/
|
||||
public static function setToken($adminId, $terminal, $multipointLogin = 1)
|
||||
{
|
||||
if ((int) $terminal === AdminTerminalEnum::WORKSTATION) {
|
||||
return DesktopSessionService::open((int) $adminId);
|
||||
}
|
||||
$time = time();
|
||||
$adminSession = AdminSession::where([['admin_id', '=', $adminId], ['terminal', '=', $terminal]])->find();
|
||||
|
||||
@@ -113,6 +122,9 @@ class AdminTokenService
|
||||
if ($adminSession->isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
if ((int) $adminSession->terminal === AdminTerminalEnum::WORKSTATION) {
|
||||
return false; // 只有桌面心跳可以续租,不能跨过每天 05:00。
|
||||
}
|
||||
if ((int) $adminSession->terminal === AdminTerminalEnum::BCR) {
|
||||
$previousExpiry = (int) $adminSession->expire_time;
|
||||
if ($previousExpiry <= $time || !Admin::where('id', $adminSession->admin_id)
|
||||
@@ -153,6 +165,9 @@ class AdminTokenService
|
||||
return false;
|
||||
}
|
||||
|
||||
if ((int) $adminSession->terminal === AdminTerminalEnum::WORKSTATION) {
|
||||
return (bool) DesktopSessionService::update((string) $token, 'logout');
|
||||
}
|
||||
// 其他终端保留多处登录的原有退出语义。
|
||||
// BCR 必须注销;同账号多台 BCR 复用一个 token 时,这些设备会一起失效。
|
||||
if ((int) $adminSession->terminal !== AdminTerminalEnum::BCR
|
||||
|
||||
@@ -88,7 +88,7 @@ final class AssistantRequestControl
|
||||
/** Atomic ownership record immediately before this generation's first POST execution. */
|
||||
public function beginSubmission(string $profile, string $user): string
|
||||
{
|
||||
if (!in_array($profile, ['qwen', 'openai'], true) || strlen($user) > 128) throw new \RuntimeException('Invalid submission scope');
|
||||
if (!in_array($profile, ['qwen', 'openai', 'doubao'], true) || strlen($user) > 128) throw new \RuntimeException('Invalid submission scope');
|
||||
$token = bin2hex(random_bytes(12));
|
||||
return $this->update(static function (array &$state) use ($profile, $user, $token): string {
|
||||
if (!self::canStartState($state)) throw new \RuntimeException('UPSTREAM_CLEANUP_PENDING');
|
||||
@@ -198,7 +198,7 @@ final class AssistantRequestControl
|
||||
|
||||
public function bindTask(string $profile, string $user, string $taskId, string $workflowRunId = '', string $submissionId = ''): void
|
||||
{
|
||||
if (!in_array($profile, ['qwen', 'openai'], true)
|
||||
if (!in_array($profile, ['qwen', 'openai', 'doubao'], true)
|
||||
|| !self::validUpstreamId($taskId) || strlen($user) > 128
|
||||
|| ($workflowRunId !== '' && !self::validUpstreamId($workflowRunId))) {
|
||||
throw new \RuntimeException('Invalid upstream task identity');
|
||||
@@ -573,7 +573,7 @@ final class AssistantRequestControl
|
||||
$task = $target['upstream'] ?? [];
|
||||
if (!is_array($task) || !is_string($task['task_id'] ?? null) || !self::validUpstreamId($task['task_id'])
|
||||
|| !is_string($task['workflow_run_id'] ?? null) || !self::validUpstreamId($task['workflow_run_id'])
|
||||
|| !in_array($task['profile'] ?? '', ['qwen', 'openai'], true)
|
||||
|| !in_array($task['profile'] ?? '', ['qwen', 'openai', 'doubao'], true)
|
||||
|| !is_string($task['user'] ?? null) || strlen($task['user']) > 128
|
||||
|| !is_int($task['generation'] ?? null) || $task['generation'] <= 0) return null;
|
||||
return ['profile' => $task['profile'], 'user' => $task['user'], 'task_id' => $task['task_id'],
|
||||
|
||||
@@ -0,0 +1,130 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\adminapi\service;
|
||||
|
||||
use app\common\cache\AdminTokenCache;
|
||||
use app\common\enum\AdminTerminalEnum;
|
||||
use app\common\model\auth\Admin;
|
||||
use app\common\service\JsonService;
|
||||
use DateTimeImmutable;
|
||||
use DateTimeZone;
|
||||
use think\facade\Db;
|
||||
|
||||
/** 医生工作站每次登录单独计时,认证记录与历史记录在同一事务内更新。 */
|
||||
final class DesktopSessionService
|
||||
{
|
||||
public const HEARTBEAT_GRACE = 180;
|
||||
|
||||
public static function nextCutoff(int $time): int
|
||||
{
|
||||
$local = (new DateTimeImmutable('@' . $time))->setTimezone(new DateTimeZone('Asia/Shanghai'));
|
||||
$cutoff = $local->setTime(5, 0);
|
||||
return ($cutoff <= $local ? $cutoff->modify('+1 day') : $cutoff)->getTimestamp();
|
||||
}
|
||||
|
||||
public static function open(int $adminId): array
|
||||
{
|
||||
return Db::transaction(static function () use ($adminId): array {
|
||||
// 即使还没有任何会话行,账号锁也能串行化单点登录的首次并发签发。
|
||||
$admin = Admin::where('id', $adminId)->lock(true)->find();
|
||||
if (!$admin || (int) $admin['disable'] !== 0) {
|
||||
JsonService::throw('账号不存在或已禁用');
|
||||
}
|
||||
if (!(int) $admin['multipoint_login']) {
|
||||
self::closeForAdmin($adminId, 'replaced');
|
||||
}
|
||||
$now = time();
|
||||
$cutoff = self::nextCutoff($now);
|
||||
$due = min($cutoff, $now + self::HEARTBEAT_GRACE);
|
||||
$slot = Db::name('desktop_login_session')->insertGetId([
|
||||
'admin_id' => $adminId, 'account' => $admin['account'], 'name' => $admin['name'],
|
||||
'login_time' => $now, 'last_seen_time' => $now, 'expires_at' => $cutoff,
|
||||
'due_time' => $due, 'login_ip' => request()->ip(),
|
||||
]);
|
||||
$token = bin2hex(random_bytes(16));
|
||||
Db::name('admin_session')->insert([
|
||||
'admin_id' => $adminId, 'terminal' => AdminTerminalEnum::WORKSTATION,
|
||||
'workstation_slot' => $slot, 'token' => $token, 'expire_time' => $due, 'update_time' => $now,
|
||||
]);
|
||||
$info = (new AdminTokenCache())->setAdminInfo($token);
|
||||
if (!$info) {
|
||||
// 签发若恰好跨过 05:00,与认证行一起回滚。
|
||||
JsonService::throw('登录已到期,请重新登录', [], -1);
|
||||
}
|
||||
return $info;
|
||||
});
|
||||
}
|
||||
|
||||
/** 在改密、停用及单点重登事务内调用。其他终端不访问工作站历史表。 */
|
||||
public static function closeForAdmin(int $adminId, string $reason): void
|
||||
{
|
||||
$tokens = Db::name('admin_session')->where('admin_id', $adminId)
|
||||
->where('terminal', AdminTerminalEnum::WORKSTATION)->order('id')->column('token');
|
||||
foreach ($tokens as $token) {
|
||||
self::update((string) $token, $reason);
|
||||
}
|
||||
}
|
||||
|
||||
/** 心跳返回固定的 05:00 截止时间;手工退出或撤销返回结束时间。过期凭据不能续活。 */
|
||||
public static function update(string $token, string $action): int
|
||||
{
|
||||
return Db::transaction(static function () use ($token, $action): int {
|
||||
$auth = Db::name('admin_session')->where('token', $token)
|
||||
->where('terminal', AdminTerminalEnum::WORKSTATION)->lock(true)->find();
|
||||
if (!$auth) {
|
||||
return 0;
|
||||
}
|
||||
$row = Db::name('desktop_login_session')->where('id', $auth['workstation_slot'])
|
||||
->whereNull('logout_time')->lock(true)->find();
|
||||
if (!$row) {
|
||||
return 0;
|
||||
}
|
||||
$now = time();
|
||||
$reason = $action;
|
||||
$end = $now;
|
||||
if ((int) $auth['expire_time'] < (int) $row['due_time'] && (int) $auth['expire_time'] <= $now) {
|
||||
// 兼容通过通用账号管理入口撤销凭据的情形。
|
||||
$reason = 'revoked';
|
||||
$end = min($now, (int) $auth['update_time']);
|
||||
} elseif ((int) $row['due_time'] <= $now) {
|
||||
$cutoffReached = (int) $row['due_time'] === (int) $row['expires_at'];
|
||||
$reason = $cutoffReached ? 'daily_cutoff' : 'timeout';
|
||||
$end = $cutoffReached ? (int) $row['expires_at'] : (int) $row['last_seen_time'];
|
||||
} elseif ($action === 'heartbeat') {
|
||||
$due = min((int) $row['expires_at'], $now + self::HEARTBEAT_GRACE);
|
||||
Db::name('desktop_login_session')->where('id', $row['id'])->update([
|
||||
'last_seen_time' => $now, 'due_time' => $due, 'duration_seconds' => $now - (int) $row['login_time'],
|
||||
]);
|
||||
Db::name('admin_session')->where('id', $auth['id'])->update(['expire_time' => $due, 'update_time' => $now]);
|
||||
return (int) $row['expires_at'];
|
||||
} elseif ($action === 'settle') {
|
||||
// 扫描与锁行之间可能已收到心跳。
|
||||
return 0;
|
||||
}
|
||||
$end = max((int) $row['login_time'], $end);
|
||||
Db::name('desktop_login_session')->where('id', $row['id'])->update([
|
||||
'logout_time' => $end, 'duration_seconds' => $end - (int) $row['login_time'], 'end_reason' => $reason,
|
||||
]);
|
||||
Db::name('admin_session')->where('id', $auth['id'])->delete();
|
||||
return $action === 'heartbeat' ? 0 : $end;
|
||||
});
|
||||
}
|
||||
|
||||
/** 定时任务及报表入口共用;到期时间取已落库的界线,不受任务迟到的影响。 */
|
||||
public static function settleDue(): int
|
||||
{
|
||||
$settled = 0;
|
||||
$now = time();
|
||||
Db::name('admin_session')->where('terminal', AdminTerminalEnum::WORKSTATION)
|
||||
->where('workstation_slot', '>', 0)->where('expire_time', '<=', $now)->field('id,token')
|
||||
->chunk(200, static function ($rows) use (&$settled) {
|
||||
foreach ($rows as $row) {
|
||||
if (self::update((string) $row['token'], 'settle')) {
|
||||
$settled++;
|
||||
}
|
||||
}
|
||||
}, 'id');
|
||||
return $settled;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user