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,
|
||||
})),
|
||||
);
|
||||
@@ -0,0 +1,6 @@
|
||||
import { Button } from "antd";
|
||||
import type { ButtonProps } from "antd";
|
||||
|
||||
export function AdminButton(props: ButtonProps) {
|
||||
return <Button {...props} />;
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
import { Card } from "antd";
|
||||
import type { CardProps } from "antd";
|
||||
|
||||
export function AdminCard(props: CardProps) {
|
||||
return <Card {...props} />;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import { App, ConfigProvider } from "antd";
|
||||
import type { ConfigProviderProps } from "antd";
|
||||
import type { ReactNode } from "react";
|
||||
|
||||
export function AdminConfigProvider({
|
||||
children,
|
||||
...props
|
||||
}: ConfigProviderProps & { children?: ReactNode }) {
|
||||
return (
|
||||
<ConfigProvider {...props}>
|
||||
<App>{children}</App>
|
||||
</ConfigProvider>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export { Form as AdminForm, Input as AdminInput } from "antd";
|
||||
@@ -0,0 +1,12 @@
|
||||
import { Layout } from "antd";
|
||||
import type { BasicProps } from "antd/es/layout/layout";
|
||||
|
||||
function AdminLayoutBase(props: BasicProps) {
|
||||
return <Layout {...props} />;
|
||||
}
|
||||
|
||||
export const AdminLayout = Object.assign(AdminLayoutBase, {
|
||||
Sider: Layout.Sider,
|
||||
Header: Layout.Header,
|
||||
Content: Layout.Content,
|
||||
});
|
||||
@@ -0,0 +1,6 @@
|
||||
import { Menu } from "antd";
|
||||
import type { MenuProps } from "antd";
|
||||
|
||||
export function AdminMenu(props: MenuProps) {
|
||||
return <Menu {...props} />;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export { message as adminMessage } from "antd";
|
||||
@@ -0,0 +1,6 @@
|
||||
import { Select } from "antd";
|
||||
import type { SelectProps } from "antd";
|
||||
|
||||
export function AdminSelect(props: SelectProps) {
|
||||
return <Select {...props} />;
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
import { Skeleton } from "antd";
|
||||
import type { SkeletonProps } from "antd";
|
||||
|
||||
export function AdminSkeleton(props: SkeletonProps) {
|
||||
return <Skeleton {...props} />;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import { AdminCard } from "./AdminCard.js";
|
||||
import { AdminSkeleton } from "./AdminSkeleton.js";
|
||||
|
||||
export function LoadingStack({ rows = 3 }: { rows?: number }) {
|
||||
return (
|
||||
<div className="admin-loading-stack">
|
||||
<AdminCard>
|
||||
<AdminSkeleton active paragraph={{ rows: 2 }} title={false} />
|
||||
</AdminCard>
|
||||
{Array.from({ length: rows }).map((_, index) => (
|
||||
<AdminCard key={index}>
|
||||
<AdminSkeleton active paragraph={{ rows: 1 }} title={false} />
|
||||
</AdminCard>
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import { Button, Card, Skeleton } from "antd";
|
||||
import { Button } from "antd";
|
||||
import type { ReactNode } from "react";
|
||||
import { LoadingStack } from "./LoadingStack.js";
|
||||
|
||||
function errorMessage(error: unknown, fallback: string) {
|
||||
return error instanceof Error ? error.message : fallback;
|
||||
@@ -21,21 +22,6 @@ export function QueryState({
|
||||
return <>{children}</>;
|
||||
}
|
||||
|
||||
export function LoadingStack({ rows = 3 }: { rows?: number }) {
|
||||
return (
|
||||
<div className="admin-loading-stack">
|
||||
<Card>
|
||||
<Skeleton active paragraph={{ rows: 2 }} title={false} />
|
||||
</Card>
|
||||
{Array.from({ length: rows }).map((_, index) => (
|
||||
<Card key={index}>
|
||||
<Skeleton active paragraph={{ rows: 1 }} title={false} />
|
||||
</Card>
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export function ErrorNote({
|
||||
error,
|
||||
onRetry,
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
import { useState } from "react";
|
||||
import { createApiClient } from "../../lib/api.js";
|
||||
import { BrandMark } from "../../components/BrandMark.js";
|
||||
import { adminMessage as message } from "../../components/AdminMessage.js";
|
||||
import { AdminButton } from "../../components/AdminButton.js";
|
||||
import { AdminCard } from "../../components/AdminCard.js";
|
||||
import { AdminForm, AdminInput } from "../../components/AdminForm.js";
|
||||
|
||||
export function Login({
|
||||
onLogin,
|
||||
}: {
|
||||
onLogin: (session: { role: string; mustChangePassword: boolean }) => void;
|
||||
}) {
|
||||
const [form] = AdminForm.useForm();
|
||||
const [loading, setLoading] = useState(false);
|
||||
async function submit(values: { username: string; password: string }) {
|
||||
setLoading(true);
|
||||
try {
|
||||
const result = await createApiClient().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 (
|
||||
<div className="admin-auth">
|
||||
<AdminCard className="admin-auth-card">
|
||||
<div className="admin-auth-brand">
|
||||
<BrandMark />
|
||||
<span className="admin-header-brand">个人品牌后台</span>
|
||||
</div>
|
||||
<p className="admin-auth-hint">管理你的品牌表达、内容发布与客户咨询。</p>
|
||||
<AdminForm form={form} layout="vertical" onFinish={submit}>
|
||||
<AdminForm.Item
|
||||
name="username"
|
||||
label="账号"
|
||||
initialValue="admin"
|
||||
rules={[{ required: true }]}
|
||||
>
|
||||
<AdminInput />
|
||||
</AdminForm.Item>
|
||||
<AdminForm.Item name="password" label="密码" rules={[{ required: true }]}>
|
||||
<AdminInput.Password />
|
||||
</AdminForm.Item>
|
||||
<AdminButton type="primary" size="large" htmlType="submit" loading={loading} block>
|
||||
登录
|
||||
</AdminButton>
|
||||
</AdminForm>
|
||||
<p className="admin-auth-footnote">仅限授权成员访问。</p>
|
||||
</AdminCard>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export function ChangePassword({ onComplete }: { onComplete: () => void }) {
|
||||
const [form] = AdminForm.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 createApiClient().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 (
|
||||
<div className="admin-auth">
|
||||
<AdminCard className="admin-auth-card">
|
||||
<div className="admin-auth-brand">
|
||||
<BrandMark />
|
||||
<span className="admin-header-brand">首次登录需要修改密码</span>
|
||||
</div>
|
||||
<p className="admin-auth-hint">请先设置只有你本人知道的新密码。</p>
|
||||
<AdminForm form={form} layout="vertical" onFinish={submit}>
|
||||
<AdminForm.Item name="currentPassword" label="初始密码" rules={[{ required: true }]}>
|
||||
<AdminInput.Password />
|
||||
</AdminForm.Item>
|
||||
<AdminForm.Item
|
||||
name="newPassword"
|
||||
label="新密码"
|
||||
rules={[{ required: true, min: 8, message: "请输入至少 8 位新密码" }]}
|
||||
>
|
||||
<AdminInput.Password />
|
||||
</AdminForm.Item>
|
||||
<AdminForm.Item name="confirmPassword" label="确认密码" rules={[{ required: true }]}>
|
||||
<AdminInput.Password />
|
||||
</AdminForm.Item>
|
||||
<AdminButton type="primary" size="large" htmlType="submit" loading={loading} block>
|
||||
保存新密码
|
||||
</AdminButton>
|
||||
</AdminForm>
|
||||
</AdminCard>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
+10
-336
@@ -1,81 +1,13 @@
|
||||
import React, { Suspense, useState } from "react";
|
||||
import React 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,
|
||||
ConfigProvider,
|
||||
Form,
|
||||
Input,
|
||||
Layout,
|
||||
Menu,
|
||||
Select,
|
||||
message,
|
||||
} from "antd";
|
||||
import zhCN from "antd/locale/zh_CN";
|
||||
import {
|
||||
AppstoreOutlined,
|
||||
BarsOutlined,
|
||||
CloudUploadOutlined,
|
||||
FormOutlined,
|
||||
MessageOutlined,
|
||||
MobileOutlined,
|
||||
SafetyCertificateOutlined,
|
||||
TeamOutlined,
|
||||
ThunderboltOutlined,
|
||||
} from "@ant-design/icons";
|
||||
import { BrowserRouter } from "react-router-dom";
|
||||
import { Root } from "./app/Root.js";
|
||||
import { createApiClient } from "./lib/api.js";
|
||||
import { adminTheme } from "./lib/theme.js";
|
||||
import { BrandMark } from "./components/BrandMark.js";
|
||||
import { LoadingStack } from "./components/QueryState.js";
|
||||
import { StatusTag } from "./components/StatusTag.js";
|
||||
import { AdminConfigProvider } from "./components/AdminConfigProvider.js";
|
||||
import "./styles/admin.css";
|
||||
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({
|
||||
@@ -90,275 +22,17 @@ 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 (
|
||||
<div className="admin-auth">
|
||||
<Card className="admin-auth-card">
|
||||
<div className="admin-auth-brand">
|
||||
<BrandMark />
|
||||
<span className="admin-header-brand">个人品牌后台</span>
|
||||
</div>
|
||||
<p className="admin-auth-hint">管理你的品牌表达、内容发布与客户咨询。</p>
|
||||
<Form form={form} layout="vertical" onFinish={submit}>
|
||||
<Form.Item name="username" label="账号" initialValue="admin" rules={[{ required: true }]}>
|
||||
<Input />
|
||||
</Form.Item>
|
||||
<Form.Item name="password" label="密码" rules={[{ required: true }]}>
|
||||
<Input.Password />
|
||||
</Form.Item>
|
||||
<Button type="primary" size="large" htmlType="submit" loading={loading} block>
|
||||
登录
|
||||
</Button>
|
||||
</Form>
|
||||
<p className="admin-auth-footnote">仅限授权成员访问。</p>
|
||||
</Card>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
export const api = createApiClient;
|
||||
|
||||
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 (
|
||||
<div className="admin-auth">
|
||||
<Card className="admin-auth-card">
|
||||
<div className="admin-auth-brand">
|
||||
<BrandMark />
|
||||
<span className="admin-header-brand">首次登录需要修改密码</span>
|
||||
</div>
|
||||
<p className="admin-auth-hint">请先设置只有你本人知道的新密码。</p>
|
||||
<Form form={form} layout="vertical" onFinish={submit}>
|
||||
<Form.Item name="currentPassword" label="初始密码" rules={[{ required: true }]}>
|
||||
<Input.Password />
|
||||
</Form.Item>
|
||||
<Form.Item
|
||||
name="newPassword"
|
||||
label="新密码"
|
||||
rules={[{ required: true, min: 8, message: "请输入至少 8 位新密码" }]}
|
||||
>
|
||||
<Input.Password />
|
||||
</Form.Item>
|
||||
<Form.Item name="confirmPassword" label="确认新密码" rules={[{ required: true }]}>
|
||||
<Input.Password />
|
||||
</Form.Item>
|
||||
<Button type="primary" size="large" htmlType="submit" loading={loading} block>
|
||||
保存新密码
|
||||
</Button>
|
||||
</Form>
|
||||
</Card>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
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 (
|
||||
<Layout className="admin-shell">
|
||||
<Layout.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>
|
||||
<Menu
|
||||
selectedKeys={[page]}
|
||||
onClick={({ key }) => navigate(`/admin/${key}`)}
|
||||
items={items}
|
||||
/>
|
||||
</Layout.Sider>
|
||||
<Layout className="admin-main">
|
||||
<Layout.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" ? "负责人" : "内容编辑"}
|
||||
/>
|
||||
<Select
|
||||
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>
|
||||
<Button type="text" onClick={onLogout}>
|
||||
退出登录
|
||||
</Button>
|
||||
</Layout.Header>
|
||||
<Layout.Content className="admin-content">
|
||||
<div className="admin-content-inner">
|
||||
<Suspense fallback={<LoadingStack />}>
|
||||
<Routes>
|
||||
<Route path="/admin/overview" element={<FeatureOverviewPage />} />
|
||||
<Route
|
||||
path="/admin/publication"
|
||||
element={<FeaturePublicationPage canManageTemplate={role !== "editor"} />}
|
||||
/>
|
||||
<Route path="/admin/brand" element={<FeatureBrandPage />} />
|
||||
<Route path="/admin/workspace" element={<FeatureWorkspacePage />} />
|
||||
{role === "editor" ? null : (
|
||||
<Route path="/admin/capabilities" element={<FeatureCapabilitiesPage />} />
|
||||
)}
|
||||
{role === "editor" ? null : (
|
||||
<Route path="/admin/members" element={<FeatureMembersPage />} />
|
||||
)}
|
||||
<Route path="/admin/trust" element={<FeatureTrustContentPage />} />
|
||||
<Route path="/admin/services" element={<FeatureServicesPage />} />
|
||||
<Route path="/admin/inquiries" element={<FeatureInquiriesPage />} />
|
||||
<Route path="*" element={<Navigate to="/admin/overview" replace />} />
|
||||
</Routes>
|
||||
</Suspense>
|
||||
</div>
|
||||
</Layout.Content>
|
||||
</Layout>
|
||||
</Layout>
|
||||
);
|
||||
}
|
||||
|
||||
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} />;
|
||||
}
|
||||
createRoot(document.getElementById("app")!).render(
|
||||
<React.StrictMode>
|
||||
<ConfigProvider locale={zhCN} theme={adminTheme}>
|
||||
<AdminConfigProvider locale={zhCN} theme={adminTheme}>
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<App>
|
||||
<BrowserRouter>
|
||||
<Root />
|
||||
</BrowserRouter>
|
||||
</App>
|
||||
<BrowserRouter>
|
||||
<Root />
|
||||
</BrowserRouter>
|
||||
</QueryClientProvider>
|
||||
</ConfigProvider>
|
||||
</AdminConfigProvider>
|
||||
</React.StrictMode>,
|
||||
);
|
||||
Reference in new issue
Block a user