Files
dhcp-mon/web/dashboard.html
T
Sakurasan e76d6984e0 ESP8266无睡眠轮询版 + 服务端推送记录
- 固件: 去掉 deepSleep(D0->RST 跳线依赖), 上电常驻每60秒轮询抓取上报;
  WiFi 掉线自动重连(30s超时), 失败也POST占位body让服务器记录fail
- server: /api/raw 成功/失败均记 ReportRec(时间/来源/结果/客户端数/HTTP码),
  持久化 reports.csv, /api/site 返回最近50条
- 看板: 新增"最近推送记录"面板(成功/失败徽标), 移动端兼容
- secrets.h.example 同步轮询版配置说明
2026-10-14 02:25:02 +08:00

249 lines
12 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); }
.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; }
}
</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">设备列表</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">
<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 => ({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[c]));
let currentSite = null;
let siteMap = {};
// 从 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 = () => { currentSite = s.site; 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').textContent = `设备列表 · ${esc(currentSite)}`;
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;
$('devices').innerHTML = (data.devices || []).map(d => `
<tr>
<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>';
$('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()}`;
} catch (e) {
$('footer').textContent = '加载站点失败: ' + e.message;
}
}
loadSites();
loadSite();
setInterval(() => { loadSites(); loadSite(); }, 5000);
</script>
</body>
</html>