refactor: 将提供项详情改为结构化表单

This commit is contained in:
que01 committed 2026-09-20 09:18:29 +08:00
1 parent 7f0830567e
commit 02aa698fb9
1 file changed
+80 -47
@@ -77,36 +77,18 @@ export function ServicesPage() {
const beginOffering = (item?: Offering) => {
setEditingOffering(item ?? null);
setOfferingOpen(true);
const config = (item?.config ?? {}) as Record<string, unknown>;
offeringForm.setFieldsValue(
item
? {
...item,
sections: JSON.stringify(item.sections ?? [], null, 2),
media: JSON.stringify(item.media ?? [], null, 2),
cta: JSON.stringify(item.cta ?? null, null, 2),
sections: config.sections ?? [],
media: config.media ?? [],
cta: config.cta ?? {},
}
: {
kind: "service",
isEnabled: true,
sortOrder: 0,
sections: "[]",
media: "[]",
cta: "null",
},
: { kind: "service", isEnabled: true, sortOrder: 0, sections: [], media: [], cta: {} },
);
};
const parseOfferingValues = (values: Record<string, unknown>) => {
for (const key of ["sections", "media", "cta"]) {
if (typeof values[key] === "string") {
try {
values[key] = JSON.parse(values[key] as string);
} catch {
throw new Error(`${key} 必须是合法 JSON`);
}
}
}
return values;
};
return (
<Card
title="服务与合作"
@@ -191,13 +173,7 @@ export function ServicesPage() {
<Form
form={offeringForm}
layout="vertical"
onFinish={(values) => {
try {
saveOffering.mutate(parseOfferingValues(values));
} catch (error) {
message.error(error instanceof Error ? error.message : "详情配置格式错误");
}
}}
onFinish={(values) => saveOffering.mutate(values)}
>
<Form.Item name="kind" label="类型" rules={[{ required: true }]}>
<Select
@@ -224,23 +200,80 @@ export function ServicesPage() {
<Form.Item name="coverUrl" label="封面图片 URL">
<Input />
</Form.Item>
<Form.Item name="sections" label="内容分区 JSON">
<Input.TextArea
rows={6}
placeholder='例如:[ { "type": "text", "title": "服务内容", "content": "..." } ]'
/>
</Form.Item>
<Form.Item name="media" label="媒体列表 JSON">
<Input.TextArea
rows={4}
placeholder='例如:[ { "url": "https://...", "type": "image", "alt": "封面" } ]'
/>
</Form.Item>
<Form.Item name="cta" label="行动按钮 JSON">
<Input.TextArea
rows={3}
placeholder='例如:{ "type": "inquiry", "label": "立即咨询" }'
/>
<Form.List name="sections">
{(fields, { add, remove }) => (
<>
<Form.Item label="内容分区">
<Button onClick={() => add({ type: "text" })}>新增分区</Button>
</Form.Item>
{fields.map(({ key, name, ...restField }) => (
<Space key={key} align="start" style={{ display: "flex", marginBottom: 8 }}>
<Form.Item {...restField} name={[name, "type"]}>
<Select
style={{ width: 120 }}
options={[
{ value: "text", label: "文本" },
{ value: "image-list", label: "图片列表" },
{ value: "timeline", label: "时间线" },
{ value: "faq", label: "常见问题" },
]}
/>
</Form.Item>
<Form.Item {...restField} name={[name, "title"]}>
<Input placeholder="分区标题" />
</Form.Item>
<Form.Item {...restField} name={[name, "content"]}>
<Input placeholder="分区内容" />
</Form.Item>
<Button danger onClick={() => remove(name)}>
删除
</Button>
</Space>
))}
</>
)}
</Form.List>
<Form.List name="media">
{(fields, { add, remove }) => (
<>
<Form.Item label="媒体列表">
<Button onClick={() => add({ type: "image" })}>新增媒体</Button>
</Form.Item>
{fields.map(({ key, name, ...restField }) => (
<Space key={key} align="start" style={{ display: "flex", marginBottom: 8 }}>
<Form.Item {...restField} name={[name, "url"]}>
<Input placeholder="图片 URL" />
</Form.Item>
<Form.Item {...restField} name={[name, "alt"]}>
<Input placeholder="替代文本" />
</Form.Item>
<Button danger onClick={() => remove(name)}>
删除
</Button>
</Space>
))}
</>
)}
</Form.List>
<Form.Item label="行动按钮">
<Space>
<Form.Item name={["cta", "type"]} noStyle>
<Select
style={{ width: 150 }}
allowClear
placeholder="按钮行为"
options={[
{ value: "inquiry", label: "提交咨询" },
{ value: "booking", label: "预约服务" },
{ value: "contact", label: "联系品牌" },
{ value: "external-link", label: "外部链接" },
]}
/>
</Form.Item>
<Form.Item name={["cta", "label"]} noStyle>
<Input placeholder="按钮文案" />
</Form.Item>
</Space>
</Form.Item>
<Form.Item name="isEnabled" label="公开状态">
<Select