diff --git a/apps/api/src/modules/admin/admin-miniapp-release.controller.ts b/apps/api/src/modules/admin/admin-miniapp-release.controller.ts index dff2faa..309157a 100644 --- a/apps/api/src/modules/admin/admin-miniapp-release.controller.ts +++ b/apps/api/src/modules/admin/admin-miniapp-release.controller.ts @@ -12,6 +12,7 @@ import type { FastifyRequest } from "fastify"; import { existsSync } from "node:fs"; import { resolve } from "node:path"; import { Prisma } from "@prisma/client"; +import { getTemplatePackageManifest, templatePackageRegistry } from "@personal-brand/config"; import { AdminAccessService } from "../../admin-access.service.js"; import { PrismaService } from "../../prisma.service.js"; import { AdminMiniAppReleaseService } from "./admin-miniapp-release.service.js"; @@ -25,6 +26,16 @@ import { const MINIAPP_ARTIFACT_ROOT = process.env.MINIAPP_ARTIFACT_ROOT ?? "apps/taro/dist"; +function miniAppTemplateKey(industryTemplateKey: string): string | null { + return ( + getTemplatePackageManifest(industryTemplateKey)?.packageKey ?? + templatePackageRegistry.find((item) => + item.supportedIndustryTemplates.includes(industryTemplateKey), + )?.packageKey ?? + null + ); +} + interface MiniprogramCi { Project: new (options: { projectPath: string; @@ -104,7 +115,7 @@ export class AdminMiniAppReleaseController { ...this.jsonObject(body.build), } as Prisma.InputJsonObject, ...(credential ? { credential } : {}), - templateKey: current.templateKey ?? process.env.MINIAPP_TEMPLATE_KEY ?? "food", + templateKey: current.templateKey ?? null, } satisfies Prisma.InputJsonObject; await this.prisma.brandProfile.update({ where: { id: identity.brandId }, @@ -146,7 +157,7 @@ export class AdminMiniAppReleaseController { throw new BadRequestException("当前小程序发布任务不能重复上传"); const brand = await this.prisma.brandProfile.findUniqueOrThrow({ where: { id: identity.brandId }, - select: { miniAppSettings: true }, + select: { miniAppSettings: true, templateKey: true }, }); const settings = this.jsonObject(brand.miniAppSettings); const appid = @@ -156,13 +167,13 @@ export class AdminMiniAppReleaseController { const privateKey = settings.credential ? decryptMiniAppCredential(settings.credential) : this.requiredEnv("WECHAT_UPLOAD_PRIVATE_KEY"); - const templateKey = - typeof settings.templateKey === "string" && settings.templateKey - ? settings.templateKey - : this.requiredEnv("MINIAPP_TEMPLATE_KEY"); + const templateKey = miniAppTemplateKey(brand.templateKey); + if (!templateKey) throw new BadRequestException("当前品牌模板没有可上传的小程序包"); const projectPath = resolve(MINIAPP_ARTIFACT_ROOT, templateKey, "weapp"); if (!existsSync(projectPath) || !existsSync(resolve(projectPath, "app.json"))) - throw new BadRequestException("小程序构建产物不存在"); + throw new BadRequestException( + `小程序构建产物不存在:${projectPath},请先执行对应模板的微信小程序构建`, + ); const ci = (await import("miniprogram-ci")) as unknown as MiniprogramCi; const project = new ci.Project({ projectPath,