fix: 完善工坊模板安装预览与详情样例
This commit is contained in:
1 parent
5313fd9d32
commit
50753db79d
16 files changed
+496
-26
No files matched your search
@@ -0,0 +1,63 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { PrismaService } from "../dist/prisma.service.js";
|
||||
import { TemplateWorkspaceService } from "../dist/template-workspace.service.js";
|
||||
|
||||
// 显式创建独立品牌;不注册模板样例,不导入内容,也不发布。
|
||||
const username = process.argv[2];
|
||||
if (!username)
|
||||
throw new Error("Usage: node --env-file=.env scripts/create-atelier-brand.mjs <admin-username>");
|
||||
const prisma = new PrismaService();
|
||||
try {
|
||||
const admin = await prisma.adminUser.findUniqueOrThrow({ where: { username } });
|
||||
assert.ok(admin.isEnabled && admin.role === "admin", "An enabled administrator is required");
|
||||
const slug = "atelier-studio";
|
||||
const existingBrands = await prisma.brandProfile.findMany({
|
||||
where: { slug: { not: slug } },
|
||||
orderBy: { id: "asc" },
|
||||
});
|
||||
const brand = await prisma.$transaction(async (tx) => {
|
||||
let current = await tx.brandProfile.findUnique({ where: { slug } });
|
||||
if (current) {
|
||||
assert.equal(current.templateKey, "personal", "Existing brand has a different industry");
|
||||
assert.equal(
|
||||
current.config?.visualTemplateKey,
|
||||
"atelier-solopreneur",
|
||||
"Existing brand has a different visual template",
|
||||
);
|
||||
} else {
|
||||
current = await tx.brandProfile.create({
|
||||
data: {
|
||||
slug,
|
||||
name: "独立工坊 · 品牌小程序",
|
||||
templateKey: "personal",
|
||||
config: { visualTemplateKey: "atelier-solopreneur" },
|
||||
},
|
||||
});
|
||||
}
|
||||
await tx.brandMembership.upsert({
|
||||
where: { adminUserId_brandId: { adminUserId: admin.id, brandId: current.id } },
|
||||
create: { adminUserId: admin.id, brandId: current.id, role: "owner" },
|
||||
update: {},
|
||||
});
|
||||
return current;
|
||||
});
|
||||
if (!(await prisma.brandTemplateInstallation.findFirst({ where: { brandId: brand.id } }))) {
|
||||
await new TemplateWorkspaceService(prisma).install(brand.id, "personal");
|
||||
}
|
||||
assert.deepEqual(
|
||||
await prisma.brandProfile.findMany({ where: { slug: { not: slug } }, orderBy: { id: "asc" } }),
|
||||
existingBrands,
|
||||
);
|
||||
console.log(
|
||||
JSON.stringify({
|
||||
slug,
|
||||
name: brand.name,
|
||||
template: "atelier-solopreneur",
|
||||
administrator: username,
|
||||
sampleImported: false,
|
||||
publishedByScript: false,
|
||||
}),
|
||||
);
|
||||
} finally {
|
||||
await prisma.$disconnect();
|
||||
}
|
||||
@@ -111,7 +111,8 @@ export class AdminSampleContentService {
|
||||
name: item.name,
|
||||
summary: item.summary,
|
||||
...(item.coverUrl ? { coverUrl: item.coverUrl } : {}),
|
||||
config: { categoryKey: item.categoryKey },
|
||||
...(item.priceText ? { priceText: item.priceText } : {}),
|
||||
config: { ...item.config, categoryKey: item.categoryKey } as Prisma.InputJsonValue,
|
||||
sortOrder,
|
||||
isEnabled: true,
|
||||
})),
|
||||
|
||||
@@ -2,6 +2,7 @@ import assert from "node:assert/strict";
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { after, before, test } from "node:test";
|
||||
import { PrismaClient } from "@prisma/client";
|
||||
import { getVisualTemplateManifest } from "@personal-brand/config";
|
||||
import { createApp } from "../dist/main.js";
|
||||
import { readIdentity, signIdentity } from "../dist/auth.js";
|
||||
import { WechatAuthService } from "../dist/wechat-auth.js";
|
||||
@@ -1189,6 +1190,12 @@ test("visual template compatibility is enforced by catalog, preview, and brand s
|
||||
const catalog = await request("/public/templates");
|
||||
assert.equal(catalog.statusCode, 200);
|
||||
const visuals = JSON.parse(catalog.body).visuals;
|
||||
for (const visual of visuals) {
|
||||
const manifest = getVisualTemplateManifest(visual.key);
|
||||
assert.ok(manifest, `Catalog visual must be installable: ${visual.key}`);
|
||||
assert.equal(manifest.key, visual.key);
|
||||
assert.deepEqual(manifest.supportedIndustryTemplates, visual.supportedIndustryTemplates);
|
||||
}
|
||||
assert.deepEqual(
|
||||
visuals.find((visual) => visual.key === "linjing-editorial").supportedIndustryTemplates,
|
||||
["personal", "doors"],
|
||||
@@ -3468,7 +3475,7 @@ test("atelier template imports selected package and preserves draft publication
|
||||
tagline: "独立开发者 · 品牌小程序构建",
|
||||
description: "专注个人品牌表达、内容管理与咨询承接。",
|
||||
config: {
|
||||
visualTemplateKey: "atelier-solopreneur",
|
||||
visualTemplateKey: "linjing-editorial",
|
||||
founder: "验收主理人",
|
||||
title: "产品设计 / 全栈开发",
|
||||
visualPresentation: { home: { subtitle: "保留已有品牌说明" } },
|
||||
@@ -3501,6 +3508,18 @@ test("atelier template imports selected package and preserves draft publication
|
||||
403,
|
||||
);
|
||||
}
|
||||
await json("/admin/config/brand", {
|
||||
method: "PATCH",
|
||||
headers,
|
||||
payload: { config: { visualTemplateKey: "atelier-solopreneur" } },
|
||||
});
|
||||
const installation = await json("/admin/workspace/install", {
|
||||
method: "POST",
|
||||
headers,
|
||||
payload: { templateKey: "personal" },
|
||||
});
|
||||
assert.equal(installation.visualTemplate.key, "atelier-solopreneur");
|
||||
assert.equal(installation.visualTemplate.tokens.colors.accent, "#183329");
|
||||
const preview = await json("/admin/workspace/sample-content/preview", { headers });
|
||||
assert.equal(preview.counts.fields, 5);
|
||||
await json("/admin/workspace/sample-content/import", { method: "POST", headers });
|
||||
@@ -3523,6 +3542,16 @@ test("atelier template imports selected package and preserves draft publication
|
||||
where: { brandId: brand.id },
|
||||
});
|
||||
assert.match(exampleOffering.coverUrl, /^\/assets\/cases\//);
|
||||
const sampleRuntime = await json("/admin/workspace/preview", { headers });
|
||||
for (const item of sampleRuntime.offerings) {
|
||||
assert.equal(item.presentation.categoryLabel, "案例展示");
|
||||
assert.equal(item.priceText, "按需求确认范围与报价");
|
||||
assert.ok(item.presentation.spaceType);
|
||||
assert.ok(item.presentation.details.background);
|
||||
assert.ok(item.presentation.details.designConcept);
|
||||
assert.ok(item.presentation.details.crafts.length);
|
||||
assert.ok(item.presentation.details.materialsList.length);
|
||||
}
|
||||
const offeringFields = [
|
||||
...new Map(
|
||||
manifest.admin.sections
|
||||
|
||||
@@ -11,7 +11,8 @@ export const routes = {
|
||||
};
|
||||
export type PageKey = keyof typeof routes;
|
||||
export function navigate(page: PageKey, params: Record<string, string> = {}) {
|
||||
const isTab = TEMPLATE_MANIFEST.navigation.some((tab) => tab.page === page);
|
||||
const isTab =
|
||||
!PREVIEW_ALL_TEMPLATES && TEMPLATE_MANIFEST.navigation.some((tab) => tab.page === page);
|
||||
if (isTab) {
|
||||
Taro.setStorageSync(`routeQuery:${page}`, params);
|
||||
void Taro.switchTab({ url: routes[page] });
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
export function resolvePreviewRenderer<T>(
|
||||
modules: Record<string, T>,
|
||||
config: unknown,
|
||||
): T | undefined {
|
||||
if (!config || typeof config !== "object" || Array.isArray(config)) return undefined;
|
||||
const key = (config as Record<string, unknown>).visualTemplateKey;
|
||||
if (typeof key !== "string" || !key) return undefined;
|
||||
const suffix = `/template-${key}/taro/index.tsx`;
|
||||
return Object.entries(modules).find(([path]) => path.endsWith(suffix))?.[1];
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import type { ComponentType } from "react";
|
||||
import type { TemplateProps } from "./template";
|
||||
import { resolvePreviewRenderer } from "./preview-renderer";
|
||||
|
||||
type TemplateRenderer = ComponentType<TemplateProps>;
|
||||
|
||||
@@ -8,8 +9,7 @@ const renderers = import.meta.glob<{
|
||||
}>("../../../../packages/template-*/taro/index.tsx", { eager: true });
|
||||
|
||||
export function TemplatePage(props: TemplateProps) {
|
||||
const suffix = `/template-${TEMPLATE_KEY}/taro/index.tsx`;
|
||||
const rendererModule = Object.entries(renderers).find(([path]) => path.endsWith(suffix))?.[1];
|
||||
const rendererModule = resolvePreviewRenderer(renderers, props.workspace.brand.config);
|
||||
if (!rendererModule)
|
||||
return <div className="runtime-state">当前模板没有可用的 H5 预览 renderer</div>;
|
||||
const Renderer = rendererModule.TemplatePage;
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
import assert from "node:assert/strict";
|
||||
import fs from "node:fs";
|
||||
import { test } from "node:test";
|
||||
import { resolvePreviewRenderer } from "../src/runtime/preview-renderer.ts";
|
||||
|
||||
const packages = new URL("../../../packages/", import.meta.url);
|
||||
const manifests = fs
|
||||
.readdirSync(packages)
|
||||
.filter((name) => fs.existsSync(new URL(`${name}/taro/index.tsx`, packages)))
|
||||
.map((name) => JSON.parse(fs.readFileSync(new URL(`${name}/template.json`, packages), "utf8")));
|
||||
const modules = Object.fromEntries(
|
||||
manifests.map((manifest) => [
|
||||
`../../../../packages/template-${manifest.packageKey}/taro/index.tsx`,
|
||||
{ key: manifest.packageKey },
|
||||
]),
|
||||
);
|
||||
|
||||
test("多模板预览随当前品牌配置切换所有真实模板,重复切换不保留旧 renderer", () => {
|
||||
for (const manifest of [...manifests, ...manifests.reverse()]) {
|
||||
assert.equal(
|
||||
resolvePreviewRenderer(modules, { visualTemplateKey: manifest.packageKey })?.key,
|
||||
manifest.packageKey,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
test("缺少或未知模板时不回落到首个模板", () => {
|
||||
for (const config of [
|
||||
undefined,
|
||||
null,
|
||||
[],
|
||||
{},
|
||||
{ visualTemplateKey: "missing" },
|
||||
{ visualTemplateKey: 1 },
|
||||
]) {
|
||||
assert.equal(resolvePreviewRenderer(modules, config), undefined);
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user