diff --git a/apps/admin/src/main.tsx b/apps/admin/src/main.tsx index 0154965..7dbc36e 100644 --- a/apps/admin/src/main.tsx +++ b/apps/admin/src/main.tsx @@ -8,7 +8,15 @@ import "antd/dist/reset.css"; const apiBaseUrl = import.meta.env.VITE_API_BASE_URL ?? "http://localhost:3001"; const queryClient = new QueryClient(); -const api = () => new ApiClient({ baseUrl: apiBaseUrl, token: localStorage.getItem("adminToken") ?? undefined, fetcher: (input, init) => fetch(input, init) }); +async function authenticatedFetch(input: RequestInfo | URL, init?: RequestInit): Promise { + const response = await fetch(input, init); + if (response.status === 401) { + localStorage.removeItem("adminToken"); + window.dispatchEvent(new Event("admin-auth-expired")); + } + return response; +} +const api = () => new ApiClient({ baseUrl: apiBaseUrl, token: localStorage.getItem("adminToken") ?? undefined, fetcher: authenticatedFetch }); function Login({ onLogin }: { onLogin: () => void }) { const [form] = Form.useForm(); @@ -56,7 +64,7 @@ function InquiriesPage() { return update.mutate({ id: item.id, nextStatus: nextStatus as never })} options={bookingStatusOptions.map((value) => ({ value, label: bookingStatusLabels[value] }))} /> }]} />; } -function Dashboard() { const [page, setPage] = useState("brand"); return 个人品牌 setPage(key)} items={[{ key: "brand", label: "品牌信任" }, { key: "trust", label: "信任内容" }, { key: "services", label: "服务与合作" }, { key: "inquiries", label: "咨询记录" }]} />品牌管理后台{page === "brand" ? : page === "trust" ? : page === "services" ? : }; } +function Dashboard({ onLogout }: { onLogout: () => void }) { const [page, setPage] = useState("brand"); return 个人品牌 setPage(key)} items={[{ key: "brand", label: "品牌信任" }, { key: "trust", label: "信任内容" }, { key: "services", label: "服务与合作" }, { key: "inquiries", label: "咨询记录" }]} />品牌管理后台{page === "brand" ? : page === "trust" ? : page === "services" ? : }; } -function Root() { const [loggedIn, setLoggedIn] = useState(Boolean(localStorage.getItem("adminToken"))); return loggedIn ? : setLoggedIn(true)} />; } +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 ? : setLoggedIn(true)} />; } createRoot(document.getElementById("app")!).render();