From bf4bbf81c72bde6075df0804f329f1f2b50cba8b Mon Sep 17 00:00:00 2001 From: que01 Date: Thu, 15 Oct 2026 15:10:21 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=8B=86=E5=88=86=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E7=AB=AF=E5=85=A5=E5=8F=A3=E5=B9=B6=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E6=9E=84=E5=BB=BA=E5=88=86=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/admin/src/app/Dashboard.tsx | 155 ++++++++ apps/admin/src/app/Root.tsx | 38 ++ apps/admin/src/app/routes.tsx | 45 +++ apps/admin/src/components/AdminButton.tsx | 6 + apps/admin/src/components/AdminCard.tsx | 6 + .../src/components/AdminConfigProvider.tsx | 14 + apps/admin/src/components/AdminForm.ts | 1 + apps/admin/src/components/AdminLayout.tsx | 12 + apps/admin/src/components/AdminMenu.tsx | 6 + apps/admin/src/components/AdminMessage.ts | 1 + apps/admin/src/components/AdminSelect.tsx | 6 + apps/admin/src/components/AdminSkeleton.tsx | 6 + apps/admin/src/components/LoadingStack.tsx | 17 + apps/admin/src/components/QueryState.tsx | 18 +- apps/admin/src/features/auth/AuthPages.tsx | 117 ++++++ apps/admin/src/main.tsx | 346 +----------------- apps/admin/vite.config.ts | 14 +- 17 files changed, 455 insertions(+), 353 deletions(-) create mode 100644 apps/admin/src/app/Dashboard.tsx create mode 100644 apps/admin/src/app/Root.tsx create mode 100644 apps/admin/src/app/routes.tsx create mode 100644 apps/admin/src/components/AdminButton.tsx create mode 100644 apps/admin/src/components/AdminCard.tsx create mode 100644 apps/admin/src/components/AdminConfigProvider.tsx create mode 100644 apps/admin/src/components/AdminForm.ts create mode 100644 apps/admin/src/components/AdminLayout.tsx create mode 100644 apps/admin/src/components/AdminMenu.tsx create mode 100644 apps/admin/src/components/AdminMessage.ts create mode 100644 apps/admin/src/components/AdminSelect.tsx create mode 100644 apps/admin/src/components/AdminSkeleton.tsx create mode 100644 apps/admin/src/components/LoadingStack.tsx create mode 100644 apps/admin/src/features/auth/AuthPages.tsx diff --git a/apps/admin/src/app/Dashboard.tsx b/apps/admin/src/app/Dashboard.tsx new file mode 100644 index 0000000..7d3485b --- /dev/null +++ b/apps/admin/src/app/Dashboard.tsx @@ -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: }, + { key: "publication", label: "配置与发布", icon: }, + { key: "brand", label: "小程序页面", icon: }, + { key: "trust", label: "信任内容", icon: }, + { key: "services", label: "业务提供项", icon: }, + { key: "inquiries", label: "咨询记录", icon: }, + { key: "workspace", label: "咨询表单与字段", icon: }, + ], + }, + ...(role === "editor" + ? [] + : [ + { + type: "group" as const, + label: "系统管理", + children: [ + { key: "capabilities", label: "能力插件", icon: }, + { key: "members", label: "团队成员", icon: }, + ], + }, + ]), + ]; + return ( + + +
+ + 品牌工作区 +
+ navigate(`/admin/${key}`)} + items={items} + /> +
+ + +
+ {brand.data?.name || "当前品牌"} + 品牌工作区 + + { + if (value) void switchBrand(value); + }} + options={(memberships.data ?? []).map((item) => ({ + value: item.brand.id, + label: item.brand.name, + }))} + /> +
+ + 退出登录 + +
+ +
+ }> + + } /> + } /> + } + /> + } /> + } /> + } /> + } /> + } /> + {role === "editor" ? null : ( + <> + } /> + } /> + + )} + } /> + + +
+
+
+
+ ); +} diff --git a/apps/admin/src/app/Root.tsx b/apps/admin/src/app/Root.tsx new file mode 100644 index 0000000..b3c4c08 --- /dev/null +++ b/apps/admin/src/app/Root.tsx @@ -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 ( + { + setRole(session.role); + setMustChangePassword(session.mustChangePassword); + setLoggedIn(true); + }} + /> + ); + if (mustChangePassword) return setMustChangePassword(false)} />; + return ; +} diff --git a/apps/admin/src/app/routes.tsx b/apps/admin/src/app/routes.tsx new file mode 100644 index 0000000..cd1ea7a --- /dev/null +++ b/apps/admin/src/app/routes.tsx @@ -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, + })), +); diff --git a/apps/admin/src/components/AdminButton.tsx b/apps/admin/src/components/AdminButton.tsx new file mode 100644 index 0000000..fcbcda8 --- /dev/null +++ b/apps/admin/src/components/AdminButton.tsx @@ -0,0 +1,6 @@ +import { Button } from "antd"; +import type { ButtonProps } from "antd"; + +export function AdminButton(props: ButtonProps) { + return - -

仅限授权成员访问。

- - - ); -} +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 ( -
- -
- - 首次登录需要修改密码 -
-

请先设置只有你本人知道的新密码。

-
- - - - - - - - - - -
-
-
- ); -} - -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: }, - { key: "publication", label: "配置与发布", icon: }, - { key: "brand", label: "小程序页面", icon: }, - { key: "trust", label: "信任内容", icon: }, - { key: "services", label: "业务提供项", icon: }, - { key: "inquiries", label: "咨询记录", icon: }, - { key: "workspace", label: "咨询表单与字段", icon: }, - ], - }, - ...(role === "editor" - ? [] - : [ - { - type: "group" as const, - label: "系统管理", - children: [ - { key: "capabilities", label: "能力插件", icon: }, - { key: "members", label: "团队成员", icon: }, - ], - }, - ]), - ]; - return ( - - -
- - 品牌工作区 -
- navigate(`/admin/${key}`)} - items={items} - /> - - - -
- {brand.data?.name || "当前品牌"} - 品牌工作区 - -