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) => { const beginOffering = (item?: Offering) => {
setEditingOffering(item ?? null); setEditingOffering(item ?? null);
setOfferingOpen(true); setOfferingOpen(true);
const config = (item?.config ?? {}) as Record<string, unknown>;
offeringForm.setFieldsValue( offeringForm.setFieldsValue(
item item
? { ? {
...item, ...item,
sections: JSON.stringify(item.sections ?? [], null, 2), sections: config.sections ?? [],
media: JSON.stringify(item.media ?? [], null, 2), media: config.media ?? [],
cta: JSON.stringify(item.cta ?? null, null, 2), cta: config.cta ?? {},
} }
: { : { kind: "service", isEnabled: true, sortOrder: 0, sections: [], media: [], cta: {} },
kind: "service",
isEnabled: true,
sortOrder: 0,
sections: "[]",
media: "[]",
cta: "null",
},
); );
}; };
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 ( return (
<Card <Card
title="服务与合作" title="服务与合作"
@@ -191,13 +173,7 @@ export function ServicesPage() {
<Form <Form
form={offeringForm} form={offeringForm}
layout="vertical" layout="vertical"
onFinish={(values) => { onFinish={(values) => saveOffering.mutate(values)}
try {
saveOffering.mutate(parseOfferingValues(values));
} catch (error) {
message.error(error instanceof Error ? error.message : "详情配置格式错误");
}
}}
> >
<Form.Item name="kind" label="类型" rules={[{ required: true }]}> <Form.Item name="kind" label="类型" rules={[{ required: true }]}>
<Select <Select
@@ -224,23 +200,80 @@ export function ServicesPage() {
<Form.Item name="coverUrl" label="封面图片 URL"> <Form.Item name="coverUrl" label="封面图片 URL">
<Input /> <Input />
</Form.Item> </Form.Item>
<Form.Item name="sections" label="内容分区 JSON"> <Form.List name="sections">
<Input.TextArea {(fields, { add, remove }) => (
rows={6} <>
placeholder='例如:[ { "type": "text", "title": "服务内容", "content": "..." } ]' <Form.Item label="内容分区">
/> <Button onClick={() => add({ type: "text" })}>新增分区</Button>
</Form.Item> </Form.Item>
<Form.Item name="media" label="媒体列表 JSON"> {fields.map(({ key, name, ...restField }) => (
<Input.TextArea <Space key={key} align="start" style={{ display: "flex", marginBottom: 8 }}>
rows={4} <Form.Item {...restField} name={[name, "type"]}>
placeholder='例如:[ { "url": "https://...", "type": "image", "alt": "封面" } ]' <Select
/> style={{ width: 120 }}
</Form.Item> options={[
<Form.Item name="cta" label="行动按钮 JSON"> { value: "text", label: "文本" },
<Input.TextArea { value: "image-list", label: "图片列表" },
rows={3} { value: "timeline", label: "时间线" },
placeholder='例如:{ "type": "inquiry", "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>
<Form.Item name="isEnabled" label="公开状态"> <Form.Item name="isEnabled" label="公开状态">
<Select <Select