feat(online): 停留>1min未登录访客计在线(前端60s首发+30s心跳,relay90s内存窗口,Admin usage页30s刷新展示)

This commit is contained in:
jinghui committed 2026-11-12 12:52:47 +08:00
1 parent cd4b324740
commit c6341ea35b
3 files changed
+52 -1

No files matched your search

+9
View File
@@ -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<typeof setInterval> | 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
<option :value="30">近 30 天</option>
<option :value="90">近 90 天</option>
</select>
<span v-if="usage" class="adm-sub" style="color:#16a34a;font-weight:600">● 当前在线(未登录,停留&gt;1分钟):{{ usage.online ?? 0 }} 人</span>
<span v-if="usage" class="adm-sub">总使用次数:{{ usage.total }}<template v-if="usage.guest?.count">(其中未登录 {{ usage.guest.count }} 次)</template></span>
</div>
<div class="adm-usage-grid">
+3 -1
View File
@@ -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;
+40
View File
@@ -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);
}