feat: 支持小程序上传配置加密存储

This commit is contained in:
que01 committed 2026-10-13 17:28:40 +08:00
1 parent 526a9db789
commit 3d0252d187
10 files changed
+257 -12

No files matched your search

@@ -11,9 +11,12 @@ import {
import type { FastifyRequest } from "fastify";
import { existsSync } from "node:fs";
import { resolve } from "node:path";
import { Prisma } from "@prisma/client";
import { AdminAccessService } from "../../admin-access.service.js";
import { PrismaService } from "../../prisma.service.js";
import { AdminMiniAppReleaseService } from "./admin-miniapp-release.service.js";
import { UpdateMiniAppSettingsDto } from "./admin-miniapp-settings.dto.js";
import { decryptMiniAppCredential, encryptMiniAppCredential } from "./miniapp-credential-crypto.js";
import {
CreateMiniAppReleaseDto,
UpdateMiniAppReleaseStatusDto,
@@ -44,6 +47,65 @@ export class AdminMiniAppReleaseController {
private readonly prisma: PrismaService,
) {}
@Get("settings")
async settings(@Req() request: FastifyRequest): Promise<unknown> {
const identity = await this.identity(request);
const brand = await this.prisma.brandProfile.findUnique({
where: { id: identity.brandId },
select: { miniAppSettings: true },
});
const settings = this.jsonObject(brand?.miniAppSettings);
return {
appId: "",
artifactPath: "",
build: {},
...settings,
privateKey: undefined,
credential: settings.credential ? { algorithm: "aes-256-gcm" } : null,
hasStoredCredentials: Boolean(settings.credential),
};
}
@Patch("settings")
async updateSettings(
@Req() request: FastifyRequest,
@Body() body: UpdateMiniAppSettingsDto & { privateKey?: string },
): Promise<unknown> {
const identity = await this.identity(request);
await this.access.requireRole(request, ["owner", "admin", "platform-admin"]);
if (body.privateKey !== undefined && !body.privateKey.startsWith("-----BEGIN"))
throw new BadRequestException("上传私钥格式无效");
const brand = await this.prisma.brandProfile.findUniqueOrThrow({
where: { id: identity.brandId },
select: { miniAppSettings: true },
});
const current = this.jsonObject(brand.miniAppSettings);
const credential =
body.privateKey === undefined
? current.credential
: encryptMiniAppCredential(body.privateKey);
const settings = {
...current,
appId: body.appId ?? current.appId ?? "",
artifactPath: body.artifactPath ?? current.artifactPath ?? "",
build: {
...this.jsonObject(current.build),
...this.jsonObject(body.build),
} as Prisma.InputJsonObject,
...(credential ? { credential } : {}),
} satisfies Prisma.InputJsonObject;
await this.prisma.brandProfile.update({
where: { id: identity.brandId },
data: { miniAppSettings: settings },
});
return {
appId: settings.appId,
artifactPath: settings.artifactPath,
build: settings.build,
hasStoredCredentials: Boolean(settings.credential),
};
}
@Get()
list(@Req() request: FastifyRequest): Promise<unknown[]> {
return this.identity(request).then(({ brandId }) => this.releases.list(brandId));
@@ -68,10 +130,22 @@ export class AdminMiniAppReleaseController {
});
if (!task) throw new BadRequestException("小程序发布任务不存在");
if (task.status !== "built") throw new BadRequestException("当前小程序发布任务不能重复上传");
const appid = this.requiredEnv("WECHAT_APP_ID");
const privateKey = this.requiredEnv("WECHAT_UPLOAD_PRIVATE_KEY");
const artifactPath = process.env.WEAPP_ARTIFACT_PATH;
if (!artifactPath) throw new BadRequestException("WEAPP_ARTIFACT_PATH 未配置");
const brand = await this.prisma.brandProfile.findUniqueOrThrow({
where: { id: identity.brandId },
select: { miniAppSettings: true },
});
const settings = this.jsonObject(brand.miniAppSettings);
const appid =
typeof settings.appId === "string" && settings.appId
? settings.appId
: this.requiredEnv("WECHAT_APP_ID");
const privateKey = settings.credential
? decryptMiniAppCredential(settings.credential)
: this.requiredEnv("WECHAT_UPLOAD_PRIVATE_KEY");
const artifactPath =
typeof settings.artifactPath === "string" && settings.artifactPath
? settings.artifactPath
: this.requiredEnv("WEAPP_ARTIFACT_PATH");
const projectPath = resolve(artifactPath);
if (!existsSync(projectPath) || !existsSync(resolve(projectPath, "app.json")))
throw new BadRequestException("小程序构建产物不存在");
@@ -126,4 +200,10 @@ export class AdminMiniAppReleaseController {
if (!value) throw new BadRequestException(`${name} 未配置`);
return value;
}
private jsonObject(value: unknown): Record<string, unknown> {
return value && typeof value === "object" && !Array.isArray(value)
? (value as Record<string, unknown>)
: {};
}
}