diff --git a/src/pages/Admin.page.vue b/src/pages/Admin.page.vue index 584b43c..8e7e346 100644 --- a/src/pages/Admin.page.vue +++ b/src/pages/Admin.page.vue @@ -458,7 +458,14 @@ function recoMove(i: number, d: -1 | 1) { const tab = ref<'overview' | 'files' | 'usage' | 'storage' | 'reco' | 'feedback'>('overview'); +// 在线人数 30s 自动刷新(切走用量页签即停) +let usageTimer: ReturnType | null = null; + function switchTab(id: 'overview' | 'files' | 'usage' | 'storage' | 'reco' | 'feedback') { + if (usageTimer) { + clearInterval(usageTimer); + usageTimer = null; + } tab.value = id; if (id === 'overview') { loadOverview(); @@ -468,6 +475,7 @@ function switchTab(id: 'overview' | 'files' | 'usage' | 'storage' | 'reco' | 'fe } if (id === 'usage') { loadUsage(); + usageTimer = setInterval(loadUsage, 30000); } if (id === 'storage') { loadStorage(); @@ -688,6 +696,7 @@ function switchTab(id: 'overview' | 'files' | 'usage' | 'storage' | 'reco' | 'fe + ● 当前在线(未登录,停留>1分钟):{{ usage.online ?? 0 }} 人 总使用次数:{{ usage.total }}
diff --git a/src/router.ts b/src/router.ts index 49bc34d..2ef8f9f 100644 --- a/src/router.ts +++ b/src/router.ts @@ -4,7 +4,7 @@ import HomePage from './pages/Home.page.vue'; import NotFound from './pages/404.page.vue'; import { tools } from './tools'; import { config } from './config'; -import { reportToolUsage } from './tools/license/license'; +import { reportToolUsage, startOnlineHeartbeat } from './tools/license/license'; import { routes as demoRoutes } from './ui/demo/demo.routes'; const toolsRoutes = tools.map(({ path, name, component, ...config }) => ({ @@ -64,6 +64,8 @@ router.afterEach((to) => { if (to.meta?.isTool) { reportToolUsage(String(to.path)); } + // 全站在线心跳:停留满 1 分钟的未登录访客才计在线(内部 once 保护) + startOnlineHeartbeat(); }); export default router; diff --git a/src/tools/license/license.ts b/src/tools/license/license.ts index 11849fe..606f4ee 100644 --- a/src/tools/license/license.ts +++ b/src/tools/license/license.ts @@ -273,3 +273,43 @@ export function reportToolUsage(path: string): void { // 统计失败绝不影响功能 } } + +// ---- 在线统计心跳:打开满 1 分钟才开始上报;仅未登录访客被统计 ---- +const ONLINE_DELAY_MS = 60 * 1000; // 停留超过 1 分钟才计入在线(由前端延迟首发实现) +const ONLINE_BEAT_MS = 30 * 1000; // 之后每 30s 心跳一次(后台标签被浏览器节流为 60s,服务端 90s 窗口判定) +let onlineStarted = false; + +function isGuest(): boolean { + try { + return !JSON.parse(localStorage.getItem('it-tools-auth') ?? 'null'); + } + catch { + return true; + } +} + +export function startOnlineHeartbeat(): void { + if (onlineStarted) { + return; + } + onlineStarted = true; + const beat = () => { + try { + fetch('/relay/online', { + method: 'POST', + keepalive: true, + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ machine_id: getMachineId(), guest: isGuest() }), + }).catch(() => {}); + } + catch { + // 统计失败绝不影响功能 + } + }; + setTimeout(() => { + beat(); + setInterval(beat, ONLINE_BEAT_MS); + // 登录/登出立即同步:登录后该机器被移出在线统计 + window.addEventListener('it-tools-auth-changed', beat); + }, ONLINE_DELAY_MS); +}