mobile.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. #bgState{display:none;position:absolute;top:8px;left:50%;transform:translateX(-50%);background:rgba(233,69,96,.9);color:#fff;font-size:12px;padding:5px 12px;border-radius:4px}
  33. .chSwitcher{display:flex;overflow-x:auto;padding:8px;background:#0e1426;border-top:1px solid #0f3460;flex-shrink:0}
  34. .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}
  35. .chSwitcher button.active{background:#e94560}
  36. /* 报警页 */
  37. #alarmPage{flex:1;overflow-y:auto;padding:12px}
  38. #alarmMore{text-align:center;color:#8892a8;font-size:13px;padding:16px;display:none}
  39. .filterBar{display:flex;gap:8px;margin-bottom:12px;flex-wrap:wrap}
  40. .filterBar select,.filterBar input{padding:8px;background:#0e1426;border:1px solid #0f3460;color:#eee;font-size:13px;border-radius:4px}
  41. .filterBar select{flex:1;min-width:100px}
  42. .filterBar input[type=datetime-local]{flex:1;min-width:130px}
  43. .alarmItem{display:flex;gap:10px;padding:10px;margin-bottom:10px;background:#0e1426;border:1px solid #0f3460;border-left:4px solid #e94560;border-radius:6px}
  44. .alarmItem img{width:72px;height:54px;object-fit:cover;border-radius:4px;flex-shrink:0}
  45. .alarmItem .info{flex:1;min-width:0}
  46. .alarmItem .time{font-size:11px;color:#888;margin-bottom:4px}
  47. .alarmItem .type{font-size:14px;font-weight:bold;margin-bottom:2px}
  48. .alarmItem .meta{font-size:12px;color:#aaa}
  49. .alarmItem .conf{font-size:12px;color:#27ae60}
  50. /* 详情弹窗 */
  51. .modal{position:fixed;inset:0;background:rgba(0,0,0,.9);z-index:50;display:none;flex-direction:column}
  52. .modal.show{display:flex}
  53. .modal img{flex:1;object-fit:contain;min-height:0}
  54. .modal .mbar{display:flex;align-items:center;padding:10px 14px;background:#0e1426;border-top:1px solid #0f3460}
  55. .modal .mbar .minfo{flex:1;font-size:13px}
  56. .modal .mbar button{padding:8px 16px;background:#e94560;border:none;color:#fff;border-radius:4px;font-size:14px}
  57. .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}
  58. .empty-tip{text-align:center;color:#666;padding:40px 0;font-size:14px}
  59. </style>
  60. </head>
  61. <body>
  62. <div id="loginPage">
  63. <img class="logo" src="/logo.png">
  64. <h1>钢包视频监测</h1>
  65. <div id="loginErr"></div>
  66. <input type="text" id="loginUser" placeholder="用户名" value="admin">
  67. <input type="password" id="loginPass" placeholder="密码" value="admin">
  68. <button onclick="doLogin()">登 录</button>
  69. </div>
  70. <div id="appPage" class="hidden">
  71. <div class="topbar">
  72. <h2 id="pageTitle">视频监控</h2>
  73. <div class="tab">
  74. <button id="tabVideo" class="active" onclick="switchTab('video')">视频</button>
  75. <button id="tabAlarm" onclick="switchTab('alarm')">报警</button>
  76. </div>
  77. </div>
  78. <div id="videoPage">
  79. <div id="videoWrap">
  80. <img id="video" alt="">
  81. <div id="videoLoader">加载中...</div>
  82. <div id="bgState">后台运行中(视频已暂停)</div>
  83. </div>
  84. <div class="chSwitcher" id="chSwitcher"></div>
  85. </div>
  86. <div id="alarmPage" class="hidden">
  87. <div class="filterBar">
  88. <select id="fltType" onchange="loadAlarms(true)">
  89. <option value="">全部类型</option>
  90. <option value="fence">围栏报警</option>
  91. <option value="pose">姿态报警</option>
  92. </select>
  93. <select id="fltCh" onchange="loadAlarms(true)"><option value="">全部通道</option></select>
  94. <input type="datetime-local" id="fltStart">
  95. <input type="datetime-local" id="fltEnd">
  96. <button onclick="loadAlarms(true)" style="padding:8px 12px;background:#e94560;border:none;color:#fff;border-radius:4px">查询</button>
  97. </div>
  98. <div id="alarmList"></div>
  99. <div id="alarmMore">下拉加载更多</div>
  100. </div>
  101. </div>
  102. <div class="modal" id="detailModal">
  103. <img id="detailImg" alt="">
  104. <div class="mbar">
  105. <div class="minfo" id="detailInfo"></div>
  106. <button onclick="closeDetail()">关闭</button>
  107. </div>
  108. </div>
  109. <div class="toast" id="toast"></div>
  110. <script>
  111. let channels = [];
  112. let curChannel = 0;
  113. let curType = '';
  114. let alarmItems = [];
  115. let alarmPage = 0;
  116. const alarmPageSize = 20;
  117. let alarmLoading = false;
  118. let alarmHasMore = false;
  119. let videoTimer = null;
  120. let videoBusy = false;
  121. let videoUrl = '';
  122. function toast(msg) {
  123. const t = document.getElementById('toast');
  124. t.textContent = msg;
  125. t.style.display = 'block';
  126. setTimeout(() => t.style.display = 'none', 2500);
  127. }
  128. async function doLogin() {
  129. const u = document.getElementById('loginUser').value;
  130. const p = document.getElementById('loginPass').value;
  131. try {
  132. const r = await fetch('/api/login', {
  133. method: 'POST',
  134. headers: {'Content-Type': 'application/json'},
  135. body: JSON.stringify({username: u, password: p})
  136. });
  137. const d = await r.json();
  138. if (d.ok) {
  139. document.getElementById('loginPage').classList.add('hidden');
  140. document.getElementById('appPage').classList.remove('hidden');
  141. init();
  142. } else {
  143. document.getElementById('loginErr').textContent = d.error || '登录失败';
  144. }
  145. } catch (e) {
  146. document.getElementById('loginErr').textContent = '网络错误';
  147. }
  148. }
  149. async function init() {
  150. await loadChannels();
  151. renderChSwitcher();
  152. switchChannel(0);
  153. notifyNativeReady();
  154. }
  155. // 通知 Android 宿主 H5 已就绪(宿主可据此开始转发生命周期事件)
  156. function notifyNativeReady() {
  157. try {
  158. if (window.AndroidBridge && window.AndroidBridge.onWebReady) {
  159. window.AndroidBridge.onWebReady();
  160. }
  161. } catch (e) {}
  162. }
  163. async function loadChannels() {
  164. try {
  165. const r = await fetch('/api/status');
  166. if (r.status === 401) { location.reload(); return; }
  167. const d = await r.json();
  168. channels = d.channels || [];
  169. const sel = document.getElementById('fltCh');
  170. sel.innerHTML = '<option value="">全部通道</option>' +
  171. channels.map(c => `<option value="${c.id}">CH${c.id} ${c.name||''}</option>`).join('');
  172. } catch (e) {}
  173. }
  174. function renderChSwitcher() {
  175. const sw = document.getElementById('chSwitcher');
  176. sw.innerHTML = channels.map(c =>
  177. `<button data-ch="${c.id}" class="${c.id===curChannel?'active':''}" onclick="switchChannel(${c.id})">CH${c.id} ${c.name||''}</button>`).join('');
  178. }
  179. function switchChannel(id) {
  180. curChannel = id;
  181. renderChSwitcher();
  182. startVideoPolling(id);
  183. }
  184. // 手机浏览器对multipart MJPEG兼容性不一致, 使用单帧JPEG轮询保证兼容
  185. function startVideoPolling(id) {
  186. stopVideoPolling();
  187. const v = document.getElementById('video');
  188. videoUrl = '/stream/' + id;
  189. videoBusy = false;
  190. document.getElementById('videoLoader').textContent = '加载中...';
  191. document.getElementById('videoLoader').style.display = 'block';
  192. const next = () => {
  193. if (curChannel !== id) return;
  194. // 后台运行时不拉流(省流量/CPU), 回前台由 setBackground 恢复
  195. if (isBackground) { videoTimer = setTimeout(next, 500); return; }
  196. const img = new Image();
  197. videoBusy = true;
  198. img.onload = () => {
  199. if (curChannel === id) {
  200. v.src = img.src;
  201. document.getElementById('videoLoader').style.display = 'none';
  202. }
  203. videoBusy = false;
  204. videoTimer = setTimeout(next, 100);
  205. };
  206. img.onerror = () => {
  207. videoBusy = false;
  208. document.getElementById('videoLoader').textContent = '视频加载失败,重试中...';
  209. videoTimer = setTimeout(next, 1000);
  210. };
  211. img.src = videoUrl + '?t=' + Date.now();
  212. };
  213. next();
  214. }
  215. function stopVideoPolling() {
  216. if (videoTimer) { clearTimeout(videoTimer); videoTimer = null; }
  217. videoBusy = false;
  218. }
  219. /* ============ 后台运行检测 ============
  220. * 三种来源:
  221. * 1) Android WebView 原生回调 (最可靠, 宿主 onPause/onResume 时调用)
  222. * 2) Page Visibility API (浏览器/部分WebView支持)
  223. * 3) 心跳兜底: 定时器被系统挂起时, 恢复后检测时间跳变判定经过后台
  224. * ======================================= */
  225. let isBackground = false;
  226. let lastHeartbeat = Date.now();
  227. function setBackground(bg) {
  228. if (isBackground === bg) return;
  229. isBackground = bg;
  230. const badge = document.getElementById('bgState');
  231. if (badge) {
  232. badge.textContent = '后台运行中(视频已暂停)';
  233. badge.style.display = bg ? 'block' : 'none';
  234. }
  235. if (bg) {
  236. stopVideoPolling(); // 进后台: 停止拉流省流量
  237. } else if (!document.getElementById('videoPage').classList.contains('hidden')) {
  238. startVideoPolling(curChannel); // 回前台: 恢复
  239. }
  240. }
  241. // 1) 供 Android 原生调用: webView.evaluateJavascript("onAndroidLifecycle('pause')")
  242. window.onAndroidLifecycle = function(state) {
  243. setBackground(state === 'pause' || state === 'background' || state === 'stop');
  244. };
  245. window.onNativeBackground = function() { setBackground(true); };
  246. window.onNativeForeground = function() { setBackground(false); };
  247. // 2) 浏览器 Page Visibility API
  248. document.addEventListener('visibilitychange', () => {
  249. setBackground(document.hidden);
  250. });
  251. window.addEventListener('blur', () => { /* 仅失焦不一定是后台, 不处理 */ });
  252. window.addEventListener('pagehide', () => setBackground(true));
  253. window.addEventListener('pageshow', () => setBackground(false));
  254. // 3) 心跳兜底: 定时器被挂起后, 间隔异常变大说明经历过后台
  255. setInterval(() => {
  256. const now = Date.now();
  257. const gap = now - lastHeartbeat;
  258. lastHeartbeat = now;
  259. if (gap > 3000 && !document.hidden) {
  260. // 定时器被冻结超过3秒后恢复 -> 刚从后台回来
  261. setBackground(false);
  262. }
  263. }, 1000);
  264. function switchTab(tab) {
  265. document.getElementById('tabVideo').classList.toggle('active', tab==='video');
  266. document.getElementById('tabAlarm').classList.toggle('active', tab==='alarm');
  267. document.getElementById('videoPage').classList.toggle('hidden', tab!=='video');
  268. document.getElementById('alarmPage').classList.toggle('hidden', tab!=='alarm');
  269. if (tab === 'alarm') {
  270. stopVideoPolling(); // 看报警时暂停视频拉流, 省流量
  271. loadAlarms(true);
  272. } else if (!document.hidden) {
  273. startVideoPolling(curChannel); // 回到视频页恢复
  274. }
  275. }
  276. function poseColor(a) {
  277. const m = {'跌倒':'#e94560','倒地':'#e94560','举手呼救':'#f39c12','异常蹲坐':'#3498db','半躺':'#9b59b6','手近嘴部':'#e67e22','疲劳瞌睡':'#8e44ad','长时间静止':'#2c3e50','疑似离岗':'#c0392b'};
  278. return m[a] || '#8892a8';
  279. }
  280. function renderAlarmPage() {
  281. const start = alarmPage * alarmPageSize;
  282. const page = alarmItems.slice(start, start + alarmPageSize);
  283. alarmPage++;
  284. alarmHasMore = start + alarmPageSize < alarmItems.length;
  285. const list = document.getElementById('alarmList');
  286. list.insertAdjacentHTML('beforeend', page.map(a => `
  287. <div class="alarmItem" style="border-left-color:${a.color}" onclick="showDetail('${a.img}','${a.ts}','${a.label}','${a.detail}',${a.conf},${a.ch})">
  288. <img src="/${a.img}" loading="lazy">
  289. <div class="info">
  290. <div class="time">${a.ts} · CH${a.ch}</div>
  291. <div class="type" style="color:${a.color}">${a.label}</div>
  292. <div class="meta">${a.detail}</div>
  293. <div class="conf">${(a.conf*100).toFixed(0)}%</div>
  294. </div>
  295. </div>`).join(''));
  296. document.getElementById('alarmMore').style.display = alarmHasMore ? 'block' : 'none';
  297. }
  298. async function loadAlarms(reset = false) {
  299. if (alarmLoading) return;
  300. if (!reset && alarmItems.length && alarmHasMore) { renderAlarmPage(); return; }
  301. alarmLoading = true;
  302. alarmItems = []; alarmPage = 0; alarmHasMore = true;
  303. document.getElementById('alarmList').innerHTML = '';
  304. const type = document.getElementById('fltType').value;
  305. const ch = document.getElementById('fltCh').value;
  306. const st = document.getElementById('fltStart').value.replace('T',' ');
  307. const en = document.getElementById('fltEnd').value.replace('T',' ');
  308. const qs = new URLSearchParams();
  309. qs.set('limit', '100');
  310. if (ch) qs.set('channel', ch);
  311. if (st) qs.set('start', st + ':00');
  312. if (en) qs.set('end', en + ':59');
  313. try {
  314. const [fr, pr] = await Promise.all([fetch('/api/alarms?' + qs), fetch('/api/pose/alarms?' + qs)]);
  315. const fd = await fr.json(), pd = await pr.json();
  316. 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'}));
  317. 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)}));
  318. alarmItems = type==='fence' ? fence : type==='pose' ? pose : [...fence, ...pose];
  319. alarmItems.sort((a,b) => b.ts.localeCompare(a.ts));
  320. if (!alarmItems.length) {
  321. document.getElementById('alarmList').innerHTML = '<div class="empty-tip">暂无报警记录</div>';
  322. document.getElementById('alarmMore').style.display = 'none';
  323. } else renderAlarmPage();
  324. } catch (e) {}
  325. alarmLoading = false;
  326. }
  327. function showDetail(img, ts, label, detail, conf, ch) {
  328. document.getElementById('detailImg').src = '/' + img;
  329. document.getElementById('detailInfo').innerHTML =
  330. `<b>${label}</b> ${(conf*100).toFixed(0)}%<br>时间: ${ts}<br>通道: CH${ch}<br>详情: ${detail}`;
  331. document.getElementById('detailModal').classList.add('show');
  332. }
  333. function closeDetail() { document.getElementById('detailModal').classList.remove('show'); }
  334. document.getElementById('alarmPage').addEventListener('scroll', () => {
  335. const el = document.getElementById('alarmPage');
  336. if (el.scrollTop + el.clientHeight >= el.scrollHeight - 80 && alarmHasMore && !alarmLoading) {
  337. loadAlarms(false);
  338. }
  339. });
  340. // 每10秒刷新报警列表(回到第一页)
  341. setInterval(() => {
  342. if (!document.getElementById('alarmPage').classList.contains('hidden')) loadAlarms(true);
  343. }, 10000);
  344. </script>
  345. </body>
  346. </html>