feat: 完善模板驱动的品牌内容工作台

This commit is contained in:
que01 committed 2026-09-25 05:21:42 +08:00
1 parent 77224ea51f
commit dd55af8d73
46 files changed
+2204 -295

No files matched your search

@@ -1,189 +1,155 @@
import { Alert, Button, Card, Col, Form, Input, Row, Space, Typography, message } from "antd";
import {
Alert,
Button,
Card,
Col,
Form,
Input,
InputNumber,
Row,
Space,
Typography,
message,
} from "antd";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { useEffect } from "react";
import { useEffect, useMemo } from "react";
import type { AdminTemplateContent, TemplateAdminField } from "@personal-brand/api-client";
import { createApiClient } from "../../lib/api.js";
import { ImageUrlField } from "../../components/ImageUrlField.js";
import { MutationError } from "../../components/QueryState.js";
import {
applyPresentationValues,
presentationFieldName,
readPresentationValue,
type PresentationValues,
} from "./presentation-bindings.js";
interface PresentationEditorProps {
initialValue: PresentationFormValues;
runtimeValue?: PresentationFormValues | undefined;
runtimeLoading?: boolean | undefined;
runtimeError?: unknown;
templateContent: AdminTemplateContent | null | undefined;
config: Record<string, unknown> | null | undefined;
loading?: boolean;
error?: unknown;
}
type PresentationFormValues = {
identity?: Record<string, string>;
home?: Record<string, string>;
offerings?: Record<string, string>;
detail?: Record<string, string>;
profile?: Record<string, string>;
inquiry?: Record<string, string>;
success?: Record<string, string>;
records?: Record<string, string>;
};
function renderField(field: TemplateAdminField) {
if (field.input === "textarea")
return (
<Input.TextArea
rows={3}
maxLength={field.maxLength}
showCount={!!field.maxLength}
placeholder={field.placeholder}
/>
);
if (field.input === "image")
return <ImageUrlField {...(field.placeholder ? { placeholder: field.placeholder } : {})} />;
if (field.input === "number")
return <InputNumber style={{ width: "100%" }} placeholder={field.placeholder} />;
return (
<Input
type={field.input === "phone" ? "tel" : "text"}
maxLength={field.maxLength}
placeholder={field.placeholder}
/>
);
}
export function PresentationEditor({
initialValue,
runtimeValue,
runtimeLoading,
runtimeError,
templateContent,
config,
loading,
error,
}: PresentationEditorProps) {
const [form] = Form.useForm<PresentationFormValues>();
const [form] = Form.useForm<PresentationValues>();
const queryClient = useQueryClient();
const sections = templateContent?.admin.sections ?? [];
const initialValues = useMemo(() => {
const values: PresentationValues = {};
for (const section of sections) {
for (const field of section.fields) {
values[presentationFieldName(section.key, field.key)] = readPresentationValue(
config,
field.binding,
);
}
}
return values;
}, [config, sections]);
const save = useMutation({
mutationFn: (values: PresentationFormValues) =>
createApiClient().updateAdminBrandPresentation(values),
mutationFn: (values: PresentationValues) => {
const nextConfig = applyPresentationValues(config, sections, values);
return createApiClient().updateAdminBrand({ config: nextConfig });
},
onSuccess: () => {
message.success("页面表达已保存,小程序将使用最新内容");
message.success("页面内容已保存,小程序将使用最新内容");
void queryClient.invalidateQueries({ queryKey: ["admin-brand"] });
void queryClient.invalidateQueries({ queryKey: ["admin-workspace"] });
void queryClient.invalidateQueries({ queryKey: ["public-runtime-workspace"] });
void queryClient.invalidateQueries({ queryKey: ["publication-readiness"] });
},
});
useEffect(() => {
form.setFieldsValue(initialValue);
}, [form, initialValue]);
form.setFieldsValue(initialValues);
}, [form, initialValues]);
return (
<Card
title="小程序页面表达"
style={{ marginTop: 24 }}
extra={<Typography.Text type="secondary">普通用户配置</Typography.Text>}
>
<Card title="小程序页面内容" style={{ marginTop: 24 }}>
<Typography.Paragraph type="secondary">
这里的内容会显示在小程序中。只填写你希望访客看到的话,不需要理解模板或技术配置。
编辑访客在小程序中看到的文字和图片。不同模板会提供适合本行业的内容项目。
</Typography.Paragraph>
{runtimeLoading ? <Alert type="info" showIcon message="正在读取小程序当前内容" /> : null}
{runtimeError ? (
<Alert type="warning" showIcon message="小程序当前内容暂时无法读取,保存后可重新加载。" />
) : runtimeValue ? (
<Card size="small" title="小程序当前已生效内容" style={{ marginBottom: 16 }}>
<Typography.Text strong>
{runtimeValue.identity?.founder || "尚未设置主理人 / 老板姓名"}
</Typography.Text>
{runtimeValue.identity?.title ? (
<Typography.Text type="secondary"> · {runtimeValue.identity.title}</Typography.Text>
) : null}
<Typography.Paragraph strong>
{runtimeValue.home?.proofTitle || "尚未设置信任介绍标题"}
</Typography.Paragraph>
<Typography.Paragraph type="secondary">
{runtimeValue.inquiry?.subtitle || "尚未设置咨询说明"}
</Typography.Paragraph>
<Typography.Text type="secondary">
咨询按钮:{runtimeValue.inquiry?.submitLabel || "未设置"} · 记录入口:
{runtimeValue.profile?.recordsTitle || "未设置"}
</Typography.Text>
</Card>
{loading ? <Alert type="info" showIcon message="正在读取当前模板的编辑项目" /> : null}
{error ? (
<Alert type="warning" showIcon message="模板编辑项目读取失败,请刷新后重试。" />
) : null}
{!loading && !error && !templateContent ? (
<Alert type="warning" showIcon message="当前模板暂未提供可编辑的页面内容。" />
) : null}
{templateContent ? (
<>
<Typography.Title level={5}>{templateContent.displayName}</Typography.Title>
<Form
form={form}
layout="vertical"
initialValues={initialValues}
onFinish={(values) => save.mutate(values)}
>
{sections.map((section) => (
<Card
key={section.key}
size="small"
title={section.title}
style={{ marginBottom: 16 }}
>
<Typography.Paragraph type="secondary">{section.description}</Typography.Paragraph>
<Row gutter={16}>
{section.fields.map((field) => (
<Col key={field.key} xs={24} md={field.input === "textarea" ? 24 : 12}>
<Form.Item
name={presentationFieldName(section.key, field.key)}
label={field.label}
{...(field.help ? { help: field.help } : {})}
{...(field.required
? { rules: [{ required: true, message: `请填写${field.label}` }] }
: {})}
>
{renderField(field)}
</Form.Item>
</Col>
))}
</Row>
</Card>
))}
<Space direction="vertical" style={{ width: "100%" }}>
<MutationError error={save.error} />
<Button type="primary" htmlType="submit" loading={save.isPending}>
保存页面内容
</Button>
</Space>
</Form>
</>
) : null}
<Form
form={form}
layout="vertical"
initialValues={initialValue}
onFinish={(values) => save.mutate(values)}
>
<Card size="small" title="主理人信息" style={{ marginBottom: 16 }}>
<Typography.Paragraph type="secondary">
访客需要先知道是谁在提供服务。这里填写主理人或老板的公开信息。
</Typography.Paragraph>
<Row gutter={16}>
<Col xs={24} md={12}>
<Form.Item name={["identity", "founder"]} label="主理人 / 老板姓名">
<Input placeholder="例如:林然" />
</Form.Item>
</Col>
<Col xs={24} md={12}>
<Form.Item name={["identity", "title"]} label="身份或职位">
<Input placeholder="例如:品牌顾问 / 工作室主理人" />
</Form.Item>
</Col>
<Col xs={24}>
<Form.Item name={["identity", "slogan"]} label="主理人希望传达的一句话">
<Input placeholder="例如:认真做好每一次定制" />
</Form.Item>
</Col>
<Col xs={24} md={12}>
<Form.Item name={["identity", "phone"]} label="公开联系电话">
<Input placeholder="选填" />
</Form.Item>
</Col>
<Col xs={24} md={12}>
<Form.Item name={["identity", "wechat"]} label="公开微信号">
<Input placeholder="选填" />
</Form.Item>
</Col>
<Col xs={24}>
<Form.Item name={["identity", "studioAddress"]} label="工作地点或服务范围">
<Input placeholder="例如:杭州及周边地区 / 某某工作室" />
</Form.Item>
</Col>
</Row>
</Card>
<Card size="small" title="首页" style={{ marginBottom: 16 }}>
<Row gutter={16}>
<Col xs={24} md={12}>
<Form.Item name={["home", "founderBadge"]} label="主理人标签">
<Input placeholder="例如:亲自主理 / 10 年经验" />
</Form.Item>
</Col>
<Col xs={24} md={12}>
<Form.Item name={["home", "proofTitle"]} label="信任介绍标题">
<Input placeholder="例如:用经历和作品建立信任" />
</Form.Item>
</Col>
<Col xs={24} md={12}>
<Form.Item name={["home", "worksTitle"]} label="作品区域标题">
<Input placeholder="例如:精选作品" />
</Form.Item>
</Col>
<Col xs={24} md={12}>
<Form.Item name={["home", "flowTitle"]} label="合作流程标题">
<Input placeholder="例如:从沟通到交付" />
</Form.Item>
</Col>
</Row>
</Card>
<Card size="small" title="咨询页面" style={{ marginBottom: 16 }}>
<Form.Item name={["inquiry", "title"]} label="页面标题">
<Input placeholder="例如:告诉我你的需求" />
</Form.Item>
<Form.Item name={["inquiry", "subtitle"]} label="说明文字">
<Input.TextArea
rows={2}
showCount
maxLength={120}
placeholder="告诉访客提交后会发生什么。"
/>
</Form.Item>
<Form.Item name={["inquiry", "submitLabel"]} label="提交按钮文字">
<Input placeholder="例如:提交咨询" />
</Form.Item>
</Card>
<Card size="small" title="主理人与我" style={{ marginBottom: 16 }}>
<Row gutter={16}>
<Col xs={24} md={12}>
<Form.Item name={["profile", "recordsTitle"]} label="咨询记录入口标题">
<Input placeholder="例如:我的咨询记录" />
</Form.Item>
</Col>
<Col xs={24} md={12}>
<Form.Item name={["profile", "coverageTitle"]} label="服务范围标题">
<Input placeholder="例如:服务范围与合作方式" />
</Form.Item>
</Col>
<Col xs={24} md={12}>
<Form.Item name={["profile", "warrantyTitle"]} label="承诺标题">
<Input placeholder="例如:我的服务承诺" />
</Form.Item>
</Col>
</Row>
</Card>
<Space direction="vertical" style={{ width: "100%" }}>
<MutationError error={save.error} />
<Button type="primary" htmlType="submit" loading={save.isPending}>
保存页面表达
</Button>
</Space>
</Form>
</Card>
);
}