refactor: 清理旧后台入口并优化页面分包

This commit is contained in:
que01 committed 2026-09-19 19:02:25 +08:00
1 parent 8ea80f8c80
commit 893a547967
3 files changed
+28 -145

No files matched your search

-23
View File
@@ -1,23 +0,0 @@
import { bootstrapAdmin } from "./main.js";
import { createApiClient } from "./lib/api.js";
const root = document.querySelector<HTMLElement>("#app");
if (root) {
const token = localStorage.getItem("adminToken");
if (!token) {
root.innerHTML = `<h1>品牌后台</h1><form id="login"><input id="username" value="admin" placeholder="账号"><input id="password" type="password" placeholder="密码"><button>登录</button></form>`;
root.querySelector("#login")?.addEventListener("submit", async (event) => {
event.preventDefault();
const username = (root.querySelector("#username") as HTMLInputElement).value;
const password = (root.querySelector("#password") as HTMLInputElement).value;
try {
const client = createApiClient();
const result = await client.loginAdmin(username, password);
localStorage.setItem("adminToken", result.token);
location.reload();
} catch (error) {
alert(error instanceof Error ? error.message : "登录失败");
}
});
} else bootstrapAdmin(root);
}
-110
View File
@@ -1,110 +0,0 @@
import { ApiClient, type AdminBooking, type BookingStatus } from "@personal-brand/api-client";
export interface AdminDashboardOptions {
client: ApiClient;
root: HTMLElement;
}
export class AdminDashboard {
private bookings: AdminBooking[] = [];
private selected: AdminBooking | undefined;
constructor(private readonly options: AdminDashboardOptions) {}
async load(): Promise<void> {
const { root, client } = this.options;
const [bookings, unread, brand, services] = await Promise.all([
client.listAdminBookings(),
client.getAdminUnreadCount(),
client.getAdminBrand(),
client.listServices(),
]);
this.bookings = bookings;
root.innerHTML = `<h1>品牌后台</h1><p>未读订单:${unread.count}</p><section><h2>品牌配置</h2><label>行业模板 <select id="brand-template"><option value="food" ${brand.templateKey === "food" ? "selected" : ""}>餐饮</option><option value="personal" ${brand.templateKey === "personal" ? "selected" : ""}>个人品牌</option></select></label><label>品牌名 <input id="brand-name" value="${brand.name}"></label><label>品牌简介 <textarea id="brand-description">${brand.description ?? ""}</textarea></label><label>联系电话 <input id="brand-phone" value="${brand.contactPhone ?? ""}"></label><label>微信号 <input id="brand-wechat" value="${brand.contactWechat ?? ""}"></label><button id="save-brand">保存品牌配置</button></section><section><h2>服务配置</h2><ul id="services">${services.map((item) => `<li><input data-service-name="${item.id}" value="${item.name}"><input data-service-price="${item.id}" value="${item.priceText ?? ""}"><button data-service-save="${item.id}">保存</button></li>`).join("")}</ul></section><label>状态 <select id="status"><option value="">全部</option><option>received</option><option>contacting</option><option>confirmed</option><option>completed</option><option>cancelled</option></select></label><button id="refresh">刷新</button><ul id="bookings">${this.listHtml()}</ul><section id="detail">选择订单查看详情</section>`;
root.querySelector("#refresh")?.addEventListener("click", () => void this.load());
root.querySelector("#status")?.addEventListener("change", () => void this.filter());
root.querySelector("#save-brand")?.addEventListener("click", () => void this.saveBrand());
root
.querySelectorAll<HTMLElement>("[data-service-save]")
.forEach((item) =>
item.addEventListener("click", () => void this.saveService(item.dataset.serviceSave)),
);
root
.querySelectorAll<HTMLElement>("[data-id]")
.forEach((item) => item.addEventListener("click", () => this.select(item.dataset.id)));
}
private listHtml(): string {
return (
this.bookings
.map(
(item) =>
`<li><button data-id="${item.id}">${item.bookingNo} · ${item.customerName} · ${item.status}</button></li>`,
)
.join("") || "<li>暂无订单</li>"
);
}
private async filter(): Promise<void> {
const status = (this.options.root.querySelector("#status") as HTMLSelectElement).value as
BookingStatus | "";
this.bookings = await this.options.client.listAdminBookings(status ? { status } : {});
const list = this.options.root.querySelector("#bookings");
if (list) list.innerHTML = this.listHtml();
}
private select(id: string | undefined): void {
this.selected = this.bookings.find((item) => item.id === id);
if (!this.selected) return;
const detail = this.options.root.querySelector("#detail");
if (!detail) return;
detail.innerHTML = `<h2>${this.selected.bookingNo}</h2><p>客户:${this.selected.customerName}</p><select id="detail-status"><option>received</option><option>contacting</option><option>confirmed</option><option>completed</option><option>cancelled</option></select><textarea id="note" placeholder="内部备注">${this.selected.adminNote ?? ""}</textarea><button id="save">保存</button>`;
detail.querySelector("#save")?.addEventListener("click", () => void this.save());
}
private async save(): Promise<void> {
if (!this.selected) return;
const detail = this.options.root.querySelector("#detail")!;
const status = (detail.querySelector("#detail-status") as HTMLSelectElement)
.value as BookingStatus;
const note = (detail.querySelector("#note") as HTMLTextAreaElement).value;
this.selected = await this.options.client.updateAdminBookingStatus(
this.selected.id,
status,
note,
);
await this.load();
}
private async saveBrand(): Promise<void> {
const root = this.options.root;
await this.options.client.updateAdminBrand({
templateKey: (root.querySelector("#brand-template") as HTMLSelectElement).value as
"food" | "personal",
name: (root.querySelector("#brand-name") as HTMLInputElement).value,
description: (root.querySelector("#brand-description") as HTMLTextAreaElement).value,
contactPhone: (root.querySelector("#brand-phone") as HTMLInputElement).value,
contactWechat: (root.querySelector("#brand-wechat") as HTMLInputElement).value,
});
}
private async saveService(id: string | undefined): Promise<void> {
if (!id) return;
const root = this.options.root;
await this.options.client.updateAdminService(id, {
name: (root.querySelector(`[data-service-name="${id}"]`) as HTMLInputElement).value,
priceText: (root.querySelector(`[data-service-price="${id}"]`) as HTMLInputElement).value,
});
}
}
export const adminAppName = "personal-brand-admin";
export function bootstrapAdmin(
root: HTMLElement,
baseUrl = "http://localhost:3001",
): AdminDashboard {
const token = localStorage.getItem("adminToken");
const client = new ApiClient({
baseUrl,
fetcher: (input, init) => fetch(input, init),
...(token ? { token } : {}),
});
const dashboard = new AdminDashboard({ client, root });
void dashboard.load();
return dashboard;
}
+28 -12
View File
@@ -1,4 +1,4 @@
import React, { useState } from "react";
import React, { Suspense, useState } from "react";
import { createRoot } from "react-dom/client";
import {
QueryClient,
@@ -26,10 +26,24 @@ import {
Badge,
} from "antd";
import { createApiClient } from "./lib/api.js";
import { InquiriesPage as FeatureInquiriesPage } from "./features/inquiries/InquiriesPage.js";
import { TrustContentPage as FeatureTrustContentPage } from "./features/trust-content/TrustContentPage.js";
import { ServicesPage as FeatureServicesPage } from "./features/services/ServicesPage.js";
import { BrandPage as FeatureBrandPage } from "./features/brand/BrandPage.js";
const FeatureInquiriesPage = React.lazy(() =>
import("./features/inquiries/InquiriesPage.js").then((module) => ({
default: module.InquiriesPage,
})),
);
const FeatureTrustContentPage = React.lazy(() =>
import("./features/trust-content/TrustContentPage.js").then((module) => ({
default: module.TrustContentPage,
})),
);
const FeatureServicesPage = React.lazy(() =>
import("./features/services/ServicesPage.js").then((module) => ({
default: module.ServicesPage,
})),
);
const FeatureBrandPage = React.lazy(() =>
import("./features/brand/BrandPage.js").then((module) => ({ default: module.BrandPage })),
);
import { BrowserRouter, Navigate, Route, Routes, useLocation, useNavigate } from "react-router-dom";
import type {
BrandProfile,
@@ -112,13 +126,15 @@ function Dashboard({ onLogout }: { onLogout: () => void }) {
</Button>
</Layout.Header>
<Layout.Content style={{ padding: 24 }}>
<Routes>
<Route path="brand" element={<FeatureBrandPage />} />
<Route path="trust" element={<FeatureTrustContentPage />} />
<Route path="services" element={<FeatureServicesPage />} />
<Route path="inquiries" element={<FeatureInquiriesPage />} />
<Route path="*" element={<Navigate to="/admin/brand" replace />} />
</Routes>
<Suspense fallback={<Card loading />}>
<Routes>
<Route path="brand" element={<FeatureBrandPage />} />
<Route path="trust" element={<FeatureTrustContentPage />} />
<Route path="services" element={<FeatureServicesPage />} />
<Route path="inquiries" element={<FeatureInquiriesPage />} />
<Route path="*" element={<Navigate to="/admin/brand" replace />} />
</Routes>
</Suspense>
</Layout.Content>
</Layout>
</Layout>