import React, { Suspense, useState } from "react";
import { createRoot } from "react-dom/client";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { useQuery } from "@tanstack/react-query";
import {
App,
Button,
Card,
Form,
Input,
Layout,
Menu,
Select,
Space,
Tag,
Typography,
message,
} from "antd";
import { createApiClient } from "./lib/api.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 })),
);
const FeatureWorkspacePage = React.lazy(() =>
import("./features/workspace/WorkspacePage.js").then((module) => ({
default: module.WorkspacePage,
})),
);
const FeatureOverviewPage = React.lazy(() =>
import("./features/overview/OverviewPage.js").then((module) => ({
default: module.OverviewPage,
})),
);
const FeatureCapabilitiesPage = React.lazy(() =>
import("./features/capabilities/CapabilitiesPage.js").then((module) => ({
default: module.CapabilitiesPage,
})),
);
const FeatureMembersPage = React.lazy(() =>
import("./features/team/MembersPage.js").then((module) => ({
default: module.MembersPage,
})),
);
const FeaturePublicationPage = React.lazy(() =>
import("./features/publication/PublicationPage.js").then((module) => ({
default: module.PublicationPage,
})),
);
import { BrowserRouter, Navigate, Route, Routes, useLocation, useNavigate } from "react-router-dom";
import "antd/dist/reset.css";
const queryClient = new QueryClient();
const api = createApiClient;
function Login({
onLogin,
}: {
onLogin: (session: { role: string; mustChangePassword: boolean }) => void;
}) {
const [form] = Form.useForm();
const [loading, setLoading] = useState(false);
async function submit(values: { username: string; password: string }) {
setLoading(true);
try {
const result = await api().loginAdmin(values.username, values.password);
localStorage.setItem("adminToken", result.token);
localStorage.setItem("adminRole", result.role);
localStorage.setItem("adminMustChangePassword", String(result.mustChangePassword));
onLogin(result);
} catch (error) {
message.error(error instanceof Error ? error.message : "登录失败");
} finally {
setLoading(false);
}
}
return (
);
}
function ChangePassword({ onComplete }: { onComplete: () => void }) {
const [form] = Form.useForm<{
currentPassword: string;
newPassword: string;
confirmPassword: string;
}>();
const [loading, setLoading] = useState(false);
async function submit(values: {
currentPassword: string;
newPassword: string;
confirmPassword: string;
}) {
if (values.newPassword !== values.confirmPassword) {
message.error("两次输入的新密码不一致");
return;
}
setLoading(true);
try {
await api().changeAdminPassword(values.currentPassword, values.newPassword);
localStorage.setItem("adminMustChangePassword", "false");
message.success("密码已更新");
onComplete();
} catch (error) {
message.error(error instanceof Error ? error.message : "密码更新失败");
} finally {
setLoading(false);
}
}
return (
请先设置只有你本人知道的新密码。
);
}
function Dashboard({ onLogout, role }: { onLogout: () => void; role: string }) {
const navigate = useNavigate();
const location = useLocation();
const brand = useQuery({
queryKey: ["admin-shell-brand"],
queryFn: () => createApiClient().getAdminBrand(),
});
const memberships = useQuery({
queryKey: ["admin-brand-memberships"],
queryFn: () => createApiClient().listAdminBrands(),
});
const switchBrand = async (brandId: string) => {
try {
const result = await createApiClient().switchAdminBrand(brandId);
localStorage.setItem("adminToken", result.token);
window.location.reload();
} catch (error) {
message.error(error instanceof Error ? error.message : "品牌切换失败");
}
};
const page = location.pathname.split("/")[2] || "overview";
const items = [
{
type: "group" as const,
label: "日常经营",
children: [
{ key: "overview", label: "当前品牌概览" },
{ key: "publication", label: "配置与发布" },
{ key: "brand", label: "品牌表达" },
{ key: "trust", label: "信任内容" },
{ key: "services", label: "业务提供项" },
{ key: "inquiries", label: "咨询记录" },
],
},
...(role === "editor"
? []
: [
{
type: "group" as const,
label: "系统管理",
children: [
{ key: "workspace", label: "模板与表单设置" },
{ key: "capabilities", label: "能力插件" },
{ key: "members", label: "团队成员" },
],
},
]),
];
return (
当前品牌工作区
{brand.data?.name || "当前品牌"}
品牌工作区
{role === "owner" ? "负责人" : "内容编辑"}
}>
} />
}
/>
} />
{role === "editor" ? null : (
} />
)}
{role === "editor" ? null : (
} />
)}
{role === "editor" ? null : (
} />
)}
} />
} />
} />
} />
);
}
function Root() {
const [loggedIn, setLoggedIn] = useState(Boolean(localStorage.getItem("adminToken")));
const [role, setRole] = useState(localStorage.getItem("adminRole") || "owner");
const [mustChangePassword, setMustChangePassword] = useState(
localStorage.getItem("adminMustChangePassword") === "true",
);
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");
localStorage.removeItem("adminRole");
localStorage.removeItem("adminMustChangePassword");
setLoggedIn(false);
};
if (!loggedIn)
return (
{
setRole(session.role);
setMustChangePassword(session.mustChangePassword);
setLoggedIn(true);
}}
/>
);
if (mustChangePassword) return setMustChangePassword(false)} />;
return ;
}
createRoot(document.getElementById("app")!).render(
,
);