From 4191ba8df552836555b5f2f7b3c3533b278493f8 Mon Sep 17 00:00:00 2001 From: que01 Date: Sun, 20 Sep 2026 22:38:31 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E6=A8=A1=E6=9D=BF?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E5=9B=9E=E6=BB=9A=E8=83=BD=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/features/workspace/WorkspacePage.tsx | 14 ++++++++ apps/api/src/main.ts | 32 +++++++++++++++++++ packages/api-client/src/index.ts | 4 +++ 3 files changed, 50 insertions(+) diff --git a/apps/admin/src/features/workspace/WorkspacePage.tsx b/apps/admin/src/features/workspace/WorkspacePage.tsx index 27010a5..810baa2 100644 --- a/apps/admin/src/features/workspace/WorkspacePage.tsx +++ b/apps/admin/src/features/workspace/WorkspacePage.tsx @@ -73,6 +73,14 @@ export function WorkspacePage() { void queryClient.invalidateQueries({ queryKey: ["admin-template-preview"] }); }, }); + const rollback = useMutation({ + mutationFn: () => client.rollbackAdminTemplate(), + onSuccess: () => { + message.success("模板版本已回滚,现有内容未删除"); + void queryClient.invalidateQueries({ queryKey: ["admin-workspace"] }); + void queryClient.invalidateQueries({ queryKey: ["admin-template-preview"] }); + }, + }); const createForm = useMutation({ mutationFn: (values: { name: string; fieldsSnapshot: unknown[] }) => client.createAdminInquiryForm(values), @@ -162,8 +170,14 @@ export function WorkspacePage() { ) : ( 已是最新版本 )} + {templatePreview.data?.canRollback && ( + + )} + )} diff --git a/apps/api/src/main.ts b/apps/api/src/main.ts index 610948a..9799afb 100644 --- a/apps/api/src/main.ts +++ b/apps/api/src/main.ts @@ -420,6 +420,8 @@ export class AdminWorkspaceController { currentVersion: installation.templateVersion, latestVersion: template.version, canUpgrade: installation.templateVersion !== template.version, + canRollback: + Array.isArray(installation.previousSnapshots) && installation.previousSnapshots.length > 0, missingModules, missingFields, }; @@ -489,6 +491,36 @@ export class AdminWorkspaceController { }); } + @Post("template/rollback") + async rollbackTemplate(@Req() request: FastifyRequest): Promise { + this.requireAdmin(request); + const installation = await this.prisma.brandTemplateInstallation.findFirst({ + where: { brandId: this.brandId() }, + }); + if (!installation) throw new BadRequestException("尚未安装行业模板"); + const history = Array.isArray(installation.previousSnapshots) + ? installation.previousSnapshots + : []; + const previous = history.at(-1); + if (!previous || typeof previous !== "object" || Array.isArray(previous)) + throw new BadRequestException("没有可回滚的模板版本"); + const previousVersion = (previous as { version?: unknown }).version; + if (typeof previousVersion !== "string") throw new BadRequestException("模板快照无效"); + const remaining = history.slice(0, -1); + const updated = await this.prisma.brandTemplateInstallation.update({ + where: { id: installation.id }, + data: { + templateVersion: previousVersion, + configSnapshot: previous as Prisma.InputJsonValue, + previousSnapshots: remaining as Prisma.InputJsonValue, + }, + }); + return { + installation: updated, + note: "已回滚模板版本,现有模块、字段和业务内容未删除。", + }; + } + @Patch("modules/:id") async updateModule( @Req() request: FastifyRequest, diff --git a/packages/api-client/src/index.ts b/packages/api-client/src/index.ts index 15cd381..43724e7 100644 --- a/packages/api-client/src/index.ts +++ b/packages/api-client/src/index.ts @@ -349,6 +349,7 @@ export class ApiClient { currentVersion: string; latestVersion: string; canUpgrade: boolean; + canRollback: boolean; missingModules: unknown[]; missingFields: unknown[]; }> { @@ -357,6 +358,9 @@ export class ApiClient { upgradeAdminTemplate(): Promise { return this.request("/admin/workspace/template/upgrade", { method: "POST" }); } + rollbackAdminTemplate(): Promise { + return this.request("/admin/workspace/template/rollback", { method: "POST" }); + } updateAdminModule(id: string, body: Partial): Promise { return this.request(`/admin/workspace/modules/${encodeURIComponent(id)}`, { method: "PATCH",