问题:GET /whiteboard/{board_id} 同时被 REST(返回 JSON)与 main.py 的 HTML 页面
注册,FastAPI 按注册顺序匹配到 REST,导致浏览器访问拿到 JSON 而非前端页面。
改为按职责分命名空间,避免冲突:
- HTML 页面:/wb/{id}(main.py)、/wb-admin(main.py,Basic Auth)
- 公开 REST:GET /api/wb/{id}(前端 init 拉取初始笔画)
- WS:/ws/wb/{id}(实时同步 + 心跳)
- 管理 REST:GET /api/admin/wb、DELETE /api/admin/wb/{id}(Basic Auth)
前端 whiteboard.js / whiteboard_admin.js、测试脚本、README 路径同步更新。
旧 /whiteboard/* 路径不再注册(404)。
85 lines
3.2 KiB
JavaScript
85 lines
3.2 KiB
JavaScript
/* 白板管理页:拉取 /api/admin/wb、渲染表格、删除、新建并跳转。 */
|
|
(function () {
|
|
"use strict";
|
|
const { el, toast, fmtTime, api } = window.ZK;
|
|
const listEl = document.getElementById("list");
|
|
const countEl = document.getElementById("count");
|
|
const refreshBtn = document.getElementById("refresh");
|
|
const newBtn = document.getElementById("newBtn");
|
|
|
|
refreshBtn.addEventListener("click", load);
|
|
newBtn.addEventListener("click", () => {
|
|
// 生成一个随机 board_id 并打开(访问即创建)
|
|
const id = "b_" + Math.random().toString(36).slice(2, 10);
|
|
window.open(`/wb/${id}`, "_blank");
|
|
});
|
|
|
|
async function load() {
|
|
listEl.innerHTML = '<div class="skel">加载中…</div>';
|
|
countEl.textContent = "";
|
|
try {
|
|
const res = await api("/api/admin/wb?limit=500&offset=0");
|
|
if (!res.ok) throw new Error("HTTP " + res.status);
|
|
const body = await res.json();
|
|
render(body.items || []);
|
|
countEl.textContent = `共 ${body.total} 个白板`;
|
|
} catch (e) {
|
|
listEl.innerHTML = '<div class="empty">加载失败:' + (e.message || e) + "</div>";
|
|
}
|
|
}
|
|
|
|
function render(items) {
|
|
if (!items.length) {
|
|
listEl.innerHTML = '<div class="empty">还没有白板。点「新建并打开」创建一个。</div>';
|
|
return;
|
|
}
|
|
const table = el("table", { class: "wb-admin-table" });
|
|
const thead = el("thead", null,
|
|
el("tr", null,
|
|
el("th", { class: "col-id" }, "白板 ID"),
|
|
el("th", { class: "col-mods" }, "修改次数"),
|
|
el("th", { class: "col-created" }, "创建时间"),
|
|
el("th", { class: "col-updated" }, "上次修改"),
|
|
el("th", { class: "col-act" }, "操作")
|
|
)
|
|
);
|
|
const tbody = el("tbody", null);
|
|
for (const b of items) {
|
|
const row = el("tr", null,
|
|
el("td", { class: "col-id" },
|
|
el("a", { class: "bid link", href: `/wb/${b.board_id}`, target: "_blank" }, b.board_id)
|
|
),
|
|
el("td", { class: "col-mods mono" }, String(b.stroke_count ?? 0)),
|
|
el("td", { class: "col-created muted" }, fmtTime(b.created_at)),
|
|
el("td", { class: "col-updated muted" }, fmtTime(b.updated_at)),
|
|
el("td", { class: "col-act" },
|
|
el("button", { class: "btn danger", onclick: () => remove(b, row) }, "删除")
|
|
)
|
|
);
|
|
tbody.appendChild(row);
|
|
}
|
|
table.appendChild(thead);
|
|
table.appendChild(tbody);
|
|
listEl.innerHTML = "";
|
|
listEl.appendChild(table);
|
|
}
|
|
|
|
async function remove(b, row) {
|
|
if (!confirm(`确定删除白板「${b.board_id}」?\n所有在线协作者会被断开,内容不可恢复。`)) return;
|
|
try {
|
|
const res = await api(`/api/admin/wb/${encodeURIComponent(b.board_id)}`, { method: "DELETE" });
|
|
if (res.status === 404) { toast("白板已不存在"); }
|
|
else if (!res.ok) throw new Error("HTTP " + res.status);
|
|
row.classList.add("row-removed");
|
|
setTimeout(() => row.remove(), 250);
|
|
toast("已删除");
|
|
const m = (countEl.textContent || "").match(/(\d+)/);
|
|
if (m) countEl.textContent = `共 ${Math.max(0, Number(m[1]) - 1)} 个白板`;
|
|
} catch (e) {
|
|
toast("删除失败:" + (e.message || e), "err");
|
|
}
|
|
}
|
|
|
|
load();
|
|
})();
|