app.js 2.5 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. window.location.href = "/api/auth/logout";
  24. }
  25. function setIMEI(imei) {
  26. const el = document.getElementById("imei-value");
  27. if (el) {
  28. el.textContent = imei;
  29. }
  30. }
  31. async function fetchIMEI() {
  32. try {
  33. const res = await fetch("/api/device/imei");
  34. if (!res.ok) {
  35. throw new Error("http error");
  36. }
  37. const imei = await res.text();
  38. if (!imei || imei.trim() === "") {
  39. throw new Error("empty imei");
  40. }
  41. setIMEI(imei.trim());
  42. return;
  43. } catch (e) {
  44. console.log("get imei failed, retrying...");
  45. setTimeout(fetchIMEI, 3000); // 3秒后继续重试
  46. }
  47. }
  48. function setText(id, value)
  49. {
  50. const el = document.getElementById(id);
  51. if (el) {
  52. el.textContent = value ?? "--";
  53. }
  54. }
  55. let sysTimeTimer = null;
  56. function startSysTime() {
  57. if (sysTimeTimer !== null) {
  58. return;
  59. }
  60. function update() {
  61. const el = document.getElementById("sys-time");
  62. if (!el) {
  63. return;
  64. }
  65. const now = new Date();
  66. const year = now.getFullYear();
  67. const month = String(now.getMonth() + 1).padStart(2, "0");
  68. const day = String(now.getDate()).padStart(2, "0");
  69. const hour = String(now.getHours()).padStart(2, "0");
  70. const min = String(now.getMinutes()).padStart(2, "0");
  71. const sec = String(now.getSeconds()).padStart(2, "0");
  72. el.textContent =
  73. `${year}-${month}-${day} ${hour}:${min}:${sec}`;
  74. }
  75. update();
  76. sysTimeTimer = setInterval(update, 1000);
  77. }
  78. document.addEventListener("DOMContentLoaded", () => {
  79. startSysTime();
  80. });
  81. function reboot() {
  82. if (!confirm("确定重启设备吗?")) return;
  83. fetch("/api/system/reboot", {
  84. method: "POST"
  85. })
  86. .then(r => r.json())
  87. .then(d => {
  88. if (d.ok) {
  89. alert("设备正在重启...");
  90. setTimeout(logout, 1000);
  91. } else {
  92. alert("重启失败: " + d.error);
  93. }
  94. })
  95. .catch(e => alert("请求失败: " + e));
  96. }
  97. function loadIMEI() {
  98. fetchIMEI();
  99. }
  100. loadIMEI();