feat: 建立 React 管理后台基础架构

This commit is contained in:
que01 committed 2026-09-19 17:46:57 +08:00
1 parent b7fbfa1646
commit f29e64724d
6 files changed
+1448 -7

No files matched your search

+32
View File
@@ -0,0 +1,32 @@
import React, { useState } from "react";
import { createRoot } from "react-dom/client";
import { QueryClient, QueryClientProvider, useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { App, Button, Card, Form, Input, Layout, Menu, Select, Space, Typography, message } from "antd";
import { ApiClient } from "@personal-brand/api-client";
import type { BrandProfile } from "@personal-brand/api-client";
import "antd/dist/reset.css";
const apiBaseUrl = "http://localhost:3001";
const queryClient = new QueryClient();
const api = () => new ApiClient({ baseUrl: apiBaseUrl, token: localStorage.getItem("adminToken") ?? undefined, fetcher: (input, init) => fetch(input, init) });
function Login({ onLogin }: { onLogin: () => 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); onLogin(); } catch (error) { message.error(error instanceof Error ? error.message : "登录失败"); } finally { setLoading(false); } }
return <Card title="个人品牌后台" style={{ maxWidth: 420, margin: "120px auto" }}><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" htmlType="submit" loading={loading} block>登录</Button></Form></Card>;
}
function BrandPage() {
const client = api(); const queryClientHook = useQueryClient(); const [form] = Form.useForm<BrandProfile>();
const brand = useQuery({ queryKey: ["admin-brand"], queryFn: () => client.getAdminBrand() });
const save = useMutation({ mutationFn: (values: Partial<BrandProfile>) => client.updateAdminBrand(values), onSuccess: () => { message.success("品牌配置已保存"); void queryClientHook.invalidateQueries({ queryKey: ["admin-brand"] }); } });
if (brand.isLoading) return <Card loading />;
if (brand.isError || !brand.data) return <Card>品牌配置加载失败</Card>;
return <Card title="品牌信任配置"><Form form={form} layout="vertical" initialValues={brand.data} onFinish={(values) => save.mutate(values)}><Form.Item name="templateKey" label="行业模板"><Select options={[{ value: "personal", label: "个人品牌" }, { value: "food", label: "餐饮模板" }]} /></Form.Item><Form.Item name="name" label="品牌名称" rules={[{ required: true }]}><Input /></Form.Item><Form.Item name="tagline" label="一句话定位"><Input /></Form.Item><Form.Item name="description" label="品牌简介"><Input.TextArea rows={5} /></Form.Item><Form.Item name="hours" label="服务时间"><Input /></Form.Item><Form.Item name="address" label="所在位置"><Input /></Form.Item><Form.Item name="contactPhone" label="联系电话"><Input /></Form.Item><Form.Item name="contactWechat" label="微信号"><Input /></Form.Item><Button type="primary" htmlType="submit" loading={save.isPending}>保存配置</Button></Form></Card>;
}
function Dashboard() { const [page, setPage] = useState("brand"); return <Layout style={{ minHeight: "100vh" }}><Layout.Sider><Typography.Title level={4} style={{ color: "white", padding: 20 }}>个人品牌</Typography.Title><Menu theme="dark" selectedKeys={[page]} onClick={({ key }) => setPage(key)} items={[{ key: "brand", label: "品牌信任" }, { key: "trust", label: "信任内容" }, { key: "services", label: "服务与合作" }, { key: "inquiries", label: "咨询记录" }]} /></Layout.Sider><Layout><Layout.Header style={{ background: "white" }}><Space>品牌管理后台</Space></Layout.Header><Layout.Content style={{ padding: 24 }}>{page === "brand" ? <BrandPage /> : <Card title="即将开放">该模块正在接入通用配置接口。</Card>}</Layout.Content></Layout></Layout>; }
function Root() { const [loggedIn, setLoggedIn] = useState(Boolean(localStorage.getItem("adminToken"))); return loggedIn ? <Dashboard /> : <Login onLogin={() => setLoggedIn(true)} />; }
createRoot(document.getElementById("app")!).render(<React.StrictMode><QueryClientProvider client={queryClient}><App><Root /></App></QueryClientProvider></React.StrictMode>);