feat: 完善模板内容后台配置能力
This commit is contained in:
1 parent
2bc2193b44
commit
9266ec824a
37 files changed
+2273
-63
No files matched your search
@@ -0,0 +1,41 @@
|
||||
import { Button, Form, Input, Space } from "antd";
|
||||
import type { TemplateAdminField } from "@personal-brand/api-client";
|
||||
|
||||
export function TemplateListField({
|
||||
field,
|
||||
name,
|
||||
}: {
|
||||
field: TemplateAdminField;
|
||||
name: string | (string | number)[];
|
||||
}) {
|
||||
return (
|
||||
<Form.Item label={field.label} help={field.help}>
|
||||
<Form.List name={name}>
|
||||
{(rows, { add, remove }) => (
|
||||
<Space direction="vertical" style={{ width: "100%" }}>
|
||||
{rows.map((row, index) => (
|
||||
<Space key={row.key} align="start" wrap>
|
||||
{field.itemFields?.map((itemField) => (
|
||||
<Form.Item
|
||||
key={itemField.key}
|
||||
name={[row.name, itemField.key]}
|
||||
label={`${index + 1}. ${itemField.label}`}
|
||||
rules={
|
||||
itemField.required === false
|
||||
? []
|
||||
: [{ required: true, message: `请填写${itemField.label}` }]
|
||||
}
|
||||
>
|
||||
{itemField.input === "textarea" ? <Input.TextArea rows={2} /> : <Input />}
|
||||
</Form.Item>
|
||||
))}
|
||||
<Button onClick={() => remove(row.name)}>删除</Button>
|
||||
</Space>
|
||||
))}
|
||||
<Button onClick={() => add()}>新增{field.label}</Button>
|
||||
</Space>
|
||||
)}
|
||||
</Form.List>
|
||||
</Form.Item>
|
||||
);
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -13,11 +13,17 @@ import {
|
||||
message,
|
||||
} from "antd";
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import type { Offering } from "@personal-brand/api-client";
|
||||
import type { Offering, TemplateAdminField } from "@personal-brand/api-client";
|
||||
import { useState } from "react";
|
||||
import { createApiClient } from "../../lib/api.js";
|
||||
import { AdminPageHeader } from "../../components/AdminPageHeader.js";
|
||||
import { ImageUrlField } from "../../components/ImageUrlField.js";
|
||||
import { TemplateListField } from "../../components/TemplateListField.js";
|
||||
import {
|
||||
applyPresentationValues,
|
||||
presentationFieldName,
|
||||
readPresentationValue,
|
||||
} from "../brand/presentation-bindings.js";
|
||||
import { MutationError, QueryState } from "../../components/QueryState.js";
|
||||
|
||||
export function ServicesPage() {
|
||||
@@ -30,11 +36,32 @@ export function ServicesPage() {
|
||||
queryKey: ["admin-offerings"],
|
||||
queryFn: () => client.listAdminOfferings(),
|
||||
});
|
||||
const workspace = useQuery({
|
||||
queryKey: ["admin-workspace-offering-fields"],
|
||||
queryFn: () => client.getAdminWorkspace(),
|
||||
});
|
||||
const templateFields: TemplateAdminField[] = (
|
||||
workspace.data?.templateContent?.admin.sections ?? []
|
||||
)
|
||||
.filter((section) => section.target === "offering")
|
||||
.flatMap((section) =>
|
||||
section.fields.filter((field) => field.binding.startsWith("offering.config.")),
|
||||
);
|
||||
const saveOffering = useMutation({
|
||||
mutationFn: (values: Record<string, unknown>) =>
|
||||
editingOffering
|
||||
? client.updateAdminOffering(editingOffering.id, values)
|
||||
: client.createAdminOffering(values as Omit<Offering, "id" | "brandId">),
|
||||
mutationFn: (values: Record<string, unknown>) => {
|
||||
const fields = Object.fromEntries(
|
||||
Object.entries(values).filter(([key]) => key !== "config" && !key.startsWith("offering:")),
|
||||
);
|
||||
const config = applyPresentationValues(
|
||||
editingOffering?.config,
|
||||
[{ key: "offering", fields: templateFields }],
|
||||
values,
|
||||
);
|
||||
const body = { ...fields, config };
|
||||
return editingOffering
|
||||
? client.updateAdminOffering(editingOffering.id, body)
|
||||
: client.createAdminOffering(body as Omit<Offering, "id" | "brandId">);
|
||||
},
|
||||
onSuccess: () => {
|
||||
message.success("业务提供项已保存");
|
||||
setOfferingOpen(false);
|
||||
@@ -72,16 +99,33 @@ export function ServicesPage() {
|
||||
coverUrl: item.coverUrl ?? "",
|
||||
sortOrder: item.sortOrder,
|
||||
isEnabled: item.isEnabled,
|
||||
...Object.fromEntries(
|
||||
templateFields.map((field) => [
|
||||
presentationFieldName("offering", field.key),
|
||||
readPresentationValue(item.config, field.binding, field),
|
||||
]),
|
||||
),
|
||||
}
|
||||
: { kind: "service", isEnabled: true, sortOrder: 0 },
|
||||
: {
|
||||
kind: "service",
|
||||
isEnabled: true,
|
||||
sortOrder: 0,
|
||||
...Object.fromEntries(
|
||||
templateFields.map((field) => [
|
||||
presentationFieldName("offering", field.key),
|
||||
field.input === "list" ? [] : undefined,
|
||||
]),
|
||||
),
|
||||
},
|
||||
);
|
||||
};
|
||||
return (
|
||||
<QueryState
|
||||
loading={offerings.isLoading}
|
||||
error={offerings.error}
|
||||
loading={offerings.isLoading || workspace.isLoading}
|
||||
error={offerings.error || workspace.error}
|
||||
onRetry={() => {
|
||||
void offerings.refetch();
|
||||
void workspace.refetch();
|
||||
}}
|
||||
>
|
||||
<AdminPageHeader
|
||||
@@ -229,6 +273,30 @@ export function ServicesPage() {
|
||||
]}
|
||||
/>
|
||||
</Form.Item>
|
||||
{templateFields.map((field) =>
|
||||
field.input === "list" ? (
|
||||
<TemplateListField
|
||||
key={field.key}
|
||||
field={field}
|
||||
name={presentationFieldName("offering", field.key)}
|
||||
/>
|
||||
) : (
|
||||
<Form.Item
|
||||
key={field.key}
|
||||
name={presentationFieldName("offering", field.key)}
|
||||
label={field.label}
|
||||
help={field.help}
|
||||
>
|
||||
{field.input === "textarea" ? (
|
||||
<Input.TextArea rows={3} />
|
||||
) : field.input === "image" ? (
|
||||
<ImageUrlField />
|
||||
) : (
|
||||
<Input />
|
||||
)}
|
||||
</Form.Item>
|
||||
),
|
||||
)}
|
||||
</Form>
|
||||
</Modal>
|
||||
</QueryState>
|
||||
|
||||
@@ -14,11 +14,16 @@ import {
|
||||
message,
|
||||
} from "antd";
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import type { TrustContent, TrustContentInput } from "@personal-brand/api-client";
|
||||
import type {
|
||||
TemplateAdminField,
|
||||
TrustContent,
|
||||
TrustContentInput,
|
||||
} from "@personal-brand/api-client";
|
||||
import { useState } from "react";
|
||||
import { createApiClient } from "../../lib/api.js";
|
||||
import { AdminPageHeader } from "../../components/AdminPageHeader.js";
|
||||
import { ImageUrlField } from "../../components/ImageUrlField.js";
|
||||
import { TemplateListField } from "../../components/TemplateListField.js";
|
||||
import { MutationError, QueryState } from "../../components/QueryState.js";
|
||||
|
||||
const types = [
|
||||
@@ -59,6 +64,7 @@ type TrustTypeOption = {
|
||||
help?: string;
|
||||
placeholder?: string;
|
||||
required?: boolean;
|
||||
configFields?: TemplateAdminField[];
|
||||
};
|
||||
const fallbackTypes: TrustTypeOption[] = types.map(({ value, label, help }) => ({
|
||||
type: value,
|
||||
@@ -86,7 +92,19 @@ export function TrustContentPage() {
|
||||
? workspace.data.templateContent.trustContentTypes
|
||||
: fallbackTypes;
|
||||
const selectedTypeMeta = availableTypes.find((item) => item.type === selectedType);
|
||||
const contentByType = new Map((query.data ?? []).map((item) => [item.type, item]));
|
||||
const contentByType = new Map<string, TrustContent>();
|
||||
for (const item of query.data ?? []) {
|
||||
const current = contentByType.get(item.type);
|
||||
const hasConfig = Object.keys(item.config ?? {}).length > 0;
|
||||
const currentHasConfig = Object.keys(current?.config ?? {}).length > 0;
|
||||
if (
|
||||
!current ||
|
||||
(item.isPublished && !current.isPublished) ||
|
||||
(item.isPublished === current.isPublished && hasConfig && !currentHasConfig)
|
||||
) {
|
||||
contentByType.set(item.type, item);
|
||||
}
|
||||
}
|
||||
const firstMissingRequiredType =
|
||||
availableTypes.find((item) => item.required && !contentByType.has(item.type))?.type ??
|
||||
availableTypes[0]?.type ??
|
||||
@@ -121,10 +139,11 @@ export function TrustContentPage() {
|
||||
title: item.title,
|
||||
...(item.summary ? { summary: item.summary } : {}),
|
||||
content: item.content,
|
||||
config: item.config ?? {},
|
||||
...(item.coverUrl ? { coverUrl: item.coverUrl } : {}),
|
||||
isPublished: item.isPublished,
|
||||
}
|
||||
: { type: type ?? firstMissingRequiredType, isPublished: false },
|
||||
: { type: type ?? firstMissingRequiredType, isPublished: false, config: {} },
|
||||
);
|
||||
};
|
||||
return (
|
||||
@@ -266,6 +285,21 @@ export function TrustContentPage() {
|
||||
placeholder={selectedTypeMeta?.placeholder ?? "例如:\n第一项内容\n第二项内容"}
|
||||
/>
|
||||
</Form.Item>
|
||||
{(!editing || contentByType.get(selectedType)?.id === editing.id) &&
|
||||
selectedTypeMeta?.configFields?.map((field) =>
|
||||
field.input === "list" ? (
|
||||
<TemplateListField key={field.key} field={field} name={["config", field.key]} />
|
||||
) : (
|
||||
<Form.Item
|
||||
key={field.key}
|
||||
name={["config", field.key]}
|
||||
label={field.label}
|
||||
help={field.help}
|
||||
>
|
||||
{field.input === "textarea" ? <Input.TextArea rows={3} /> : <Input />}
|
||||
</Form.Item>
|
||||
),
|
||||
)}
|
||||
<Form.Item
|
||||
name="coverUrl"
|
||||
label="配图地址"
|
||||
|
||||
Reference in new issue
Block a user