/* * @Author: HenryDu8133 813367384@qq.com * @Date: 2026-06-19 17:30:23 * @LastEditors: HenryDu8133 813367384@qq.com * @LastEditTime: 2026-06-19 17:35:05 * @FilePath: \TicketMachine\web\public-status.js * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE */ (() => { const STATUS_CLASSES = ['is-checking', 'is-online', 'is-offline']; const ENDPOINTS = [ '/api/public/config', '/api/public/popular', '/api/assets/manifest' ]; const roots = () => Array.from(document.querySelectorAll('[data-server-status-root]')); const setStatus = (state, text) => { roots().forEach((root) => { root.classList.remove(...STATUS_CLASSES); root.classList.add(`is-${state}`); const value = root.querySelector('[data-server-status-value]'); if (value) value.textContent = text; }); }; const probe = async () => { for (const endpoint of ENDPOINTS) { const separator = endpoint.includes('?') ? '&' : '?'; const controller = new AbortController(); const timeoutId = window.setTimeout(() => controller.abort(), 3500); try { const response = await fetch(`${endpoint}${separator}_=${Date.now()}`, { cache: 'no-store', signal: controller.signal }); if (response.ok) { window.clearTimeout(timeoutId); return true; } } catch (error) { void error; } window.clearTimeout(timeoutId); } return false; }; const refresh = async () => { if (!roots().length) return; if (navigator.onLine === false) { setStatus('offline', '网络离线'); return; } setStatus('checking', '检测中'); const online = await probe(); setStatus(online ? 'online' : 'offline', online ? '连接正常' : '连接异常'); }; let timerId = null; const init = () => { if (!roots().length || timerId !== null) return; refresh(); timerId = window.setInterval(refresh, 30000); window.addEventListener('online', refresh); window.addEventListener('offline', () => setStatus('offline', '网络离线')); document.addEventListener('visibilitychange', () => { if (!document.hidden) refresh(); }); }; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', init, { once: true }); } else { init(); } })();