From f9ca5b1fead2da8675795c106c12745db1f8cb63 Mon Sep 17 00:00:00 2001 From: que01 Date: Tue, 22 Sep 2026 17:23:52 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=BE=93=E5=87=BA=E7=BB=9F=E4=B8=80?= =?UTF-8?q?=E8=BF=90=E8=A1=8C=E6=97=B6=E5=B7=A5=E4=BD=9C=E7=A9=BA=E9=97=B4?= =?UTF-8?q?=E5=A5=91=E7=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/api/package.json | 1 + apps/api/src/main.ts | 137 ++++++++++++++++++++++++++++-- docs/template-runtime-progress.md | 5 +- pnpm-lock.yaml | 3 + 4 files changed, 138 insertions(+), 8 deletions(-) diff --git a/apps/api/package.json b/apps/api/package.json index fc097ad..48fa8d1 100644 --- a/apps/api/package.json +++ b/apps/api/package.json @@ -21,6 +21,7 @@ "@nestjs/core": "^11.0.11", "@nestjs/platform-fastify": "^11.0.11", "@personal-brand/config": "workspace:^", + "@personal-brand/types": "workspace:^", "@personal-brand/validation": "workspace:^", "@prisma/client": "^6.2.1", "bcryptjs": "^2.4.3", diff --git a/apps/api/src/main.ts b/apps/api/src/main.ts index e72dac4..c6e6e02 100644 --- a/apps/api/src/main.ts +++ b/apps/api/src/main.ts @@ -42,6 +42,12 @@ import { ApiExceptionFilter, RequestIdMiddleware } from "./http.js"; import { getBrandTemplate, resolveCapabilities } from "@personal-brand/config"; import { AdminWorkspaceController } from "./admin-workspace.controller.js"; import { TemplateWorkspaceService } from "./template-workspace.service.js"; +import type { + RuntimeInquiryField, + RuntimeOffering, + RuntimeWorkspace, + RuntimeModule, +} from "@personal-brand/types"; @Controller("health") export class HealthController { @@ -132,15 +138,93 @@ export class PublicWorkspaceController { ]); const templateKey = installation?.templateKey ?? brand?.templateKey ?? "personal"; const template = getBrandTemplate(templateKey); - const runtimeModules = modules.map((module) => ({ + const runtimeModules: RuntimeModule[] = modules.map((module) => ({ key: module.type, - type: module.type, - title: module.title, - schemaVersion: "1.0", - sortOrder: module.sortOrder, - config: module.config, + type: this.moduleType(module.type), + version: "1.0.0", + propsSchema: `module.${module.type}.v1`, + dataSource: module.type, + config: this.jsonObject(module.config), + source: + module.source === "custom" ? "custom" : module.source === "template" ? "template" : "core", })); + const runtimeOfferings: RuntimeOffering[] = offerings.map((item) => { + const config = this.jsonObject(item.config); + const sections = Array.isArray(config.sections) ? config.sections : []; + return { + id: item.id, + name: item.name, + summary: item.summary ?? item.description ?? "", + enabled: item.isEnabled, + schemaKey: typeof config.schemaKey === "string" ? config.schemaKey : "offering.default", + schemaVersion: typeof config.schemaVersion === "string" ? config.schemaVersion : "1.0.0", + sections: sections + .filter((section): section is Record => + Boolean(section && typeof section === "object"), + ) + .map((section) => ({ + key: typeof section.key === "string" ? section.key : "section", + type: this.sectionType(section.type), + config: this.jsonObject(section.config), + })), + }; + }); + const inquiryFields: RuntimeInquiryField[] = this.jsonArray( + inquiryForm?.fieldsSnapshot, + ).flatMap((field) => { + if (!field || typeof field !== "object") return []; + const value = field as Record; + if (typeof value.key !== "string" || typeof value.label !== "string") return []; + const type = [ + "text", + "tel", + "number", + "select", + "radio", + "checkbox", + "textarea", + "date", + ].includes(String(value.type)) + ? (value.type as RuntimeInquiryField["type"]) + : "text"; + return [{ key: value.key, label: value.label, type, required: value.required === true }]; + }); + const runtimeWorkspace: RuntimeWorkspace = { + schemaVersion: "1.0.0", + brand: (brand ?? {}) as Record, + navigation: { + tabs: [ + { key: "home", label: "首页", page: "pages/index/index" }, + { key: "offerings", label: "案例与体系", page: "pages/services/index" }, + { key: "profile", label: "主理人与我", page: "pages/bookings/index" }, + ], + }, + modules: runtimeModules, + offerings: runtimeOfferings, + inquiryForms: inquiryForm + ? [ + { + key: inquiryForm.id, + version: String(inquiryForm.version), + purpose: this.formPurpose(inquiryForm.purpose), + target: inquiryForm.targetType === "offering" ? "offering" : "brand", + submissionAction: this.submissionAction(inquiryForm.submissionAction), + fields: inquiryFields, + }, + ] + : [], + capabilities: (template ? resolveCapabilities(template.capabilities) : []).map( + (capability) => ({ + key: capability.key, + version: capability.version, + config: {}, + status: "enabled" as const, + }), + ), + theme: { colors: { background: "#F7F5F1", foreground: "#252525", accent: "#8A6846" } }, + }; return { + ...runtimeWorkspace, contractVersion: "1.0", brand, template: template @@ -159,6 +243,47 @@ export class PublicWorkspaceController { inquiryForm, }; } + private jsonObject(value: unknown): Record { + return value && typeof value === "object" && !Array.isArray(value) + ? (value as Record) + : {}; + } + private jsonArray(value: unknown): Prisma.JsonValue[] { + return Array.isArray(value) ? value : []; + } + private moduleType(value: string): RuntimeModule["type"] { + const known: RuntimeModule["type"][] = [ + "brand-hero", + "founder-card", + "trust-proof", + "offering-list", + "offering-detail", + "gallery", + "timeline", + "faq", + "contact", + "inquiry", + "records", + "custom", + ]; + return ( + known.includes(value as RuntimeModule["type"]) ? value : "custom" + ) as RuntimeModule["type"]; + } + private sectionType(value: unknown): RuntimeOffering["sections"][number]["type"] { + const known = ["text", "image-list", "timeline", "faq", "specification"] as const; + return known.includes(value as (typeof known)[number]) + ? (value as (typeof known)[number]) + : "text"; + } + private formPurpose(value: string): RuntimeWorkspace["inquiryForms"][number]["purpose"] { + return value === "booking" || value === "quote-request" ? value : "inquiry"; + } + private submissionAction( + value: string, + ): RuntimeWorkspace["inquiryForms"][number]["submissionAction"] { + return value === "create-booking" || value === "create-quote" ? value : "create-inquiry"; + } } @Controller("public/offerings") diff --git a/docs/template-runtime-progress.md b/docs/template-runtime-progress.md index a833150..defbdf4 100644 --- a/docs/template-runtime-progress.md +++ b/docs/template-runtime-progress.md @@ -19,7 +19,8 @@ - [x] 新增共享 `TemplateManifest`、`ModuleManifest`、`CapabilityManifest` 类型。 - [x] 新增 `RuntimeWorkspace`、Offering、Inquiry 和 Capability 运行时类型。 - [x] 新增模板声明的基础校验函数。 -- [ ] 在 API 增加运行时工作空间 DTO 和公开接口。 +- [x] 在 API 增加运行时工作空间公开接口字段(`/public/workspace`)。 +- [x] API 工作空间输出统一导航、模块、Offering、表单和能力结构。 - [ ] 增加 API 授权、品牌隔离和 schema 版本测试。 - [ ] 让 Admin 与小程序读取同一运行时契约。 @@ -33,4 +34,4 @@ ## 当前交付门槛 -在 Phase 1 的 API 契约、测试和客户端消费完成前,不新增第二个行业页面,也不继续扩展行业专属固定 WXML。 +在 Phase 1 的 API 契约、测试和客户端消费完成前,不新增第二个行业页面,也不继续扩展行业专属固定 WXML。当前 API 契约已建立,下一步优先补测试和客户端消费。 diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 0bf42b6..3bb0aa7 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -78,6 +78,9 @@ importers: '@personal-brand/config': specifier: workspace:^ version: link:../../packages/config + '@personal-brand/types': + specifier: workspace:^ + version: link:../../packages/types '@personal-brand/validation': specifier: workspace:^ version: link:../../packages/validation