fix: 收敛模板编辑发布契约并还原餐饮页面视觉

This commit is contained in:
que01 committed 2026-10-08 13:36:59 +08:00
1 parent 20bcb43534
commit f4061a24bd
102 files changed
+4510 -870

No files matched your search

+4 -2
View File
@@ -33,7 +33,7 @@ export function BrandPage() {
{!brand.data ? null : (
<>
<AdminPageHeader
title="品牌表达"
title="品牌资料"
description="先让访客理解你是谁、服务谁,以及为什么值得信任。"
extra={<Tag color="blue">{template.data?.name ?? "当前模板"}</Tag>}
/>
@@ -152,7 +152,9 @@ export function BrandPage() {
</Col>
<Col xs={24} xl={9}>
<Card title="公开表达预览" style={{ position: "sticky", top: 24 }}>
<Typography.Text type="secondary">小程序首页将优先使用以下信息</Typography.Text>
<Typography.Text type="secondary">
这些资料会用于小程序首页和联系区域
</Typography.Text>
<Typography.Title level={3}>{brand.data.name || "品牌名称"}</Typography.Title>
<Typography.Title level={5}>{brand.data.tagline || "一句话定位"}</Typography.Title>
<Typography.Paragraph>
@@ -2,17 +2,23 @@ import {
Alert,
Button,
Card,
Divider,
Col,
Form,
Grid,
Input,
InputNumber,
Row,
Select,
Segmented,
Space,
Tag,
Typography,
message,
} from "antd";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { useEffect, useMemo } from "react";
import { useEffect, useMemo, useState } from "react";
import { useNavigate } from "react-router-dom";
import type { AdminTemplateContent, TemplateAdminField } from "@personal-brand/api-client";
import { createApiClient } from "../../lib/api.js";
import { ImageUrlField } from "../../components/ImageUrlField.js";
@@ -46,6 +52,22 @@ function renderField(field: TemplateAdminField) {
return <ImageUrlField {...(field.placeholder ? { placeholder: field.placeholder } : {})} />;
if (field.input === "number")
return <InputNumber style={{ width: "100%" }} placeholder={field.placeholder} />;
if (field.input === "boolean")
return (
<Select
options={[
{ value: true, label: "是" },
{ value: false, label: "否" },
]}
/>
);
if (field.input === "select" || field.input === "multi-select")
return (
<Select
{...(field.input === "multi-select" ? { mode: "multiple" as const } : {})}
options={field.options ?? []}
/>
);
return (
<Input
type={field.input === "phone" ? "tel" : "text"}
@@ -63,15 +85,88 @@ export function PresentationEditor({
}: PresentationEditorProps) {
const [form] = Form.useForm<PresentationValues>();
const queryClient = useQueryClient();
const sections = useMemo(
() =>
(templateContent?.admin.sections ?? [])
.map((section) => ({
...section,
fields: section.fields.filter((field) => field.binding.startsWith("brand.config.")),
}))
.filter((section) => section.fields.length > 0),
[templateContent],
const navigate = useNavigate();
const breakpoints = Grid.useBreakpoint();
const [activePage, setActivePage] = useState<string>();
const sections = useMemo(() => {
const declaredTemplateFields = (templateContent?.templateFields ?? []).map((field) => ({
...field,
binding: field.binding ?? `brand.config.templateData.${field.key}`,
}));
return [
...(declaredTemplateFields.length
? [
{
key: "template-data",
title: "模板专属内容",
description: "这些字段由当前小程序模板定义,保存后发布才会对外展示。",
page: templateContent?.templateFields?.[0]?.page ?? "home",
target: "brand" as const,
fields: declaredTemplateFields,
},
]
: []),
...(templateContent?.admin.sections ?? []),
]
.map((section) => ({
...section,
fields: section.fields.filter(
(field) =>
field.binding.startsWith("brand.config.") ||
[
"brand.avatarUrl",
"brand.description",
"brand.contactPhone",
"brand.contactWechat",
"brand.address",
].includes(field.binding),
),
}))
.filter((section) => section.fields.length > 0);
}, [templateContent]);
const pageOptions = useMemo(() => {
const pages = new Map<string, string>();
for (const item of templateContent?.navigation ?? []) pages.set(item.page, item.label);
for (const section of templateContent?.admin.sections ?? []) {
const navigation = templateContent?.navigation.find((item) => item.page === section.page);
if (!pages.has(section.page))
pages.set(section.page, navigation?.label ?? pageTitle(section.page));
}
return [...pages].map(([value, label]) => ({ value, label }));
}, [templateContent]);
const selectedPage = pageOptions.some((item) => item.value === activePage)
? activePage
: pageOptions[0]?.value;
const visibleSections = sections.filter((section) => section.page === selectedPage);
const relatedTargets = [
...(templateContent?.admin.sections ?? [])
.filter((section) => section.page === selectedPage && section.target !== "brand")
.map((section) => section.target),
...((templateContent?.trustContentTypes ?? []).some((item) =>
item.pages?.includes(selectedPage ?? ""),
)
? ["trust"]
: []),
];
const pageLinks = [...new Set(relatedTargets)].flatMap((target) =>
target === "trust"
? (templateContent?.trustContentTypes ?? [])
.filter((item) => item.pages?.includes(selectedPage ?? ""))
.map((item) => ({
target: `trust:${item.type}`,
label: `编辑${item.label}`,
}))
: [
{
target,
label: (
{ offering: "管理此页展示的业务内容", inquiry: "管理此页的咨询表单" } as Record<
string,
string
>
)[target],
},
],
);
const initialValues = useMemo(() => {
const values: PresentationValues = {};
@@ -105,10 +200,20 @@ export function PresentationEditor({
form.setFieldsValue(initialValues);
}, [form, initialValues]);
useEffect(() => {
if (!pageOptions.some((item) => item.value === activePage))
setActivePage(pageOptions[0]?.value);
}, [activePage, pageOptions]);
return (
<Card title="小程序页面内容" style={{ marginTop: 24 }}>
<Card
title="小程序页面配置"
extra={templateContent ? <Tag color="blue">{templateContent.displayName}</Tag> : null}
style={{ marginTop: 32 }}
styles={{ body: { minHeight: breakpoints.md ? 760 : 620 } }}
>
<Typography.Paragraph type="secondary">
编辑访客在小程序中看到的文字和图片。不同模板会提供适合本行业的内容项目。
按小程序页面查找内容。这里修改的是草稿,发布后访客才会看到更新。
</Typography.Paragraph>
{loading ? <Alert type="info" showIcon message="正在读取当前模板的编辑项目" /> : null}
{error ? (
@@ -119,51 +224,108 @@ export function PresentationEditor({
) : null}
{templateContent ? (
<>
<Typography.Title level={5}>{templateContent.displayName}</Typography.Title>
<Space wrap style={{ marginBottom: 24 }}>
{breakpoints.xs ? (
<Select
aria-label="选择小程序页面"
value={selectedPage}
options={pageOptions}
onChange={setActivePage}
style={{ minWidth: 180 }}
/>
) : (
<Segmented
value={selectedPage}
options={pageOptions}
onChange={(value) => setActivePage(String(value))}
/>
)}
</Space>
<Form
form={form}
layout="vertical"
initialValues={initialValues}
onFinish={(values) => save.mutate(values)}
style={{ minHeight: 540, display: "flex", flexDirection: "column" }}
>
{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" || field.input === "list" ? 24 : 12}
>
{field.input === "list" ? (
<TemplateListField
field={field}
name={presentationFieldName(section.key, field.key)}
/>
) : (
<Form.Item
name={presentationFieldName(section.key, field.key)}
label={field.label}
{...(field.help ? { help: field.help } : {})}
{...(field.required
? { rules: [{ required: true, message: `请填写${field.label}` }] }
: {})}
<div style={{ flex: 1 }}>
{visibleSections.map((section, index) => (
<section key={section.key} aria-labelledby={`section-${section.key}`}>
{index > 0 ? <Divider /> : null}
<Typography.Title id={`section-${section.key}`} level={5}>
{section.title}
</Typography.Title>
<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" || field.input === "list" ? 24 : 12}
>
{field.input === "list" ? (
<TemplateListField
field={field}
name={presentationFieldName(section.key, field.key)}
/>
) : (
<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>
</section>
))}
{pageLinks.length ? (
<Alert
type="info"
showIcon
style={{ marginBottom: 24 }}
message="这个页面还有关联内容"
description={
<Space wrap>
{pageLinks.map((item) => (
<Button
key={item.target}
type="link"
onClick={() => {
if (item.target.startsWith("trust:")) {
void navigate(
`/admin/trust?type=${encodeURIComponent(item.target.slice(6))}`,
);
return;
}
void navigate(
item.target === "offering" ? "/admin/services" : "/admin/workspace",
);
}}
style={{ padding: 0 }}
>
{renderField(field)}
</Form.Item>
)}
</Col>
))}
</Row>
</Card>
))}
<Space direction="vertical" style={{ width: "100%" }}>
{item.label}
</Button>
))}
</Space>
}
/>
) : null}
{!visibleSections.length && !pageLinks.length ? (
<Typography.Paragraph type="secondary">
这个页面没有单独的品牌文案配置。
</Typography.Paragraph>
) : null}
</div>
<Space direction="vertical" style={{ width: "100%", marginTop: 24 }}>
<MutationError error={save.error} />
<Button type="primary" htmlType="submit" loading={save.isPending}>
保存页面内容
@@ -175,3 +337,20 @@ export function PresentationEditor({
</Card>
);
}
function pageTitle(page: string): string {
return (
(
{
home: "首页",
offerings: "业务展示",
service: "服务说明",
detail: "详情页",
inquiry: "咨询页",
success: "提交成功页",
records: "记录页",
profile: "主理人页",
} as Record<string, string>
)[page] ?? page
);
}
@@ -48,7 +48,10 @@ export function readPresentationValue(
? (value as unknown[]).map((item) => ({ [field.itemValueKey as string]: item }))
: value;
}
return typeof value === "string" || typeof value === "number" ? value : undefined;
if (field?.input === "multi-select") return Array.isArray(value) ? value : [];
return typeof value === "string" || typeof value === "number" || typeof value === "boolean"
? value
: undefined;
}
function writePath(