357 lines
17 KiB
HTML
357 lines
17 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="zh-CN">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<title>DHCP 多站点看板</title>
|
|
<style>
|
|
:root {
|
|
--bg: #0f172a; --card: #1e293b; --border: #334155;
|
|
--text: #e2e8f0; --muted: #94a3b8;
|
|
--green: #22c55e; --red: #ef4444; --blue: #3b82f6; --amber: #f59e0b;
|
|
}
|
|
* { box-sizing: border-box; margin: 0; padding: 0; }
|
|
body { background: var(--bg); color: var(--text); font-family: -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif; padding: 20px; }
|
|
.top { display: flex; align-items: center; gap: 16px; flex-wrap: wrap; margin-bottom: 20px; }
|
|
h1 { font-size: 22px; }
|
|
.sub { color: var(--muted); font-size: 13px; }
|
|
.sitebar { display: flex; gap: 8px; flex-wrap: wrap; }
|
|
.sitebtn { background: var(--card); border: 1px solid var(--border); color: var(--text); border-radius: 8px; padding: 8px 14px; cursor: pointer; font-size: 13px; }
|
|
.sitebtn:hover { border-color: var(--blue); }
|
|
.sitebtn.active { background: var(--blue); border-color: var(--blue); color: #fff; }
|
|
.sitebtn .cnt { margin-left: 6px; color: var(--muted); font-size: 12px; }
|
|
.sitebtn.active .cnt { color: rgba(255,255,255,.8); }
|
|
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 12px; margin-bottom: 24px; }
|
|
.stat { background: var(--card); border: 1px solid var(--border); border-radius: 10px; padding: 14px 16px; }
|
|
.stat .num { font-size: 26px; font-weight: 700; }
|
|
.stat .lbl { color: var(--muted); font-size: 12px; margin-top: 2px; }
|
|
.num.online { color: var(--green); } .num.offline { color: var(--red); } .num.total { color: var(--blue); }
|
|
.num.events { color: var(--amber); }
|
|
.card { background: var(--card); border: 1px solid var(--border); border-radius: 10px; padding: 16px; margin-bottom: 24px; }
|
|
.card h2 { font-size: 15px; margin-bottom: 12px; color: var(--muted); font-weight: 600; }
|
|
table { width: 100%; border-collapse: collapse; font-size: 13px; }
|
|
th { text-align: left; color: var(--muted); font-weight: 500; padding: 8px 10px; border-bottom: 1px solid var(--border); white-space: nowrap; }
|
|
td { padding: 8px 10px; border-bottom: 1px solid rgba(51,65,85,.4); white-space: nowrap; }
|
|
tr:hover td { background: rgba(59,130,246,.06); }
|
|
.badge { display: inline-block; padding: 2px 10px; border-radius: 999px; font-size: 12px; font-weight: 600; }
|
|
.badge.online { background: rgba(34,197,94,.15); color: var(--green); }
|
|
.badge.offline { background: rgba(239,68,68,.15); color: var(--red); }
|
|
.badge.stale { background: rgba(245,158,11,.15); color: var(--amber); }
|
|
.mac { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
|
|
.muted { color: var(--muted); }
|
|
.evt { display: flex; gap: 10px; align-items: center; padding: 7px 0; border-bottom: 1px solid rgba(51,65,85,.3); font-size: 13px; }
|
|
.evt:last-child { border-bottom: none; }
|
|
.dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
|
|
.dot.up { background: var(--green); } .dot.down { background: var(--red); }
|
|
.evt .time { color: var(--muted); white-space: nowrap; }
|
|
.evt .name { font-weight: 600; }
|
|
.evt .detail { color: var(--muted); font-size: 12px; }
|
|
.rpt { display: flex; gap: 10px; align-items: center; padding: 7px 0; border-bottom: 1px solid rgba(51,65,85,.3); font-size: 13px; flex-wrap: wrap; }
|
|
.rpt:last-child { border-bottom: none; }
|
|
.rpt .time { color: var(--muted); white-space: nowrap; }
|
|
.rpt .src { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; }
|
|
.rpt .detail { color: var(--muted); font-size: 12px; flex-basis: 100%; padding-left: 16px; }
|
|
.badge.ok { background: rgba(34,197,94,.15); color: var(--green); }
|
|
.badge.fail { background: rgba(239,68,68,.15); color: var(--red); }
|
|
/* 设备行可点击 → 单设备7日详情 */
|
|
tbody tr.clickable { cursor: pointer; }
|
|
tbody tr.selected td { background: rgba(59,130,246,.12); }
|
|
.devdetail { display: none; }
|
|
.devdetail.show { display: block; }
|
|
.devdetail .head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
|
|
.devdetail .head .mac { font-size: 14px; }
|
|
.devdetail .head .name { font-size: 16px; font-weight: 700; }
|
|
.devdetail .close { margin-left: auto; background: none; border: 1px solid var(--border); color: var(--muted); border-radius: 8px; padding: 4px 12px; cursor: pointer; font-size: 13px; }
|
|
.devdetail .close:hover { color: var(--red); border-color: var(--red); }
|
|
.dstats { display: grid; grid-template-columns: repeat(auto-fit, minmax(110px, 1fr)); gap: 10px; margin-bottom: 14px; }
|
|
.dstat { background: var(--bg); border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; }
|
|
.dstat .num { font-size: 20px; font-weight: 700; }
|
|
.dstat .lbl { color: var(--muted); font-size: 12px; margin-top: 2px; }
|
|
.dstat .num.up { color: var(--green); } .dstat .num.down { color: var(--red); }
|
|
.footer { color: var(--muted); font-size: 12px; text-align: center; padding: 10px 0 30px; }
|
|
.scroll { max-height: 460px; overflow-y: auto; }
|
|
.empty { color: var(--muted); padding: 20px 0; text-align: center; }
|
|
@media (max-width: 720px) {
|
|
body { padding: 12px 10px; }
|
|
h1 { font-size: 18px; }
|
|
.top { gap: 10px; }
|
|
/* 站点栏横向滚动 */
|
|
.sitebar { flex-wrap: nowrap; overflow-x: auto; -webkit-overflow-scrolling: touch; padding-bottom: 4px; margin: 0 -10px; padding-left: 10px; padding-right: 10px; }
|
|
.sitebtn { flex-shrink: 0; padding: 7px 12px; }
|
|
/* 统计卡片 2x2 */
|
|
.stats { grid-template-columns: repeat(2, 1fr); gap: 8px; margin-bottom: 16px; }
|
|
.stat { padding: 10px 12px; }
|
|
.stat .num { font-size: 22px; }
|
|
/* 设备表 → 卡片列表 */
|
|
.card { padding: 12px; margin-bottom: 16px; }
|
|
table { display: block; font-size: 13px; }
|
|
thead { display: none; }
|
|
tbody { display: block; }
|
|
tr { display: block; background: var(--bg); border: 1px solid var(--border); border-radius: 10px; padding: 8px 12px; margin-bottom: 8px; }
|
|
tr:hover td { background: transparent; }
|
|
td { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; padding: 5px 0; border-bottom: 1px solid rgba(51,65,85,.25); white-space: normal; }
|
|
td:last-child { border-bottom: none; }
|
|
td::before { content: attr(data-label); color: var(--muted); font-size: 12px; flex-shrink: 0; }
|
|
td:first-child { display: inline-flex; justify-content: flex-start; border-bottom: 1px solid rgba(51,65,85,.25); }
|
|
td:first-child::before { content: none; }
|
|
td .badge { font-size: 11px; padding: 2px 8px; }
|
|
/* 时间信息用紧凑小字,但值自身换行 */
|
|
td:not(:first-child) { font-size: 12.5px; }
|
|
.mac { word-break: break-all; text-align: right; }
|
|
/* 紧凑事件 */
|
|
.evt { flex-wrap: wrap; gap: 4px 8px; padding: 8px 0; font-size: 12.5px; }
|
|
.evt .time { width: 100%; font-size: 12px; }
|
|
.evt .detail { flex-basis: 100%; padding-left: 16px; }
|
|
/* 单设备详情在移动端紧凑 */
|
|
.devdetail .head { gap: 6px; }
|
|
.devdetail .head .name { font-size: 15px; }
|
|
.dstats { grid-template-columns: repeat(2, 1fr); gap: 6px; }
|
|
.dstat { padding: 8px 10px; }
|
|
.dstat .num { font-size: 17px; }
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="top">
|
|
<div>
|
|
<h1>📡 DHCP 多站点看板</h1>
|
|
<div class="sub" id="sub"></div>
|
|
</div>
|
|
<div class="sitebar" id="sites"></div>
|
|
</div>
|
|
|
|
<div class="stats">
|
|
<div class="stat"><div class="num online" id="st-online">-</div><div class="lbl">在线设备</div></div>
|
|
<div class="stat"><div class="num offline" id="st-offline">-</div><div class="lbl">离线设备</div></div>
|
|
<div class="stat"><div class="num total" id="st-total">-</div><div class="lbl">设备总数</div></div>
|
|
<div class="stat"><div class="num events" id="st-events">-</div><div class="lbl">事件总数</div></div>
|
|
</div>
|
|
|
|
<div class="card">
|
|
<h2 id="dev-title">设备列表 <span class="muted" style="font-weight:400">(点击行查看该设备 7 日上下线记录)</span></h2>
|
|
<table>
|
|
<thead><tr>
|
|
<th>状态</th><th>设备名</th><th>MAC 地址</th><th>IP 地址</th>
|
|
<th>首次发现</th><th>最近上线</th><th>最近下线</th><th>在线次数</th><th>累计在线</th>
|
|
</tr></thead>
|
|
<tbody id="devices"></tbody>
|
|
</table>
|
|
</div>
|
|
|
|
<div class="card devdetail" id="devdetail">
|
|
<div class="head">
|
|
<span class="name" id="dd-name"></span>
|
|
<span class="mac" id="dd-mac"></span>
|
|
<span id="dd-status"></span>
|
|
<button class="close" id="dd-close">✕ 关闭</button>
|
|
</div>
|
|
<div class="dstats">
|
|
<div class="dstat"><div class="num up" id="dd-up">-</div><div class="lbl">7日上线次数</div></div>
|
|
<div class="dstat"><div class="num down" id="dd-down">-</div><div class="lbl">7日下线次数</div></div>
|
|
<div class="dstat"><div class="num" id="dd-dur">-</div><div class="lbl">累计在线</div></div>
|
|
<div class="dstat"><div class="num" id="dd-ip">-</div><div class="lbl">最近IP</div></div>
|
|
</div>
|
|
<div class="scroll" id="dd-events" style="max-height:320px"></div>
|
|
</div>
|
|
|
|
<div class="card">
|
|
<h2>最近事件</h2>
|
|
<div class="scroll" id="events"></div>
|
|
</div>
|
|
|
|
<div class="card">
|
|
<h2>最近推送记录</h2>
|
|
<div class="scroll" id="reports"></div>
|
|
</div>
|
|
|
|
<div class="footer" id="footer"></div>
|
|
|
|
<script>
|
|
const $ = id => document.getElementById(id);
|
|
const fmtTime = ts => {
|
|
if (!ts) return '-';
|
|
const d = new Date(ts);
|
|
const p = n => String(n).padStart(2, '0');
|
|
return `${d.getMonth()+1}-${p(d.getDate())} ${p(d.getHours())}:${p(d.getMinutes())}:${p(d.getSeconds())}`;
|
|
};
|
|
const fmtDur = sec => {
|
|
sec = Number(sec) || 0;
|
|
const h = Math.floor(sec/3600), m = Math.floor(sec%3600/60);
|
|
return h > 0 ? `${h}小时${m}分` : `${m}分钟`;
|
|
};
|
|
const esc = s => String(s ?? '').replace(/[&<>"']/g, c => ({'&':'&','<':'<','>':'>','"':'"',"'":'''}[c]));
|
|
|
|
let currentSite = null;
|
|
let siteMap = {};
|
|
let selectedMac = null; // 当前查看详情的设备 MAC
|
|
let currentDevices = []; // 当前站点设备列表(供详情重渲染)
|
|
|
|
// 从 URL 读取 token(?token=xxx),所有 API 请求带上
|
|
const urlParams = new URLSearchParams(window.location.search);
|
|
const AUTH_TOKEN = urlParams.get('token') || '';
|
|
const api = path => {
|
|
const sep = path.includes('?') ? '&' : '?';
|
|
return AUTH_TOKEN ? path + sep + 'token=' + encodeURIComponent(AUTH_TOKEN) : path;
|
|
};
|
|
|
|
async function loadSites() {
|
|
try {
|
|
const data = await fetch(api('/api/sites')).then(r => r.json());
|
|
siteMap = {};
|
|
(data.sites || []).forEach(s => siteMap[s.site] = s);
|
|
$('sub').textContent = `数据保留 ${data.retain_days || 7} 天 · 站点离线判定: 3 分钟无上报`;
|
|
|
|
const bar = $('sites');
|
|
bar.innerHTML = '';
|
|
(data.sites || []).forEach(s => {
|
|
const btn = document.createElement('button');
|
|
btn.className = 'sitebtn' + (s.site === currentSite ? ' active' : '');
|
|
btn.innerHTML = `${esc(s.site)} <span class="cnt">${s.online_count}/${s.device_count}</span>`;
|
|
btn.onclick = () => {
|
|
if (s.site === currentSite) return;
|
|
currentSite = s.site;
|
|
selectedMac = null;
|
|
$('devdetail').classList.remove('show');
|
|
loadSite(); loadSites();
|
|
};
|
|
bar.appendChild(btn);
|
|
});
|
|
if (!currentSite && data.sites && data.sites.length) {
|
|
currentSite = data.sites[0].site;
|
|
loadSite();
|
|
}
|
|
} catch (e) {
|
|
$('sub').textContent = '加载站点失败: ' + e.message;
|
|
}
|
|
}
|
|
|
|
async function loadSite() {
|
|
if (!currentSite) {
|
|
$('devices').innerHTML = '<tr><td colspan="9" class="empty">暂无站点数据</td></tr>';
|
|
$('events').innerHTML = '<div class="empty">暂无事件</div>';
|
|
$('st-online').textContent = $('st-offline').textContent = $('st-total').textContent = $('st-events').textContent = '-';
|
|
return;
|
|
}
|
|
try {
|
|
const data = await fetch(api('/api/site?site=' + encodeURIComponent(currentSite) + '&limit=100')).then(r => r.json());
|
|
$('dev-title').innerHTML = `设备列表 · ${esc(currentSite)} <span class="muted" style="font-weight:400">(点击行查看该设备 7 日上下线记录)</span>`;
|
|
|
|
let online = 0, offline = 0;
|
|
(data.devices || []).forEach(d => { if (d.status === 'online') online++; else offline++; });
|
|
|
|
$('st-online').textContent = online;
|
|
$('st-offline').textContent = offline;
|
|
$('st-total').textContent = (data.devices || []).length;
|
|
$('st-events').textContent = (data.events || []).length;
|
|
|
|
renderDeviceRows(data.devices || []);
|
|
|
|
$('events').innerHTML = (data.events || []).map(e => {
|
|
const up = e.event === 'online';
|
|
return `<div class="evt">
|
|
<span class="dot ${up ? 'up' : 'down'}"></span>
|
|
<span class="time">${fmtTime(e.timestamp)}</span>
|
|
<span class="name">${esc(e.name || '-')}</span>
|
|
<span class="mac muted">${esc(e.mac)}</span>
|
|
<span class="mac muted">${esc(e.ip || '')}</span>
|
|
<span class="detail">${esc(e.detail || '')}</span>
|
|
</div>`;
|
|
}).join('') || '<div class="empty">暂无事件</div>';
|
|
|
|
$('reports').innerHTML = (data.reports || []).map(rp => {
|
|
const ok = rp.result === 'ok';
|
|
return `<div class="rpt">
|
|
<span class="badge ${ok ? 'ok' : 'fail'}">${ok ? '成功' : '失败'}</span>
|
|
<span class="time">${fmtTime(rp.time)}</span>
|
|
<span class="src">${esc(rp.source || '-')}</span>
|
|
${rp.clients ? `<span class="muted">${rp.clients}客户端</span>` : ''}
|
|
<span class="detail">${esc(rp.detail || '')}${rp.code ? ` (HTTP ${rp.code})` : ''}</span>
|
|
</div>`;
|
|
}).join('') || '<div class="empty">暂无推送记录</div>';
|
|
|
|
$('footer').textContent = `当前站点: ${currentSite} · 最近上报 ${fmtTime(data.last_seen)} · 页面自动刷新 · ${new Date().toLocaleString()}`;
|
|
|
|
// 若详情面板开着,随轮询一起刷新
|
|
if (selectedMac) loadDeviceDetail();
|
|
} catch (e) {
|
|
$('footer').textContent = '加载站点失败: ' + e.message;
|
|
}
|
|
}
|
|
|
|
// 渲染设备行(可点击查看详情)
|
|
function renderDeviceRows(devices) {
|
|
currentDevices = devices;
|
|
$('devices').innerHTML = devices.map(d => `
|
|
<tr class="clickable${d.mac === selectedMac ? ' selected' : ''}" data-mac="${esc(d.mac)}">
|
|
<td><span class="badge ${d.status === 'online' ? 'online' : 'offline'}">${d.status === 'online' ? '在线' : '离线'}</span></td>
|
|
<td data-label="设备名">${esc(d.name || '-')}</td>
|
|
<td data-label="MAC">${esc(d.mac)}</td>
|
|
<td data-label="IP">${esc(d.last_ip || '-')}</td>
|
|
<td data-label="首次发现">${fmtTime(d.first_seen)}</td>
|
|
<td data-label="最近上线">${fmtTime(d.last_online)}</td>
|
|
<td data-label="最近下线">${fmtTime(d.last_offline)}</td>
|
|
<td data-label="在线次数">${d.online_count ?? 0}</td>
|
|
<td data-label="累计在线">${fmtDur(d.total_online_seconds)}</td>
|
|
</tr>`).join('') || '<tr><td colspan="9" class="empty">暂无设备</td></tr>';
|
|
|
|
// 点击设备行 → 查看该设备7日历史
|
|
$('devices').querySelectorAll('tr.clickable').forEach(tr => {
|
|
tr.onclick = () => {
|
|
selectedMac = tr.dataset.mac;
|
|
renderDeviceRows(devices);
|
|
loadDeviceDetail();
|
|
};
|
|
});
|
|
}
|
|
|
|
// 关闭单设备详情
|
|
function closeDeviceDetail() {
|
|
selectedMac = null;
|
|
$('devdetail').classList.remove('show');
|
|
renderDeviceRows(currentDevices);
|
|
}
|
|
|
|
// 加载并展示单设备 7 日上下线历史
|
|
async function loadDeviceDetail() {
|
|
if (!currentSite || !selectedMac) return;
|
|
const panel = $('devdetail');
|
|
panel.classList.add('show');
|
|
$('dd-events').innerHTML = '<div class="empty">加载中…</div>';
|
|
$('dd-up').textContent = $('dd-down').textContent = $('dd-dur').textContent = '-';
|
|
try {
|
|
const data = await fetch(api(`/api/device?site=${encodeURIComponent(currentSite)}&mac=${encodeURIComponent(selectedMac)}`)).then(r => r.json());
|
|
const d = data.device || {};
|
|
$('dd-name').textContent = d.name || '(未命名)';
|
|
$('dd-mac').textContent = d.mac;
|
|
$('dd-status').innerHTML = `<span class="badge ${d.status === 'online' ? 'online' : 'offline'}">${d.status === 'online' ? '在线' : '离线'}</span>`;
|
|
$('dd-ip').textContent = d.last_ip || '-';
|
|
$('dd-dur').textContent = fmtDur(d.total_online_seconds);
|
|
|
|
const evs = data.events || [];
|
|
$('dd-up').textContent = evs.filter(e => e.event === 'online').length;
|
|
$('dd-down').textContent = evs.filter(e => e.event === 'offline').length;
|
|
|
|
$('dd-events').innerHTML = evs.map(e => {
|
|
const up = e.event === 'online';
|
|
return `<div class="evt">
|
|
<span class="dot ${up ? 'up' : 'down'}"></span>
|
|
<span class="time">${fmtTime(e.timestamp)}</span>
|
|
<span class="name">${esc(e.name || '-')}</span>
|
|
<span class="mac muted">${esc(e.ip || '')}</span>
|
|
<span class="detail">${esc(e.detail || '')}</span>
|
|
</div>`;
|
|
}).join('') || '<div class="empty">该设备 7 日内暂无上下线事件</div>';
|
|
} catch (e) {
|
|
$('dd-events').innerHTML = '<div class="empty">加载失败: ' + esc(e.message) + '</div>';
|
|
}
|
|
}
|
|
|
|
// 关闭按钮
|
|
$('dd-close').onclick = closeDeviceDetail;
|
|
|
|
loadSites();
|
|
loadSite();
|
|
setInterval(() => { loadSites(); loadSite(); }, 5000);
|
|
</script>
|
|
</body>
|
|
</html> |