fix: 恢复小程序上传配置并固定产物目录
This commit is contained in:
1 parent
ad949ab370
commit
26f1157083
5 files changed
+137
-21
No files matched your search
@@ -1,4 +1,4 @@
|
|||||||
import { useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||||
import {
|
import {
|
||||||
@@ -9,6 +9,7 @@ import {
|
|||||||
Empty,
|
Empty,
|
||||||
Input,
|
Input,
|
||||||
Modal,
|
Modal,
|
||||||
|
Form,
|
||||||
Select,
|
Select,
|
||||||
Space,
|
Space,
|
||||||
Spin,
|
Spin,
|
||||||
@@ -42,6 +43,11 @@ export function PublicationPage({ canManageTemplate }: { canManageTemplate: bool
|
|||||||
const [rollbackVersion, setRollbackVersion] = useState<number | null>(null);
|
const [rollbackVersion, setRollbackVersion] = useState<number | null>(null);
|
||||||
const [note, setNote] = useState("");
|
const [note, setNote] = useState("");
|
||||||
const [activeTab, setActiveTab] = useState("content");
|
const [activeTab, setActiveTab] = useState("content");
|
||||||
|
const [miniAppSettingsForm] = Form.useForm<{
|
||||||
|
appId: string;
|
||||||
|
artifactPath: string;
|
||||||
|
privateKey?: string;
|
||||||
|
}>();
|
||||||
const brand = useQuery({
|
const brand = useQuery({
|
||||||
queryKey: ["publication-brand"],
|
queryKey: ["publication-brand"],
|
||||||
queryFn: () => client.getAdminBrand(),
|
queryFn: () => client.getAdminBrand(),
|
||||||
@@ -75,6 +81,14 @@ export function PublicationPage({ canManageTemplate }: { canManageTemplate: bool
|
|||||||
queryKey: ["publication-miniapp-settings"],
|
queryKey: ["publication-miniapp-settings"],
|
||||||
queryFn: () => client.getAdminMiniAppSettings(),
|
queryFn: () => client.getAdminMiniAppSettings(),
|
||||||
});
|
});
|
||||||
|
useEffect(() => {
|
||||||
|
if (!miniAppSettings.data) return;
|
||||||
|
miniAppSettingsForm.setFieldsValue({
|
||||||
|
appId: miniAppSettings.data.appId,
|
||||||
|
artifactPath: miniAppSettings.data.artifactPath,
|
||||||
|
privateKey: "",
|
||||||
|
});
|
||||||
|
}, [miniAppSettings.data, miniAppSettingsForm]);
|
||||||
const refresh = () => {
|
const refresh = () => {
|
||||||
for (const key of [
|
for (const key of [
|
||||||
"publication-brand",
|
"publication-brand",
|
||||||
@@ -155,6 +169,18 @@ export function PublicationPage({ canManageTemplate }: { canManageTemplate: bool
|
|||||||
void miniAppReleases.refetch();
|
void miniAppReleases.refetch();
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
const saveMiniAppSettings = useMutation({
|
||||||
|
mutationFn: (values: { appId: string; artifactPath: string; privateKey?: string }) =>
|
||||||
|
client.updateAdminMiniAppSettings({
|
||||||
|
appId: values.appId,
|
||||||
|
artifactPath: values.artifactPath,
|
||||||
|
...(values.privateKey ? { privateKey: values.privateKey } : {}),
|
||||||
|
}),
|
||||||
|
onSuccess: () => {
|
||||||
|
message.success("小程序上传配置已保存");
|
||||||
|
void miniAppSettings.refetch();
|
||||||
|
},
|
||||||
|
});
|
||||||
const uploadMiniAppRelease = useMutation({
|
const uploadMiniAppRelease = useMutation({
|
||||||
mutationFn: (id: string) => client.uploadAdminMiniAppRelease(id),
|
mutationFn: (id: string) => client.uploadAdminMiniAppRelease(id),
|
||||||
onSuccess: () => {
|
onSuccess: () => {
|
||||||
@@ -175,9 +201,15 @@ export function PublicationPage({ canManageTemplate }: { canManageTemplate: bool
|
|||||||
void miniAppReleases.refetch();
|
void miniAppReleases.refetch();
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
const loading = [brand, workspace, catalog, readiness, releases, miniAppReleases].some(
|
const loading = [
|
||||||
(query) => query.isLoading,
|
brand,
|
||||||
);
|
workspace,
|
||||||
|
catalog,
|
||||||
|
readiness,
|
||||||
|
releases,
|
||||||
|
miniAppReleases,
|
||||||
|
miniAppSettings,
|
||||||
|
].some((query) => query.isLoading);
|
||||||
const error = [
|
const error = [
|
||||||
brand,
|
brand,
|
||||||
workspace,
|
workspace,
|
||||||
@@ -221,8 +253,52 @@ export function PublicationPage({ canManageTemplate }: { canManageTemplate: bool
|
|||||||
isEnabled: boolean;
|
isEnabled: boolean;
|
||||||
}>;
|
}>;
|
||||||
|
|
||||||
const contentTab = <Space direction="vertical" size={24} style={{ width: "100%" }}></Space>;
|
const contentTab = null;
|
||||||
const miniAppTab = <Space direction="vertical" size={24} style={{ width: "100%" }}></Space>;
|
const miniAppSettingsTab = (
|
||||||
|
<Space direction="vertical" size={24} style={{ width: "100%" }}>
|
||||||
|
<Card title="小程序上传配置">
|
||||||
|
<Typography.Paragraph type="secondary">
|
||||||
|
AppID 用于服务端调用微信官方上传工具;上传私钥保存前会使用 AES-256-GCM
|
||||||
|
加密。小程序产物目录由系统构建流程决定,不需要手动修改。
|
||||||
|
</Typography.Paragraph>
|
||||||
|
<Form
|
||||||
|
form={miniAppSettingsForm}
|
||||||
|
layout="vertical"
|
||||||
|
onFinish={(values) => saveMiniAppSettings.mutate(values)}
|
||||||
|
>
|
||||||
|
<Form.Item
|
||||||
|
name="appId"
|
||||||
|
label="微信 AppID"
|
||||||
|
rules={[{ required: true, message: "请输入微信 AppID" }]}
|
||||||
|
>
|
||||||
|
<Input maxLength={100} />
|
||||||
|
</Form.Item>
|
||||||
|
<Form.Item
|
||||||
|
name="privateKey"
|
||||||
|
label="上传私钥"
|
||||||
|
help={
|
||||||
|
miniAppSettings.data?.hasStoredCredentials
|
||||||
|
? "已加密保存。留空保持现有私钥,输入新值可替换。"
|
||||||
|
: "粘贴以 -----BEGIN 开头的微信上传私钥。"
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<Input.Password autoComplete="new-password" />
|
||||||
|
</Form.Item>
|
||||||
|
<Space>
|
||||||
|
<Button type="primary" loading={saveMiniAppSettings.isPending} htmlType="submit">
|
||||||
|
保存上传配置
|
||||||
|
</Button>
|
||||||
|
{miniAppSettings.data?.hasStoredCredentials ? (
|
||||||
|
<Tag color="green">已加密保存上传凭据</Tag>
|
||||||
|
) : (
|
||||||
|
<Tag color="warning">未配置上传私钥</Tag>
|
||||||
|
)}
|
||||||
|
</Space>
|
||||||
|
<MutationError error={saveMiniAppSettings.error} />
|
||||||
|
</Form>
|
||||||
|
</Card>
|
||||||
|
</Space>
|
||||||
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<QueryState loading={loading} error={error} onRetry={retry}>
|
<QueryState loading={loading} error={error} onRetry={retry}>
|
||||||
@@ -234,8 +310,17 @@ export function PublicationPage({ canManageTemplate }: { canManageTemplate: bool
|
|||||||
activeKey={activeTab}
|
activeKey={activeTab}
|
||||||
onChange={setActiveTab}
|
onChange={setActiveTab}
|
||||||
items={[
|
items={[
|
||||||
{ key: "content", label: "内容发布", children: contentTab },
|
{
|
||||||
{ key: "miniapp", label: "小程序发布", children: miniAppTab },
|
key: "content",
|
||||||
|
label: "内容发布",
|
||||||
|
children: contentTab,
|
||||||
|
},
|
||||||
|
{ key: "miniapp", label: "小程序发布", children: null },
|
||||||
|
{
|
||||||
|
key: "settings",
|
||||||
|
label: "小程序配置",
|
||||||
|
children: activeTab === "settings" ? miniAppSettingsTab : null,
|
||||||
|
},
|
||||||
]}
|
]}
|
||||||
/>
|
/>
|
||||||
{activeTab === "content" && (
|
{activeTab === "content" && (
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import "reflect-metadata";
|
import "reflect-metadata";
|
||||||
import { Controller, Get, Module, ValidationPipe } from "@nestjs/common";
|
import { Controller, Get, Module, ValidationPipe } from "@nestjs/common";
|
||||||
|
import { APP_GUARD } from "@nestjs/core";
|
||||||
import type { NestFastifyApplication } from "@nestjs/platform-fastify";
|
import type { NestFastifyApplication } from "@nestjs/platform-fastify";
|
||||||
import { PrismaService } from "./prisma.service.js";
|
import { PrismaService } from "./prisma.service.js";
|
||||||
import { WechatAuthService } from "./wechat-auth.js";
|
import { WechatAuthService } from "./wechat-auth.js";
|
||||||
@@ -72,6 +73,7 @@ export class HealthController {
|
|||||||
AdminLocalUploadController,
|
AdminLocalUploadController,
|
||||||
],
|
],
|
||||||
providers: [
|
providers: [
|
||||||
|
{ provide: APP_GUARD, useClass: AdminMembershipGuard },
|
||||||
PrismaService,
|
PrismaService,
|
||||||
WechatAuthService,
|
WechatAuthService,
|
||||||
BookingService,
|
BookingService,
|
||||||
@@ -92,7 +94,6 @@ export class HealthController {
|
|||||||
export class AppModule {}
|
export class AppModule {}
|
||||||
|
|
||||||
export function configureApp(app: NestFastifyApplication): void {
|
export function configureApp(app: NestFastifyApplication): void {
|
||||||
app.useGlobalGuards(app.get(AdminMembershipGuard));
|
|
||||||
app.useGlobalPipes(
|
app.useGlobalPipes(
|
||||||
new ValidationPipe({ whitelist: true, forbidNonWhitelisted: true, transform: true }),
|
new ValidationPipe({ whitelist: true, forbidNonWhitelisted: true, transform: true }),
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -23,6 +23,8 @@ import {
|
|||||||
UploadMiniAppReleaseDto,
|
UploadMiniAppReleaseDto,
|
||||||
} from "./admin-miniapp-release.dto.js";
|
} from "./admin-miniapp-release.dto.js";
|
||||||
|
|
||||||
|
const MINIAPP_ARTIFACT_ROOT = process.env.MINIAPP_ARTIFACT_ROOT ?? "apps/taro/dist";
|
||||||
|
|
||||||
interface MiniprogramCi {
|
interface MiniprogramCi {
|
||||||
Project: new (options: {
|
Project: new (options: {
|
||||||
projectPath: string;
|
projectPath: string;
|
||||||
@@ -67,7 +69,7 @@ export class AdminMiniAppReleaseController {
|
|||||||
const settings = this.jsonObject(brand?.miniAppSettings);
|
const settings = this.jsonObject(brand?.miniAppSettings);
|
||||||
return {
|
return {
|
||||||
appId: "",
|
appId: "",
|
||||||
artifactPath: "",
|
artifactPath: MINIAPP_ARTIFACT_ROOT,
|
||||||
build: {},
|
build: {},
|
||||||
...settings,
|
...settings,
|
||||||
privateKey: undefined,
|
privateKey: undefined,
|
||||||
@@ -97,12 +99,12 @@ export class AdminMiniAppReleaseController {
|
|||||||
const settings = {
|
const settings = {
|
||||||
...current,
|
...current,
|
||||||
appId: body.appId ?? current.appId ?? "",
|
appId: body.appId ?? current.appId ?? "",
|
||||||
artifactPath: body.artifactPath ?? current.artifactPath ?? "",
|
|
||||||
build: {
|
build: {
|
||||||
...this.jsonObject(current.build),
|
...this.jsonObject(current.build),
|
||||||
...this.jsonObject(body.build),
|
...this.jsonObject(body.build),
|
||||||
} as Prisma.InputJsonObject,
|
} as Prisma.InputJsonObject,
|
||||||
...(credential ? { credential } : {}),
|
...(credential ? { credential } : {}),
|
||||||
|
templateKey: current.templateKey ?? process.env.MINIAPP_TEMPLATE_KEY ?? "food",
|
||||||
} satisfies Prisma.InputJsonObject;
|
} satisfies Prisma.InputJsonObject;
|
||||||
await this.prisma.brandProfile.update({
|
await this.prisma.brandProfile.update({
|
||||||
where: { id: identity.brandId },
|
where: { id: identity.brandId },
|
||||||
@@ -110,8 +112,9 @@ export class AdminMiniAppReleaseController {
|
|||||||
});
|
});
|
||||||
return {
|
return {
|
||||||
appId: settings.appId,
|
appId: settings.appId,
|
||||||
artifactPath: settings.artifactPath,
|
artifactPath: MINIAPP_ARTIFACT_ROOT,
|
||||||
build: settings.build,
|
build: settings.build,
|
||||||
|
credential: settings.credential ? { algorithm: "aes-256-gcm" } : null,
|
||||||
hasStoredCredentials: Boolean(settings.credential),
|
hasStoredCredentials: Boolean(settings.credential),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -153,11 +156,11 @@ export class AdminMiniAppReleaseController {
|
|||||||
const privateKey = settings.credential
|
const privateKey = settings.credential
|
||||||
? decryptMiniAppCredential(settings.credential)
|
? decryptMiniAppCredential(settings.credential)
|
||||||
: this.requiredEnv("WECHAT_UPLOAD_PRIVATE_KEY");
|
: this.requiredEnv("WECHAT_UPLOAD_PRIVATE_KEY");
|
||||||
const artifactPath =
|
const templateKey =
|
||||||
typeof settings.artifactPath === "string" && settings.artifactPath
|
typeof settings.templateKey === "string" && settings.templateKey
|
||||||
? settings.artifactPath
|
? settings.templateKey
|
||||||
: this.requiredEnv("WEAPP_ARTIFACT_PATH");
|
: this.requiredEnv("MINIAPP_TEMPLATE_KEY");
|
||||||
const projectPath = resolve(artifactPath);
|
const projectPath = resolve(MINIAPP_ARTIFACT_ROOT, templateKey, "weapp");
|
||||||
if (!existsSync(projectPath) || !existsSync(resolve(projectPath, "app.json")))
|
if (!existsSync(projectPath) || !existsSync(resolve(projectPath, "app.json")))
|
||||||
throw new BadRequestException("小程序构建产物不存在");
|
throw new BadRequestException("小程序构建产物不存在");
|
||||||
const ci = (await import("miniprogram-ci")) as unknown as MiniprogramCi;
|
const ci = (await import("miniprogram-ci")) as unknown as MiniprogramCi;
|
||||||
|
|||||||
@@ -7,14 +7,10 @@ export class UpdateMiniAppSettingsDto {
|
|||||||
appId?: string;
|
appId?: string;
|
||||||
|
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
@IsString()
|
|
||||||
artifactPath?: string;
|
|
||||||
|
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
@IsObject()
|
@IsObject()
|
||||||
build?: Record<string, unknown>;
|
build?: Record<string, unknown>;
|
||||||
|
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
@IsString()
|
|
||||||
privateKey?: string;
|
privateKey?: string;
|
||||||
}
|
}
|
||||||
@@ -96,6 +96,37 @@ test("miniapp release tasks are authorized, idempotent, and state checked", asyn
|
|||||||
assert.equal(JSON.parse(published.body).status, "published");
|
assert.equal(JSON.parse(published.body).status, "published");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("miniapp release settings never expose or persist plaintext private keys", async () => {
|
||||||
|
const login = await request("/admin/session/login", {
|
||||||
|
method: "POST",
|
||||||
|
payload: { username: "admin", password: process.env.SEED_ADMIN_PASSWORD },
|
||||||
|
});
|
||||||
|
const headers = { authorization: `Bearer ${JSON.parse(login.body).token}` };
|
||||||
|
const privateKey = `-----BEGIN TEST KEY-----\n${Date.now()}\n-----END TEST KEY-----`;
|
||||||
|
const saved = await request("/admin/miniapp-releases/settings", {
|
||||||
|
method: "PATCH",
|
||||||
|
headers,
|
||||||
|
payload: { appId: "wxtestsettings", privateKey },
|
||||||
|
});
|
||||||
|
assert.equal(saved.statusCode, 200);
|
||||||
|
const body = JSON.parse(saved.body);
|
||||||
|
assert.equal(body.appId, "wxtestsettings");
|
||||||
|
assert.equal(body.hasStoredCredentials, true);
|
||||||
|
assert.equal(body.privateKey, undefined);
|
||||||
|
assert.equal(body.credential?.algorithm, "aes-256-gcm");
|
||||||
|
const brand = await prisma.brandProfile.findUniqueOrThrow({ where: { id: brandId } });
|
||||||
|
const credential = brand.miniAppSettings?.credential;
|
||||||
|
assert.ok(credential);
|
||||||
|
assert.equal(typeof credential.ciphertext, "string");
|
||||||
|
assert.ok(!JSON.stringify(brand.miniAppSettings).includes(privateKey));
|
||||||
|
const missing = await request("/admin/miniapp-releases/settings");
|
||||||
|
assert.equal(missing.statusCode, 401);
|
||||||
|
const forbidden = await request("/admin/miniapp-releases/settings", {
|
||||||
|
headers: { authorization: `Bearer ${customerToken}` },
|
||||||
|
});
|
||||||
|
assert.equal(forbidden.statusCode, 403);
|
||||||
|
});
|
||||||
|
|
||||||
test("real template declarations persist brand offering and trust bindings through draft and publication", async (context) => {
|
test("real template declarations persist brand offering and trust bindings through draft and publication", async (context) => {
|
||||||
const user = await prisma.adminUser.findUniqueOrThrow({ where: { username: "admin" } });
|
const user = await prisma.adminUser.findUniqueOrThrow({ where: { username: "admin" } });
|
||||||
const { runtimeContractForBinding } = await import("@personal-brand/types");
|
const { runtimeContractForBinding } = await import("@personal-brand/types");
|
||||||
|
|||||||
Reference in new issue
Block a user