feat: 完善模板内容后台配置能力

This commit is contained in:
que01 committed 2026-09-28 17:30:34 +08:00
1 parent 2bc2193b44
commit 9266ec824a
37 files changed
+2273 -63

No files matched your search

@@ -16,6 +16,7 @@ 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 { TemplateListField } from "../../components/TemplateListField.js";
import { MutationError } from "../../components/QueryState.js";
import {
applyPresentationValues,
@@ -62,7 +63,16 @@ export function PresentationEditor({
}: PresentationEditorProps) {
const [form] = Form.useForm<PresentationValues>();
const queryClient = useQueryClient();
const sections = templateContent?.admin.sections ?? [];
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 initialValues = useMemo(() => {
const values: PresentationValues = {};
for (const section of sections) {
@@ -70,6 +80,7 @@ export function PresentationEditor({
values[presentationFieldName(section.key, field.key)] = readPresentationValue(
config,
field.binding,
field,
);
}
}
@@ -82,7 +93,7 @@ export function PresentationEditor({
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"] });
@@ -125,17 +136,28 @@ export function PresentationEditor({
<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
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>
@@ -1,6 +1,8 @@
export type PresentationField = {
key: string;
binding: string;
input?: string;
itemValueKey?: string;
};
export type PresentationSection = {
@@ -8,7 +10,7 @@ export type PresentationSection = {
fields: PresentationField[];
};
export type PresentationValues = Record<string, string | number | null | undefined>;
export type PresentationValues = Record<string, unknown>;
function asObject(value: unknown): Record<string, unknown> {
return value && typeof value === "object" && !Array.isArray(value)
@@ -17,8 +19,11 @@ function asObject(value: unknown): Record<string, unknown> {
}
function safePath(binding: string): string[] | null {
if (!binding.startsWith("brand.config.")) return null;
const path = binding.slice("brand.config.".length).split(".");
const prefix = ["brand.config.", "offering.config.", "trust.config."].find((candidate) =>
binding.startsWith(candidate),
);
if (!prefix) return null;
const path = binding.slice(prefix.length).split(".");
if (path.some((part) => !part || ["__proto__", "prototype", "constructor"].includes(part)))
return null;
return path;
@@ -31,18 +36,25 @@ export function presentationFieldName(sectionKey: string, fieldKey: string): str
export function readPresentationValue(
config: Record<string, unknown> | null | undefined,
binding: string,
): string | number | undefined {
field?: PresentationField,
): unknown {
const path = safePath(binding);
if (!path) return undefined;
let value: unknown = asObject(config);
for (const part of path) value = asObject(value)[part];
if (field?.input === "list") {
if (!Array.isArray(value)) return [];
return field.itemValueKey
? (value as unknown[]).map((item) => ({ [field.itemValueKey as string]: item }))
: value;
}
return typeof value === "string" || typeof value === "number" ? value : undefined;
}
function writePath(
source: Record<string, unknown>,
path: string[],
value: string | number | null | undefined,
value: unknown,
): Record<string, unknown> {
const [head, ...tail] = path;
if (!head) return source;
@@ -63,11 +75,16 @@ export function applyPresentationValues(
for (const field of section.fields) {
const path = safePath(field.binding);
if (!path) continue;
nextConfig = writePath(
nextConfig,
path,
values[presentationFieldName(section.key, field.key)],
);
const raw = values[presentationFieldName(section.key, field.key)];
const value =
field.input === "list" && Array.isArray(raw)
? field.itemValueKey
? raw
.map((item) => asObject(item)[field.itemValueKey as string])
.filter((item) => typeof item === "string" && item.trim())
: raw
: raw;
nextConfig = writePath(nextConfig, path, value);
}
}
return nextConfig;