refactor: 按角色分层目录, HTML 不再平铺根目录

根目录原本平铺 7 个 HTML, 而 js/ 已按功能域分子目录, 两级结构不对称;
且自包含实验页与主功能页平级摆放, 看不出可随时删除。现按角色分三层:

  tabs.html  标签外壳留在根 = 工具入口
  pages/     index / can / can_monitor / motor  主功能页
  lab/       continuity_test / spi_test  自包含实验页(零外部依赖)
  scripts/   build_mem.bat

随迁修正(全部实跑验证):
- pages/ 下 4 页共 22 处 css/js 引用加 ../ 前缀; 页面互链同目录无需改
- js/motor/main.js 的 Worker URL 改用 import.meta.url 解析: 原文档相对
  路径 "js/motor/tick.worker.js" 在 pages/ 下会解析成 /pages/js/... 而
  404, 静默退化到 setTimeout 兜底、丢失后台不节流能力(已验证旧写法 404)
- tabs.html 的 TABS 登记新路径; pageToTab 改按文件名匹配, 旧版
  localStorage 存的裸文件名(can.html)自动迁移到 pages/can.html
- esp32_server.py: GET / 与 /index.html 302 到 /tabs.html(根下已无
  index.html); 注入脚本的 ⌂ 主页判定基准由 index.html 改为 tabs.html
- serve.bat 打印外壳与 OTA 页新地址; README 补目录结构与路径约定

验证: 39/39 静态资源 200, 7 页 200 且注入生效, 逐页解析引用 0 坏链,
路由 / 与 /index.html 302、未知页 404, node --check 与 py_compile 通过。
This commit is contained in:
cbb
2026-09-03 21:51:17 +08:00
parent d2c86c425a
commit 31b90f68b3
12 changed files with 89 additions and 50 deletions
+16 -4
View File
@@ -5,10 +5,12 @@
而所有业务 API 由 ESP32 固件的 80 端口跨域提供(js/common/base-url.js 里的
设备 IP, 存 localStorage 的 dcu_esp_ip)。因此本脚本不碰任何硬件, 只做静态服务。
GET / → index.html (OTA 控制台; 标签外壳为 tabs.html)
GET /<file> → 本目录静态文件(html/css/js)
GET / → 302 到 /tabs.html (标签外壳, 工具主页)
GET /<file> → 本目录静态文件(tabs.html / pages/ / lab/ / css/ / js/)
POST /api/shutdown → 结束本服务进程(窗口模式父进程用它通知子进程退出)
目录约定: tabs.html 在根(工具入口), 功能页在 pages/, 自包含实验页在 lab/。
启动模式:
--window pywebview 桌面窗口(esp32_app.vbs) —— GUI 进程 + 独立服务子进程
默认 浏览器标签页(esp32_web.vbs / serve.bat)
@@ -78,7 +80,10 @@ user-select:none;opacity:.55;transition:opacity .15s}
var home=document.getElementById('__home');
var p=location.pathname;
var isShell=!!document.getElementById('view');
if(!isShell&&!/\\/$/.test(p)&&!/\\/index\\.html$/.test(p)){
// 顶层且非标签外壳(浏览器直开 pages/xxx.html) → 给个 ⌂ 回到外壳。
// 判定基准是外壳自身(有 #view / 路径为 tabs.html 或 /), 不能再看 index.html:
// 重排后 pages/index.html 只是普通功能页, 直开它同样该显示 ⌂。
if(!isShell&&!/\\/$/.test(p)&&!/\\/tabs\\.html$/.test(p)){
home.style.display='block';
home.addEventListener('click',function(){location.href='__HOME__';});
}
@@ -152,7 +157,14 @@ class Handler(SimpleHTTPRequestHandler):
def do_GET(self):
path = self.path.split("?")[0].lower()
if path.endswith(".html") or path == "/" or not os.path.splitext(path)[1]:
# 根路径不再有 index.html(已移入 pages/), 直接送去标签外壳
if path in ("/", "/index.html"):
self.send_response(302)
self.send_header("Location", "/tabs.html")
self.send_header("Content-Length", "0")
self.end_headers()
return
if path.endswith(".html") or not os.path.splitext(path)[1]:
# HTML(含无扩展名的 SPA 路由): 注入刷新悬浮按钮/快捷键后返回
fp = self.translate_path(self.path)
if os.path.isdir(fp):