refactor: 接入后台页面路由

This commit is contained in:
que01 committed 2026-09-19 18:34:10 +08:00
1 parent 86da43b635
commit f0836ec056
1 file changed
+3 -2
+3 -2
View File
@@ -3,6 +3,7 @@ import { createRoot } from "react-dom/client";
import { QueryClient, QueryClientProvider, useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { App, Button, Card, Form, Input, Layout, Menu, Select, Space, Typography, message, Table, Tag, Popconfirm, Modal, Badge } from "antd";
import { ApiClient } from "@personal-brand/api-client";
import { BrowserRouter, Navigate, Route, Routes, useLocation, useNavigate } from "react-router-dom";
import type { BrandProfile, PublicService, TrustContent, TrustContentInput } from "@personal-brand/api-client";
import "antd/dist/reset.css";
@@ -64,7 +65,7 @@ function InquiriesPage() {
return <Card title="咨询记录" extra={<Space><Select allowClear placeholder="全部状态" style={{ width: 130 }} value={status} onChange={setStatus} options={bookingStatusOptions.map((value) => ({ value, label: bookingStatusLabels[value] }))} /><Badge count={query.data?.filter((item) => !(item as AdminBooking & { isRead?: boolean }).isRead).length ?? 0} showZero /></Space>}><Table rowKey="id" loading={query.isLoading} dataSource={query.data ?? []} columns={[{ title: "编号", dataIndex: "bookingNo" }, { title: "类型", render: (_: unknown, item: AdminBooking & { requestType?: string }) => item.requestType === "inquiry" ? "品牌咨询" : item.requestType === "private_chef" ? "定制服务" : "服务预约" }, { title: "联系人", dataIndex: "customerName" }, { title: "提交时间", dataIndex: "createdAt", render: (value: string) => new Date(value).toLocaleString("zh-CN") }, { title: "状态", dataIndex: "status", render: (value: string) => <Tag color={value === "cancelled" ? "default" : "processing"}>{bookingStatusLabels[value] ?? value}</Tag> }, { title: "操作", render: (_: unknown, item: AdminBooking) => <Space><Select size="small" value={item.status} onChange={(nextStatus) => update.mutate({ id: item.id, nextStatus: nextStatus as never })} options={bookingStatusOptions.map((value) => ({ value, label: bookingStatusLabels[value] }))} /><Button size="small" onClick={() => markRead.mutate(item.id)}>标记已读</Button></Space> }]} /></Card>;
}
function Dashboard({ onLogout }: { onLogout: () => void }) { const [page, setPage] = useState("brand"); return <Layout style={{ minHeight: "100vh" }}><Layout.Sider><Typography.Title level={4} style={{ color: "white", padding: 20 }}>个人品牌</Typography.Title><Menu theme="dark" selectedKeys={[page]} onClick={({ key }) => setPage(key)} items={[{ key: "brand", label: "品牌信任" }, { key: "trust", label: "信任内容" }, { key: "services", label: "服务与合作" }, { key: "inquiries", label: "咨询记录" }]} /></Layout.Sider><Layout><Layout.Header style={{ background: "white", display: "flex", justifyContent: "space-between", alignItems: "center" }}><Space>品牌管理后台</Space><Button type="text" onClick={onLogout}>退出登录</Button></Layout.Header><Layout.Content style={{ padding: 24 }}>{page === "brand" ? <BrandPage /> : page === "trust" ? <TrustContentPage /> : page === "services" ? <ServicesPage /> : <InquiriesPage />}</Layout.Content></Layout></Layout>; }
function Dashboard({ onLogout }: { onLogout: () => void }) { const navigate = useNavigate(); const location = useLocation(); const page = location.pathname.split("/")[2] || "brand"; const items = [{ key: "brand", label: "品牌信任" }, { key: "trust", label: "信任内容" }, { key: "services", label: "服务与合作" }, { key: "inquiries", label: "咨询记录" }]; return <Layout style={{ minHeight: "100vh" }}><Layout.Sider><Typography.Title level={4} style={{ color: "white", padding: 20 }}>个人品牌</Typography.Title><Menu theme="dark" selectedKeys={[page]} onClick={({ key }) => navigate(`/admin/${key}`)} items={items} /></Layout.Sider><Layout><Layout.Header style={{ background: "white", display: "flex", justifyContent: "space-between", alignItems: "center" }}><Space>品牌管理后台</Space><Button type="text" onClick={onLogout}>退出登录</Button></Layout.Header><Layout.Content style={{ padding: 24 }}><Routes><Route path="brand" element={<BrandPage />} /><Route path="trust" element={<TrustContentPage />} /><Route path="services" element={<ServicesPage />} /><Route path="inquiries" element={<InquiriesPage />} /><Route path="*" element={<Navigate to="/admin/brand" replace />} /></Routes></Layout.Content></Layout></Layout>; }
function Root() { const [loggedIn, setLoggedIn] = useState(Boolean(localStorage.getItem("adminToken"))); React.useEffect(() => { const expire = () => { message.warning("登录状态已失效,请重新登录"); setLoggedIn(false); }; window.addEventListener("admin-auth-expired", expire); return () => window.removeEventListener("admin-auth-expired", expire); }, []); const logout = () => { localStorage.removeItem("adminToken"); setLoggedIn(false); }; return loggedIn ? <Dashboard onLogout={logout} /> : <Login onLogin={() => setLoggedIn(true)} />; }
createRoot(document.getElementById("app")!).render(<React.StrictMode><QueryClientProvider client={queryClient}><App><Root /></App></QueryClientProvider></React.StrictMode>);
createRoot(document.getElementById("app")!).render(<React.StrictMode><QueryClientProvider client={queryClient}><App><BrowserRouter><Root /></BrowserRouter></App></QueryClientProvider></React.StrictMode>);