app.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // DOM 加载完成后执行,启动入口:初始化菜单、默认页面加载
  2. document.addEventListener("DOMContentLoaded", () => {
  3. const items = document.querySelectorAll(".menu-item");
  4. function setActive(name) {
  5. items.forEach(i => {
  6. i.classList.toggle("active", i.dataset.page === name);
  7. });
  8. }
  9. function bindMenu() {
  10. items.forEach(item => {
  11. item.addEventListener("click", () => {
  12. const page = item.dataset.page;
  13. setActive(page);
  14. loadPage(page);
  15. });
  16. });
  17. }
  18. bindMenu();
  19. setActive("page1");
  20. loadPage("page1");
  21. });
  22. function logout() {
  23. disconnectLog() // 断开日志流连接, 避免在注销后, 日志流可能仍然占用PID资源
  24. window.location.href = "/api/auth/logout";
  25. }
  26. function setIMEI(imei) {
  27. const el = document.getElementById("imei-value");
  28. if (el) {
  29. el.textContent = imei;
  30. }
  31. }
  32. async function fetchIMEI() {
  33. try {
  34. const res = await fetch("/api/device/imei");
  35. if (!res.ok) {
  36. throw new Error("http error");
  37. }
  38. const imei = await res.text();
  39. if (!imei || imei.trim() === "") {
  40. throw new Error("empty imei");
  41. }
  42. setIMEI(imei.trim());
  43. return;
  44. } catch (e) {
  45. console.log("get imei failed, retrying...");
  46. setTimeout(fetchIMEI, 3000); // 3秒后继续重试
  47. }
  48. }
  49. let sysTimeTimer = null;
  50. function startSysTime() {
  51. if (sysTimeTimer !== null) {
  52. return;
  53. }
  54. function update() {
  55. const el = document.getElementById("sys-time");
  56. if (!el) {
  57. return;
  58. }
  59. const now = new Date();
  60. const year = now.getFullYear();
  61. const month = String(now.getMonth() + 1).padStart(2, "0");
  62. const day = String(now.getDate()).padStart(2, "0");
  63. const hour = String(now.getHours()).padStart(2, "0");
  64. const min = String(now.getMinutes()).padStart(2, "0");
  65. const sec = String(now.getSeconds()).padStart(2, "0");
  66. el.textContent =
  67. `${year}-${month}-${day} ${hour}:${min}:${sec}`;
  68. }
  69. update();
  70. sysTimeTimer = setInterval(update, 1000);
  71. }
  72. document.addEventListener("DOMContentLoaded", () => {
  73. startSysTime();
  74. });
  75. function reboot() {
  76. if (!confirm("确定重启设备吗?")) return;
  77. fetch("/api/system/reboot", {
  78. method: "POST"
  79. })
  80. .then(r => r.json())
  81. .then(d => {
  82. if (d.ok) {
  83. alert("设备正在重启...");
  84. setTimeout(logout, 1000);
  85. } else {
  86. alert("重启失败: " + d.error);
  87. }
  88. })
  89. .catch(e => alert("请求失败: " + e));
  90. }
  91. function loadIMEI() {
  92. fetchIMEI();
  93. }
  94. loadIMEI();
  95. window.onload = function() {
  96. const mask = document.getElementById("loading-mask");
  97. if (mask) {
  98. mask.style.display = "none";
  99. }
  100. };