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

+1 -1
View File
@@ -1 +1 @@
<!doctype html><html lang="zh-CN"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>品牌后台</title></head><body><main id="app"></main><script type="module" src="./dist/entry.js"></script></body></html>
<!doctype html><html lang="zh-CN"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>个人品牌后台</title></head><body><main id="app"></main><script type="module" src="/src/main.tsx"></script></body></html>
+16 -3
View File
@@ -2,12 +2,25 @@
"name": "@personal-brand/admin",
"private": true,
"type": "module",
"dependencies": { "@personal-brand/api-client": "workspace:*" },
"dependencies": {
"@personal-brand/api-client": "workspace:*",
"@tanstack/react-query": "^5.103.1",
"antd": "^6.6.4",
"react": "^19.3.0",
"react-dom": "^19.3.0",
"react-router-dom": "^7.18.4"
},
"scripts": {
"build": "tsc -p tsconfig.json",
"dev": "tsc -p tsconfig.json --watch",
"build": "vite build",
"dev": "vite",
"lint": "eslint .",
"typecheck": "tsc -p tsconfig.json --noEmit",
"clean": "rimraf dist"
},
"devDependencies": {
"@types/react": "^19.3.0",
"@types/react-dom": "^19.3.0",
"@vitejs/plugin-react": "^6.1.1",
"vite": "^8.3.0"
}
}
+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>);
+1 -1
View File
@@ -1,5 +1,5 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": { "outDir": "dist", "rootDir": "src", "lib": ["ES2022", "DOM"] },
"compilerOptions": { "outDir": "dist", "rootDir": "src", "lib": ["ES2022", "DOM"], "jsx": "react-jsx", "module": "ESNext", "moduleResolution": "Bundler", "allowImportingTsExtensions": true },
"include": ["src/**/*.ts"]
}
+4
View File
@@ -0,0 +1,4 @@
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
export default defineConfig({ plugins: [react()], build: { outDir: "dist" } });
+1394 -2
View File
File diff suppressed because it is too large. Load diff