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

+22 -1
View File
@@ -45,7 +45,14 @@ export type TemplateAdminField = {
binding: string;
label: string;
help?: string;
input: "text" | "textarea" | "phone" | "image" | "number";
input: "text" | "textarea" | "phone" | "image" | "number" | "list";
itemFields?: Array<{
key: string;
label: string;
input?: "text" | "textarea";
required?: boolean;
}>;
itemValueKey?: string;
required?: boolean;
maxLength?: number;
placeholder?: string;
@@ -70,6 +77,7 @@ export type TemplateTrustContentType = {
help?: string;
placeholder?: string;
required?: boolean;
configFields?: TemplateAdminField[];
};
export type TemplateSampleContent = {
@@ -147,6 +155,12 @@ export function validateTemplatePackageManifest(value: unknown): string[] {
errors.push(`trustContentTypes.${index}.type is required`);
if (typeof item.label !== "string" || !item.label.trim())
errors.push(`trustContentTypes.${index}.label is required`);
for (const field of item.configFields ?? []) {
if (!field.key?.trim() || !field.binding?.startsWith("trust.config."))
errors.push(`trustContentTypes.${index}.configFields binding is invalid`);
if (field.input === "list" && !field.itemFields?.length)
errors.push(`trustContentTypes.${index}.configFields list requires itemFields`);
}
}
}
}
@@ -168,6 +182,13 @@ export function validateTemplatePackageManifest(value: unknown): string[] {
for (const field of section.fields ?? []) {
if (!field?.key?.trim() || !field.binding?.trim() || !field.label?.trim())
errors.push("every admin field requires key, binding and label");
if (field.input === "list" && !field.itemFields?.length)
errors.push("list admin fields require itemFields");
if (
field.itemValueKey &&
!field.itemFields?.some((item) => item.key === field.itemValueKey)
)
errors.push("list itemValueKey must match an itemField");
}
}
}
@@ -0,0 +1,128 @@
import assert from "node:assert/strict";
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import test from "node:test";
const packagesRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../..");
const runtimeRoot = path.resolve(packagesRoot, "../apps/miniapp");
function sourceFile(templateRoot, relativePath, extension) {
const local = path.join(templateRoot, "miniapp", `${relativePath}.${extension}`);
const runtime = path.join(runtimeRoot, `${relativePath}.${extension}`);
return fs.existsSync(local) ? local : fs.existsSync(runtime) ? runtime : null;
}
function renderedFields(templateRoot, entrypoint) {
const seen = new Set();
const fields = new Set();
const heroFields = new Set();
const profileFields = new Set();
const trustFields = new Set();
function visit(relativePath) {
if (seen.has(relativePath)) return;
seen.add(relativePath);
for (const extension of ["wxml", "js"]) {
const source = sourceFile(templateRoot, relativePath, extension);
if (!source) continue;
const content = fs.readFileSync(source, "utf8");
for (const match of content.matchAll(/(?<![.\w])presentation\.([A-Za-z][A-Za-z0-9]*)/g)) {
fields.add(match[1]);
}
for (const match of content.matchAll(/(?<![.\w])hero\.([A-Za-z][A-Za-z0-9]*)/g)) {
heroFields.add(match[1]);
}
for (const match of content.matchAll(/brand\.profilePresentation\.([A-Za-z][A-Za-z0-9]*)/g)) {
profileFields.add(match[1]);
}
for (const match of content.matchAll(/trustContent\.([A-Za-z][A-Za-z0-9]*)/g)) {
trustFields.add(match[1]);
}
}
const componentConfig = sourceFile(templateRoot, relativePath, "json");
if (!componentConfig) return;
const config = JSON.parse(fs.readFileSync(componentConfig, "utf8"));
for (const component of Object.values(config.usingComponents ?? {})) {
if (typeof component === "string" && component.startsWith("/components/")) {
visit(component.slice(1));
}
}
}
visit(entrypoint);
return { fields, heroFields, profileFields, trustFields, seen };
}
const sharedOfferingBindings = [
"offering.config.categoryLabel",
"offering.config.detailHero",
"offering.config.area",
"offering.config.series",
"offering.config.spaceType",
"offering.config.keywords",
"offering.config.details.background",
"offering.config.details.designConcept",
"offering.config.details.crafts",
"offering.config.details.materialsList",
];
for (const directory of fs
.readdirSync(packagesRoot)
.filter((name) => name.startsWith("template-"))) {
const templateRoot = path.join(packagesRoot, directory);
const manifestPath = path.join(templateRoot, "template.json");
if (!fs.existsSync(manifestPath)) continue;
test(`${directory} exposes rendered content through Admin bindings`, () => {
const manifest = JSON.parse(fs.readFileSync(manifestPath, "utf8"));
const bindings = new Set(
manifest.admin.sections.flatMap((section) => section.fields.map((field) => field.binding)),
);
for (const [pageKey, entrypoint] of Object.entries(manifest.entrypoints)) {
const { fields, heroFields, profileFields, trustFields, seen } = renderedFields(
templateRoot,
entrypoint,
);
for (const field of fields) {
assert.ok(
bindings.has(`brand.config.visualPresentation.${pageKey}.${field}`),
`${manifest.packageKey}: ${pageKey}.${field} is rendered but has no Admin field`,
);
}
for (const field of heroFields) {
const binding =
field === "image" ? "brand.config.heroImage" : `brand.config.homeHero.${field}`;
assert.ok(
bindings.has(binding),
`${manifest.packageKey}: ${pageKey} hero.${field} has no Admin field`,
);
}
for (const field of profileFields) {
assert.ok(
bindings.has(`brand.config.profilePresentation.${field}`),
`${manifest.packageKey}: profilePresentation.${field} has no Admin field`,
);
}
for (const field of trustFields) {
if (field === "bio") {
assert.ok(
manifest.trustContentTypes?.some((type) => type.type === "credential"),
`${manifest.packageKey}: biography has no editable trust content type`,
);
continue;
}
assert.ok(
manifest.trustContentTypes?.some((type) =>
type.configFields?.some(
(configField) => configField.binding === `trust.config.${field}`,
),
),
`${manifest.packageKey}: trustContent.${field} has no Admin field`,
);
}
if (seen.has("components/modules/offering-detail/offering-detail")) {
for (const binding of sharedOfferingBindings) {
assert.ok(bindings.has(binding), `${manifest.packageKey}: missing ${binding}`);
}
}
}
});
}
@@ -25,3 +25,37 @@ test("template contract rejects technical or incomplete admin declarations", ()
"every admin field requires key, binding and label",
]);
});
test("template manifests declare editable material content", () => {
const linjing = JSON.parse(
fs.readFileSync(path.join(packageRoot, "template-linjing-editorial/template.json"), "utf8"),
);
const acheng = JSON.parse(
fs.readFileSync(path.join(packageRoot, "template-acheng-campus-food/template.json"), "utf8"),
);
const bindings = (manifest) =>
new Set(
manifest.admin.sections.flatMap((section) => section.fields.map((field) => field.binding)),
);
for (const binding of [
"brand.config.homeHero.title",
"brand.config.profilePresentation.coverage",
"brand.config.profilePresentation.warranty",
"offering.config.area",
"offering.config.details.designConcept",
"brand.config.visualPresentation.offerings.closingText",
])
assert.ok(bindings(linjing).has(binding), binding);
for (const binding of [
"brand.config.visualPresentation.profile.coverageContent",
"brand.config.visualPresentation.offerings.menuTip",
"brand.config.visualPresentation.inquiry.promise",
])
assert.ok(bindings(acheng).has(binding), binding);
assert.deepEqual(
linjing.trustContentTypes
.find((item) => item.type === "credential")
.configFields.map((field) => field.key),
["stats", "pillars", "serviceFlow"],
);
});