router.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. const routes = {
  2. page1: "pages/page1.html",
  3. page2: "pages/page2.html",
  4. page3: "pages/page3.html",
  5. page4: "pages/page4.html",
  6. page5: "pages/page5.html",
  7. page6: "pages/page6.html",
  8. page7: "pages/page7.html",
  9. page8: "pages/page8.html",
  10. };
  11. let currentPage = null;
  12. let loadingPage = false;
  13. const pageHandlers = {
  14. page1: {
  15. init: () => window.initPage1?.(),
  16. exit: () => window.exitPage1?.()
  17. },
  18. page2: {
  19. init: () => window.initPage2?.(),
  20. exit: () => window.exitPage2?.()
  21. },
  22. page3: {
  23. init: () => window.initPage3?.(),
  24. exit: () => window.exitPage3?.()
  25. },
  26. page4: {
  27. init: () => window.initPage4?.(),
  28. exit: () => window.exitPage4?.()
  29. },
  30. page5: {
  31. init: () => window.initPage5?.(),
  32. exit: () => window.exitPage5?.()
  33. },
  34. page6: {
  35. init: () => window.initPage6?.(),
  36. exit: () => window.exitPage6?.()
  37. },
  38. page7: {
  39. init: () => window.initPage7?.(),
  40. exit: () => window.exitPage7?.()
  41. },
  42. page8: {
  43. init: () => window.initPage8?.(),
  44. exit: () => window.exitPage8?.()
  45. },
  46. };
  47. function clearOldScripts() {
  48. document.querySelectorAll("script[data-dynamic='1']")
  49. .forEach(s => s.remove());
  50. }
  51. function runScripts(container) {
  52. const scripts = container.querySelectorAll("script");
  53. scripts.forEach(oldScript => {
  54. const s = document.createElement("script");
  55. s.dataset.dynamic = "1";
  56. if (oldScript.src) {
  57. s.src = oldScript.src;
  58. } else {
  59. s.textContent = oldScript.textContent;
  60. }
  61. document.body.appendChild(s);
  62. });
  63. }
  64. async function loadPage(name) {
  65. const view = document.getElementById("view");
  66. const url = routes[name];
  67. if (!url) {
  68. view.innerHTML = "无法找到页面";
  69. return;
  70. }
  71. if (loadingPage) {
  72. return;
  73. }
  74. if (currentPage === name) {
  75. return;
  76. }
  77. try {
  78. loadingPage = true;
  79. if (currentPage && pageHandlers[currentPage]?.exit) {
  80. console.log("exit:", currentPage);
  81. pageHandlers[currentPage].exit();
  82. }
  83. clearOldScripts();
  84. const res = await fetch(url, {
  85. credentials: "include"
  86. });
  87. const html = await res.text();
  88. if (html.includes("login.html") || html.includes("登录")) {
  89. window.location.href = "/";
  90. return;
  91. }
  92. view.innerHTML = html;
  93. runScripts(view);
  94. currentPage = name;
  95. if (pageHandlers[name]?.init) {
  96. console.log("init:", name);
  97. pageHandlers[name].init();
  98. }
  99. } catch (e) {
  100. console.error("loadPage error:", e);
  101. view.innerHTML = "页面加载失败";
  102. } finally {
  103. loadingPage = false;
  104. }
  105. }
  106. function setText(id, value, page)
  107. {
  108. if (page && currentPage !== page) {
  109. return;
  110. }
  111. const el = document.getElementById(id);
  112. if (el) el.textContent = value ?? "--";
  113. }
  114. function setBtnStatus(id, enabled, page)
  115. {
  116. if (page && currentPage !== page) {
  117. return;
  118. }
  119. const btn = document.getElementById(id);
  120. if (btn) btn.disabled = !enabled;
  121. }