refactor: 拆分管理端入口并优化构建分包
This commit is contained in:
1 parent
0f6ad8b993
commit
bf4bbf81c7
17 files changed
+455
-353
No files matched your search
@@ -0,0 +1,155 @@
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import {
|
||||
AppstoreOutlined,
|
||||
BarsOutlined,
|
||||
CloudUploadOutlined,
|
||||
FormOutlined,
|
||||
MessageOutlined,
|
||||
MobileOutlined,
|
||||
SafetyCertificateOutlined,
|
||||
TeamOutlined,
|
||||
ThunderboltOutlined,
|
||||
} from "@ant-design/icons";
|
||||
import { useLocation, useNavigate } from "react-router-dom";
|
||||
import { createApiClient } from "../lib/api.js";
|
||||
import { BrandMark } from "../components/BrandMark.js";
|
||||
import { StatusTag } from "../components/StatusTag.js";
|
||||
import {
|
||||
FeatureBrandPage,
|
||||
FeatureCapabilitiesPage,
|
||||
FeatureInquiriesPage,
|
||||
FeatureMembersPage,
|
||||
FeatureOverviewPage,
|
||||
FeaturePublicationPage,
|
||||
FeatureServicesPage,
|
||||
FeatureTrustContentPage,
|
||||
FeatureWorkspacePage,
|
||||
} from "./routes.js";
|
||||
import { LoadingStack } from "../components/LoadingStack.js";
|
||||
import { AdminMenu } from "../components/AdminMenu.js";
|
||||
import { adminMessage as message } from "../components/AdminMessage.js";
|
||||
import { AdminButton } from "../components/AdminButton.js";
|
||||
import { AdminSelect } from "../components/AdminSelect.js";
|
||||
import { AdminLayout } from "../components/AdminLayout.js";
|
||||
import { Navigate, Route, Routes } from "react-router-dom";
|
||||
import { Suspense } from "react";
|
||||
|
||||
export 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: "当前品牌概览", icon: <AppstoreOutlined /> },
|
||||
{ key: "publication", label: "配置与发布", icon: <CloudUploadOutlined /> },
|
||||
{ key: "brand", label: "小程序页面", icon: <MobileOutlined /> },
|
||||
{ key: "trust", label: "信任内容", icon: <SafetyCertificateOutlined /> },
|
||||
{ key: "services", label: "业务提供项", icon: <BarsOutlined /> },
|
||||
{ key: "inquiries", label: "咨询记录", icon: <MessageOutlined /> },
|
||||
{ key: "workspace", label: "咨询表单与字段", icon: <FormOutlined /> },
|
||||
],
|
||||
},
|
||||
...(role === "editor"
|
||||
? []
|
||||
: [
|
||||
{
|
||||
type: "group" as const,
|
||||
label: "系统管理",
|
||||
children: [
|
||||
{ key: "capabilities", label: "能力插件", icon: <ThunderboltOutlined /> },
|
||||
{ key: "members", label: "团队成员", icon: <TeamOutlined /> },
|
||||
],
|
||||
},
|
||||
]),
|
||||
];
|
||||
return (
|
||||
<AdminLayout className="admin-shell">
|
||||
<AdminLayout.Sider className="admin-sider" breakpoint="lg" collapsedWidth={0} width={232}>
|
||||
<div className="admin-sider-brand">
|
||||
<BrandMark name={brand.data?.name} />
|
||||
<span className="admin-sider-brand-name">品牌工作区</span>
|
||||
</div>
|
||||
<AdminMenu
|
||||
selectedKeys={[page]}
|
||||
onClick={({ key }) => navigate(`/admin/${key}`)}
|
||||
items={items}
|
||||
/>
|
||||
</AdminLayout.Sider>
|
||||
<AdminLayout className="admin-main">
|
||||
<AdminLayout.Header className="admin-header">
|
||||
<div className="admin-header-left">
|
||||
<span className="admin-header-brand">{brand.data?.name || "当前品牌"}</span>
|
||||
<span className="admin-header-context">品牌工作区</span>
|
||||
<StatusTag
|
||||
tone={role === "owner" ? "info" : "neutral"}
|
||||
label={role === "owner" ? "负责人" : "内容编辑"}
|
||||
/>
|
||||
<AdminSelect
|
||||
size="small"
|
||||
variant="outlined"
|
||||
className="admin-brand-switcher"
|
||||
value={brand.data?.id}
|
||||
loading={memberships.isLoading}
|
||||
onChange={(value) => {
|
||||
if (value) void switchBrand(value);
|
||||
}}
|
||||
options={(memberships.data ?? []).map((item) => ({
|
||||
value: item.brand.id,
|
||||
label: item.brand.name,
|
||||
}))}
|
||||
/>
|
||||
</div>
|
||||
<AdminButton type="text" onClick={onLogout}>
|
||||
退出登录
|
||||
</AdminButton>
|
||||
</AdminLayout.Header>
|
||||
<AdminLayout.Content className="admin-content">
|
||||
<div className="admin-content-inner">
|
||||
<Suspense fallback={<LoadingStack />}>
|
||||
<Routes>
|
||||
<Route path="/" element={<Navigate to="overview" replace />} />
|
||||
<Route path="overview" element={<FeatureOverviewPage />} />
|
||||
<Route
|
||||
path="publication"
|
||||
element={<FeaturePublicationPage canManageTemplate={role !== "editor"} />}
|
||||
/>
|
||||
<Route path="brand" element={<FeatureBrandPage />} />
|
||||
<Route path="trust" element={<FeatureTrustContentPage />} />
|
||||
<Route path="services" element={<FeatureServicesPage />} />
|
||||
<Route path="inquiries" element={<FeatureInquiriesPage />} />
|
||||
<Route path="workspace" element={<FeatureWorkspacePage />} />
|
||||
{role === "editor" ? null : (
|
||||
<>
|
||||
<Route path="capabilities" element={<FeatureCapabilitiesPage />} />
|
||||
<Route path="members" element={<FeatureMembersPage />} />
|
||||
</>
|
||||
)}
|
||||
<Route path="*" element={<Navigate to="overview" replace />} />
|
||||
</Routes>
|
||||
</Suspense>
|
||||
</div>
|
||||
</AdminLayout.Content>
|
||||
</AdminLayout>
|
||||
</AdminLayout>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
import React, { useState } from "react";
|
||||
import { ChangePassword, Login } from "../features/auth/AuthPages.js";
|
||||
import { Dashboard } from "./Dashboard.js";
|
||||
import { adminMessage as message } from "../components/AdminMessage.js";
|
||||
|
||||
export 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 (
|
||||
<Login
|
||||
onLogin={(session) => {
|
||||
setRole(session.role);
|
||||
setMustChangePassword(session.mustChangePassword);
|
||||
setLoggedIn(true);
|
||||
}}
|
||||
/>
|
||||
);
|
||||
if (mustChangePassword) return <ChangePassword onComplete={() => setMustChangePassword(false)} />;
|
||||
return <Dashboard onLogout={logout} role={role} />;
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
import React from "react";
|
||||
|
||||
export const FeatureInquiriesPage = React.lazy(() =>
|
||||
import("../features/inquiries/InquiriesPage.js").then((module) => ({
|
||||
default: module.InquiriesPage,
|
||||
})),
|
||||
);
|
||||
export const FeatureTrustContentPage = React.lazy(() =>
|
||||
import("../features/trust-content/TrustContentPage.js").then((module) => ({
|
||||
default: module.TrustContentPage,
|
||||
})),
|
||||
);
|
||||
export const FeatureServicesPage = React.lazy(() =>
|
||||
import("../features/services/ServicesPage.js").then((module) => ({
|
||||
default: module.ServicesPage,
|
||||
})),
|
||||
);
|
||||
export const FeatureBrandPage = React.lazy(() =>
|
||||
import("../features/brand/BrandPage.js").then((module) => ({ default: module.BrandPage })),
|
||||
);
|
||||
export const FeatureWorkspacePage = React.lazy(() =>
|
||||
import("../features/workspace/WorkspacePage.js").then((module) => ({
|
||||
default: module.WorkspacePage,
|
||||
})),
|
||||
);
|
||||
export const FeatureOverviewPage = React.lazy(() =>
|
||||
import("../features/overview/OverviewPage.js").then((module) => ({
|
||||
default: module.OverviewPage,
|
||||
})),
|
||||
);
|
||||
export const FeatureCapabilitiesPage = React.lazy(() =>
|
||||
import("../features/capabilities/CapabilitiesPage.js").then((module) => ({
|
||||
default: module.CapabilitiesPage,
|
||||
})),
|
||||
);
|
||||
export const FeatureMembersPage = React.lazy(() =>
|
||||
import("../features/team/MembersPage.js").then((module) => ({
|
||||
default: module.MembersPage,
|
||||
})),
|
||||
);
|
||||
export const FeaturePublicationPage = React.lazy(() =>
|
||||
import("../features/publication/PublicationPage.js").then((module) => ({
|
||||
default: module.PublicationPage,
|
||||
})),
|
||||
);
|
||||
Reference in new issue
Block a user