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; } }