feat: 按视觉模板 manifest 驱动小程序运行时

This commit is contained in:
que01 committed 2026-09-23 14:26:39 +08:00
1 parent 031c81a7c1
commit ce958fc1ae
17 files changed
+149 -26

No files matched your search

@@ -5,6 +5,7 @@ import { PublicBrandContextService } from "./public-brand-context.service.js";
import {
getBrandTemplate,
getVisualTemplate,
getVisualTemplateManifest,
resolveCapabilities,
capabilityPlugins,
} from "@personal-brand/config";
@@ -98,6 +99,9 @@ export class PublicWorkspaceController {
: "linjing-editorial";
const visualTemplate =
getVisualTemplate(visualTemplateKey) ?? getVisualTemplate("linjing-editorial");
const visualTemplateManifest =
getVisualTemplateManifest(visualTemplateKey) ??
getVisualTemplateManifest("linjing-editorial");
const runtimeBrand = {
...(brand ?? {}),
...brandConfig,
@@ -223,6 +227,7 @@ export class PublicWorkspaceController {
variants: visualTemplate.variants,
}
: null,
visualTemplateManifest,
capabilities: runtimeCapabilities,
runtimeCapabilities,
modules,
+26 -4
View File
@@ -1,6 +1,6 @@
import { BadRequestException, Injectable } from "@nestjs/common";
import { Prisma } from "@prisma/client";
import { getBrandTemplate } from "@personal-brand/config";
import { getBrandTemplate, getVisualTemplateManifest } from "@personal-brand/config";
import { PrismaService } from "./prisma.service.js";
@Injectable()
@@ -9,20 +9,42 @@ export class TemplateWorkspaceService {
async install(brandId: string, templateKey: "personal" | "food" | "doors") {
const template = getBrandTemplate(templateKey);
if (!template) throw new BadRequestException("行业模板不存在");
const existing = await this.prisma.brandTemplateInstallation.findFirst({ where: { brandId } });
const [existing, brand] = await Promise.all([
this.prisma.brandTemplateInstallation.findFirst({ where: { brandId } }),
this.prisma.brandProfile.findUnique({ where: { id: brandId }, select: { config: true } }),
]);
if (existing)
throw new BadRequestException(
existing.templateKey === templateKey
? "该模板已经安装"
: "品牌已经安装主模板,请先完成模板切换流程",
);
const brandConfig =
brand?.config && typeof brand.config === "object" && !Array.isArray(brand.config)
? (brand.config as Record<string, unknown>)
: {};
const visualTemplateKey =
typeof brandConfig.visualTemplateKey === "string"
? brandConfig.visualTemplateKey
: templateKey === "doors"
? "linjing-editorial"
: "minimal-neutral";
const visualManifest = getVisualTemplateManifest(visualTemplateKey);
if (!visualManifest) throw new BadRequestException("视觉模板不存在");
const templatePackageSnapshot = {
schemaVersion: "1.0",
version: template.version,
industryTemplate: template,
visualTemplate: visualManifest,
capabilities: template.capabilities,
};
return this.prisma.$transaction(async (tx) => {
const installation = await tx.brandTemplateInstallation.create({
data: {
brandId,
templateKey,
templateVersion: template.version,
configSnapshot: template as unknown as Prisma.InputJsonValue,
configSnapshot: templatePackageSnapshot as unknown as Prisma.InputJsonValue,
},
});
await tx.brandModule.createMany({
@@ -46,7 +68,7 @@ export class TemplateWorkspaceService {
required,
})),
});
return { installation, template };
return { installation, template, visualTemplate: visualManifest };
});
}
private async installation(brandId: string) {
+8
View File
@@ -284,6 +284,8 @@ test("admin can install a template and public workspace exposes enabled configur
});
assert.equal(installed.statusCode, 201);
assert.equal(JSON.parse(installed.body).template.key, "personal");
assert.equal(JSON.parse(installed.body).visualTemplate.key, "minimal-neutral");
assert.equal(JSON.parse(installed.body).installation.configSnapshot.schemaVersion, "1.0");
try {
const previewUnauthorized = await request("/admin/workspace/template/preview");
assert.equal(previewUnauthorized.statusCode, 401);
@@ -433,6 +435,12 @@ test("admin visual template selection is reflected in public runtime workspace",
assert.equal(body.visualTemplate.version, "1.0.0");
assert.equal(body.theme.colors.accent, "#4B5563");
assert.equal(body.visualTemplate.variants.hero, "minimal");
assert.equal(body.visualTemplateManifest.key, "minimal-neutral");
assert.equal(body.visualTemplateManifest.brandFirst, true);
assert.deepEqual(
body.visualTemplateManifest.pages.map((page) => page.key),
["home", "offerings", "detail", "inquiry", "success", "records", "profile"],
);
} finally {
await request("/admin/config/brand", {
method: "PATCH",
@@ -1,5 +1,5 @@
Component({
properties: { hero: { type: Object, value: {} } },
properties: { hero: { type: Object, value: {} }, variant: { type: String, value: "default" } },
methods: {
explore() {
this.triggerEvent("explore");
@@ -1 +1,3 @@
Component({ properties: { brand: { type: Object, value: {} } } });
Component({
properties: { brand: { type: Object, value: {} }, variant: { type: String, value: "default" } },
});
@@ -1,5 +1,8 @@
Component({
properties: { offerings: { type: Array, value: [] } },
properties: {
offerings: { type: Array, value: [] },
variant: { type: String, value: "default" },
},
methods: {
open(e) {
this.triggerEvent("open", { id: e.currentTarget.dataset.id });
@@ -1 +1,3 @@
Component({ properties: { content: { type: Object, value: {} } } });
Component({
properties: { content: { type: Object, value: {} }, variant: { type: String, value: "default" } },
});
@@ -1 +1,3 @@
Component({ properties: { content: { type: Object, value: {} } } });
Component({
properties: { content: { type: Object, value: {} }, variant: { type: String, value: "default" } },
});
+2
View File
@@ -5,6 +5,7 @@ Page({
trustContent: [],
offerings: [],
modules: [],
moduleVariants: {},
layout: [],
theme: {},
hero: {
@@ -35,6 +36,7 @@ Page({
trustContent: workspace.trustContent || [],
offerings: activeOfferings,
modules: workspace.modules || [],
moduleVariants: workspace.moduleVariants || {},
layout,
theme: workspace.theme || {},
hero: {
+5 -5
View File
@@ -1,9 +1,9 @@
<view class="page">
<block wx:for="{{layout}}" wx:key="*this" wx:for-item="moduleKey">
<founder-card wx:if="{{moduleKey === 'founder-card'}}" brand="{{brand}}" />
<brand-hero wx:if="{{moduleKey === 'brand-hero'}}" hero="{{hero}}" bind:explore="goWorks" />
<trust-proof wx:if="{{moduleKey === 'trust-proof'}}" content="{{trustContent}}" />
<trust-manifesto wx:if="{{moduleKey === 'trust-manifesto'}}" content="{{trustContent}}" />
<offering-list wx:if="{{moduleKey === 'offering-list'}}" offerings="{{offerings}}" bind:open="selectOffering" bind:more="goWorks" />
<founder-card wx:if="{{moduleKey === 'founder-card'}}" brand="{{brand}}" variant="{{moduleVariants[moduleKey]}}" />
<brand-hero wx:if="{{moduleKey === 'brand-hero'}}" hero="{{hero}}" variant="{{moduleVariants[moduleKey]}}" bind:explore="goWorks" />
<trust-proof wx:if="{{moduleKey === 'trust-proof'}}" content="{{trustContent}}" variant="{{moduleVariants[moduleKey]}}" />
<trust-manifesto wx:if="{{moduleKey === 'trust-manifesto'}}" content="{{trustContent}}" variant="{{moduleVariants[moduleKey]}}" />
<offering-list wx:if="{{moduleKey === 'offering-list'}}" offerings="{{offerings}}" variant="{{moduleVariants[moduleKey]}}" bind:open="selectOffering" bind:more="goWorks" />
</block>
</view>
+42 -4
View File
@@ -44,19 +44,57 @@ function resolveModule(module) {
};
}
const MANIFEST_TYPE_ALIASES = {
"brand-identity": "founder-card",
hero: "brand-hero",
trust: "trust-proof",
"inquiry-form": "inquiry",
"success-summary": "success-summary",
"records-list": "records",
};
function resolveManifest(workspace) {
const manifest = workspace && workspace.visualTemplateManifest;
if (!manifest || !Array.isArray(manifest.pages)) return null;
const pages = {};
for (const page of manifest.pages) {
pages[page.key] = (page.modules || []).map((module) => ({
...module,
type: MANIFEST_TYPE_ALIASES[module.type] || module.type,
variant: module.variant || "default",
}));
}
return { ...manifest, pages };
}
function resolveRuntime(workspace) {
const modules = Array.isArray(workspace && workspace.modules) ? workspace.modules : [];
const manifest = resolveManifest(workspace);
const manifestHome = manifest && manifest.pages.home;
const manifestModules = manifestHome || [];
const resolved = modules.map(resolveModule);
const home =
workspace && workspace.layout && Array.isArray(workspace.layout.home)
const resolvedTypes = new Set(resolved.map((item) => item.type));
for (const module of manifestModules) {
if (!resolvedTypes.has(module.type)) {
resolved.push(resolveModule(module));
resolvedTypes.add(module.type);
}
}
const home = manifestHome?.length
? manifestHome.map((item) => item.type)
: workspace && workspace.layout && Array.isArray(workspace.layout.home)
? workspace.layout.home
: resolved.map((item) => item.type);
const moduleVariants = {};
for (const item of manifestHome || []) moduleVariants[item.type] = item.variant;
return {
theme: workspace && workspace.theme ? workspace.theme : FALLBACK_THEME,
theme: manifest?.tokens || (workspace && workspace.theme ? workspace.theme : FALLBACK_THEME),
modules: resolved,
layout: { home: home.filter((key) => resolved.some((item) => item.type === key)) },
moduleVariants,
visualManifest: manifest,
capabilities: workspace && Array.isArray(workspace.capabilities) ? workspace.capabilities : [],
};
}
module.exports = { CORE_MODULES, FALLBACK_THEME, resolveRuntime };
module.exports = { CORE_MODULES, FALLBACK_THEME, resolveManifest, resolveRuntime };