mobile.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
  6. <meta name="apple-mobile-web-app-capable" content="yes">
  7. <title>钢包视频监测</title>
  8. <link rel="icon" href="/logo.png" type="image/png">
  9. <style>
  10. *{margin:0;padding:0;box-sizing:border-box;-webkit-tap-highlight-color:transparent}
  11. body{font-family:system-ui,sans-serif;background:#0b1220;color:#e8e8e8;height:100vh;overflow:hidden}
  12. .hidden{display:none!important}
  13. /* 登录 */
  14. #loginPage{display:flex;flex-direction:column;justify-content:center;align-items:center;height:100vh;padding:24px;background:#0b1220}
  15. #loginPage .logo{width:64px;height:64px;margin-bottom:16px}
  16. #loginPage h1{font-size:20px;margin-bottom:24px;color:#e94560}
  17. #loginPage input{width:100%;max-width:320px;padding:12px 14px;margin-bottom:12px;border:1px solid #0f3460;background:#0e1426;color:#eee;font-size:16px;border-radius:6px}
  18. #loginPage button{width:100%;max-width:320px;padding:12px;background:#e94560;color:#fff;border:none;font-size:16px;border-radius:6px;cursor:pointer}
  19. #loginErr{color:#e94560;font-size:13px;margin-bottom:12px;min-height:18px}
  20. /* 主界面 */
  21. #appPage{display:flex;flex-direction:column;height:100vh}
  22. .topbar{display:flex;align-items:center;padding:10px 12px;background:#0e1426;border-bottom:1px solid #0f3460;flex-shrink:0}
  23. .topbar h2{font-size:16px;flex:1;color:#eee}
  24. .topbar .tab{display:flex;gap:4px}
  25. .topbar .tab button{padding:6px 12px;background:transparent;border:none;color:#8892a8;font-size:14px;cursor:pointer;border-radius:4px}
  26. .topbar .tab button.active{background:#182242;color:#e94560;font-weight:bold}
  27. /* 视频页 */
  28. #videoPage{flex:1;display:flex;flex-direction:column;overflow:hidden}
  29. #videoWrap{flex:1;position:relative;background:#000;min-height:0}
  30. #videoWrap img{width:100%;height:100%;object-fit:contain;display:block}
  31. #videoLoader{position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);color:#888}
  32. .chSwitcher{display:flex;overflow-x:auto;padding:8px;background:#0e1426;border-top:1px solid #0f3460;flex-shrink:0}
  33. .chSwitcher button{padding:8px 14px;margin-right:8px;background:#0f3460;border:none;color:#eee;border-radius:4px;font-size:13px;white-space:nowrap;cursor:pointer}
  34. .chSwitcher button.active{background:#e94560}
  35. /* 报警页 */
  36. #alarmPage{flex:1;overflow-y:auto;padding:12px}
  37. #alarmMore{text-align:center;color:#8892a8;font-size:13px;padding:16px;display:none}
  38. .filterBar{display:flex;gap:8px;margin-bottom:12px;flex-wrap:wrap}
  39. .filterBar select,.filterBar input{padding:8px;background:#0e1426;border:1px solid #0f3460;color:#eee;font-size:13px;border-radius:4px}
  40. .filterBar select{flex:1;min-width:100px}
  41. .filterBar input[type=datetime-local]{flex:1;min-width:130px}
  42. .alarmItem{display:flex;gap:10px;padding:10px;margin-bottom:10px;background:#0e1426;border:1px solid #0f3460;border-left:4px solid #e94560;border-radius:6px}
  43. .alarmItem img{width:72px;height:54px;object-fit:cover;border-radius:4px;flex-shrink:0}
  44. .alarmItem .info{flex:1;min-width:0}
  45. .alarmItem .time{font-size:11px;color:#888;margin-bottom:4px}
  46. .alarmItem .type{font-size:14px;font-weight:bold;margin-bottom:2px}
  47. .alarmItem .meta{font-size:12px;color:#aaa}
  48. .alarmItem .conf{font-size:12px;color:#27ae60}
  49. /* 详情弹窗 */
  50. .modal{position:fixed;inset:0;background:rgba(0,0,0,.9);z-index:50;display:none;flex-direction:column}
  51. .modal.show{display:flex}
  52. .modal img{flex:1;object-fit:contain;min-height:0}
  53. .modal .mbar{display:flex;align-items:center;padding:10px 14px;background:#0e1426;border-top:1px solid #0f3460}
  54. .modal .mbar .minfo{flex:1;font-size:13px}
  55. .modal .mbar button{padding:8px 16px;background:#e94560;border:none;color:#fff;border-radius:4px;font-size:14px}
  56. .toast{position:fixed;top:20px;left:50%;transform:translateX(-50%);background:rgba(233,69,96,.95);color:#fff;padding:10px 18px;border-radius:6px;font-size:14px;z-index:99;display:none}
  57. .empty-tip{text-align:center;color:#666;padding:40px 0;font-size:14px}
  58. </style>
  59. </head>
  60. <body>
  61. <div id="loginPage">
  62. <img class="logo" src="/logo.png">
  63. <h1>钢包视频监测</h1>
  64. <div id="loginErr"></div>
  65. <input type="text" id="loginUser" placeholder="用户名" value="admin">
  66. <input type="password" id="loginPass" placeholder="密码" value="admin">
  67. <button onclick="doLogin()">登 录</button>
  68. </div>
  69. <div id="appPage" class="hidden">
  70. <div class="topbar">
  71. <h2 id="pageTitle">视频监控</h2>
  72. <div class="tab">
  73. <button id="tabVideo" class="active" onclick="switchTab('video')">视频</button>
  74. <button id="tabAlarm" onclick="switchTab('alarm')">报警</button>
  75. </div>
  76. </div>
  77. <div id="videoPage">
  78. <div id="videoWrap">
  79. <img id="video" alt="">
  80. <div id="videoLoader">加载中...</div>
  81. </div>
  82. <div class="chSwitcher" id="chSwitcher"></div>
  83. </div>
  84. <div id="alarmPage" class="hidden">
  85. <div class="filterBar">
  86. <select id="fltType" onchange="loadAlarms(true)">
  87. <option value="">全部类型</option>
  88. <option value="fence">围栏报警</option>
  89. <option value="pose">姿态报警</option>
  90. </select>
  91. <select id="fltCh" onchange="loadAlarms(true)"><option value="">全部通道</option></select>
  92. <input type="datetime-local" id="fltStart">
  93. <input type="datetime-local" id="fltEnd">
  94. <button onclick="loadAlarms(true)" style="padding:8px 12px;background:#e94560;border:none;color:#fff;border-radius:4px">查询</button>
  95. </div>
  96. <div id="alarmList"></div>
  97. <div id="alarmMore">下拉加载更多</div>
  98. </div>
  99. </div>
  100. <div class="modal" id="detailModal">
  101. <img id="detailImg" alt="">
  102. <div class="mbar">
  103. <div class="minfo" id="detailInfo"></div>
  104. <button onclick="closeDetail()">关闭</button>
  105. </div>
  106. </div>
  107. <div class="toast" id="toast"></div>
  108. <script>
  109. let channels = [];
  110. let curChannel = 0;
  111. let curType = '';
  112. let alarmItems = [];
  113. let alarmPage = 0;
  114. const alarmPageSize = 20;
  115. let alarmLoading = false;
  116. let alarmHasMore = false;
  117. let videoTimer = null;
  118. let videoBusy = false;
  119. let videoUrl = '';
  120. function toast(msg) {
  121. const t = document.getElementById('toast');
  122. t.textContent = msg;
  123. t.style.display = 'block';
  124. setTimeout(() => t.style.display = 'none', 2500);
  125. }
  126. async function doLogin() {
  127. const u = document.getElementById('loginUser').value;
  128. const p = document.getElementById('loginPass').value;
  129. try {
  130. const r = await fetch('/api/login', {
  131. method: 'POST',
  132. headers: {'Content-Type': 'application/json'},
  133. body: JSON.stringify({username: u, password: p})
  134. });
  135. const d = await r.json();
  136. if (d.ok) {
  137. document.getElementById('loginPage').classList.add('hidden');
  138. document.getElementById('appPage').classList.remove('hidden');
  139. init();
  140. } else {
  141. document.getElementById('loginErr').textContent = d.error || '登录失败';
  142. }
  143. } catch (e) {
  144. document.getElementById('loginErr').textContent = '网络错误';
  145. }
  146. }
  147. async function init() {
  148. await loadChannels();
  149. renderChSwitcher();
  150. switchChannel(0);
  151. }
  152. async function loadChannels() {
  153. try {
  154. const r = await fetch('/api/status');
  155. if (r.status === 401) { location.reload(); return; }
  156. const d = await r.json();
  157. channels = d.channels || [];
  158. const sel = document.getElementById('fltCh');
  159. sel.innerHTML = '<option value="">全部通道</option>' +
  160. channels.map(c => `<option value="${c.id}">CH${c.id} ${c.name||''}</option>`).join('');
  161. } catch (e) {}
  162. }
  163. function renderChSwitcher() {
  164. const sw = document.getElementById('chSwitcher');
  165. sw.innerHTML = channels.map(c =>
  166. `<button data-ch="${c.id}" class="${c.id===curChannel?'active':''}" onclick="switchChannel(${c.id})">CH${c.id} ${c.name||''}</button>`).join('');
  167. }
  168. function switchChannel(id) {
  169. curChannel = id;
  170. renderChSwitcher();
  171. startVideoPolling(id);
  172. }
  173. // 手机浏览器对multipart MJPEG兼容性不一致, 使用单帧JPEG轮询保证兼容
  174. function startVideoPolling(id) {
  175. if (videoTimer) { clearTimeout(videoTimer); videoTimer = null; }
  176. const v = document.getElementById('video');
  177. videoUrl = '/stream/' + id;
  178. videoBusy = false;
  179. document.getElementById('videoLoader').textContent = '加载中...';
  180. document.getElementById('videoLoader').style.display = 'block';
  181. const next = () => {
  182. if (curChannel !== id) return;
  183. const img = new Image();
  184. videoBusy = true;
  185. img.onload = () => {
  186. if (curChannel === id) {
  187. v.src = img.src;
  188. document.getElementById('videoLoader').style.display = 'none';
  189. }
  190. videoBusy = false;
  191. videoTimer = setTimeout(next, 100);
  192. };
  193. img.onerror = () => {
  194. videoBusy = false;
  195. document.getElementById('videoLoader').textContent = '视频加载失败,重试中...';
  196. videoTimer = setTimeout(next, 1000);
  197. };
  198. img.src = videoUrl + '?t=' + Date.now();
  199. };
  200. next();
  201. }
  202. function switchTab(tab) {
  203. document.getElementById('tabVideo').classList.toggle('active', tab==='video');
  204. document.getElementById('tabAlarm').classList.toggle('active', tab==='alarm');
  205. document.getElementById('videoPage').classList.toggle('hidden', tab!=='video');
  206. document.getElementById('alarmPage').classList.toggle('hidden', tab!=='alarm');
  207. if (tab === 'alarm') loadAlarms(true);
  208. }
  209. function poseColor(a) {
  210. const m = {'跌倒':'#e94560','倒地':'#e94560','举手呼救':'#f39c12','异常蹲坐':'#3498db','半躺':'#9b59b6','手近嘴部':'#e67e22','疲劳瞌睡':'#8e44ad','长时间静止':'#2c3e50','疑似离岗':'#c0392b'};
  211. return m[a] || '#8892a8';
  212. }
  213. function renderAlarmPage() {
  214. const start = alarmPage * alarmPageSize;
  215. const page = alarmItems.slice(start, start + alarmPageSize);
  216. alarmPage++;
  217. alarmHasMore = start + alarmPageSize < alarmItems.length;
  218. const list = document.getElementById('alarmList');
  219. list.insertAdjacentHTML('beforeend', page.map(a => `
  220. <div class="alarmItem" style="border-left-color:${a.color}" onclick="showDetail('${a.img}','${a.ts}','${a.label}','${a.detail}',${a.conf},${a.ch})">
  221. <img src="/${a.img}" loading="lazy">
  222. <div class="info">
  223. <div class="time">${a.ts} · CH${a.ch}</div>
  224. <div class="type" style="color:${a.color}">${a.label}</div>
  225. <div class="meta">${a.detail}</div>
  226. <div class="conf">${(a.conf*100).toFixed(0)}%</div>
  227. </div>
  228. </div>`).join(''));
  229. document.getElementById('alarmMore').style.display = alarmHasMore ? 'block' : 'none';
  230. }
  231. async function loadAlarms(reset = false) {
  232. if (alarmLoading) return;
  233. if (!reset && alarmItems.length && alarmHasMore) { renderAlarmPage(); return; }
  234. alarmLoading = true;
  235. alarmItems = []; alarmPage = 0; alarmHasMore = true;
  236. document.getElementById('alarmList').innerHTML = '';
  237. const type = document.getElementById('fltType').value;
  238. const ch = document.getElementById('fltCh').value;
  239. const st = document.getElementById('fltStart').value.replace('T',' ');
  240. const en = document.getElementById('fltEnd').value.replace('T',' ');
  241. const qs = new URLSearchParams();
  242. qs.set('limit', '100');
  243. if (ch) qs.set('channel', ch);
  244. if (st) qs.set('start', st + ':00');
  245. if (en) qs.set('end', en + ':59');
  246. try {
  247. const [fr, pr] = await Promise.all([fetch('/api/alarms?' + qs), fetch('/api/pose/alarms?' + qs)]);
  248. const fd = await fr.json(), pd = await pr.json();
  249. const fence = (fd.items||[]).map(a => ({t:'围栏', ts:a.timestamp, ch:a.channel, label:a.fence_name, detail:a.class_name, conf:a.confidence, img:a.screenshot, color:'#27ae60'}));
  250. const pose = (pd.items||[]).map(a => ({t:a.action, ts:a.timestamp, ch:a.channel, label:a.action, detail:'姿态', conf:a.score, img:a.screenshot, color:poseColor(a.action)}));
  251. alarmItems = type==='fence' ? fence : type==='pose' ? pose : [...fence, ...pose];
  252. alarmItems.sort((a,b) => b.ts.localeCompare(a.ts));
  253. if (!alarmItems.length) {
  254. document.getElementById('alarmList').innerHTML = '<div class="empty-tip">暂无报警记录</div>';
  255. document.getElementById('alarmMore').style.display = 'none';
  256. } else renderAlarmPage();
  257. } catch (e) {}
  258. alarmLoading = false;
  259. }
  260. function showDetail(img, ts, label, detail, conf, ch) {
  261. document.getElementById('detailImg').src = '/' + img;
  262. document.getElementById('detailInfo').innerHTML =
  263. `<b>${label}</b> ${(conf*100).toFixed(0)}%<br>时间: ${ts}<br>通道: CH${ch}<br>详情: ${detail}`;
  264. document.getElementById('detailModal').classList.add('show');
  265. }
  266. function closeDetail() { document.getElementById('detailModal').classList.remove('show'); }
  267. document.getElementById('alarmPage').addEventListener('scroll', () => {
  268. const el = document.getElementById('alarmPage');
  269. if (el.scrollTop + el.clientHeight >= el.scrollHeight - 80 && alarmHasMore && !alarmLoading) {
  270. loadAlarms(false);
  271. }
  272. });
  273. // 每10秒刷新报警列表(回到第一页)
  274. setInterval(() => {
  275. if (!document.getElementById('alarmPage').classList.contains('hidden')) loadAlarms(true);
  276. }, 10000);
  277. </script>
  278. </body>
  279. </html>