diff --git a/apps/admin/src/features/workspace/WorkspacePage.tsx b/apps/admin/src/features/workspace/WorkspacePage.tsx index 09a80aa..c7c5ca6 100644 --- a/apps/admin/src/features/workspace/WorkspacePage.tsx +++ b/apps/admin/src/features/workspace/WorkspacePage.tsx @@ -50,6 +50,18 @@ export function WorkspacePage() { void queryClient.invalidateQueries({ queryKey: ["admin-brand"] }); }, }); + const visualHistory = useQuery({ + queryKey: ["visual-template-history"], + queryFn: () => client.getAdminVisualTemplateHistory(), + }); + const rollbackVisual = useMutation({ + mutationFn: () => client.rollbackAdminVisualTemplate(), + onSuccess: () => { + message.success("视觉模板已回滚"); + void runtimeQuery.refetch(); + void visualHistory.refetch(); + }, + }); const install = useMutation({ mutationFn: (key: "personal" | "food") => client.installAdminTemplate(key), onSuccess: () => { @@ -188,6 +200,29 @@ export function WorkspacePage() { )} + + 视觉模板切换历史 + {visualHistory.data?.history.length ? ( + visualHistory.data.history.map((item) => ( + + {item.visualTemplateKey} · {item.savedAt} + + )) + ) : ( + 暂无历史版本 + )} + {visualHistory.data?.history.length ? ( + + ) : null} + + { + this.requireAdmin(request); + const brandId = this.brandId(); + const [brand, installation] = await Promise.all([ + this.prisma.brandProfile.findUnique({ where: { id: brandId }, select: { config: true } }), + this.prisma.brandTemplateInstallation.findFirst({ where: { brandId } }), + ]); + const config = + brand?.config && typeof brand.config === "object" && !Array.isArray(brand.config) + ? (brand.config as Record) + : {}; + const history = Array.isArray(installation?.previousSnapshots) + ? installation.previousSnapshots + : []; + const index = [...history] + .map((item, i) => ({ item, i })) + .reverse() + .find( + ({ item }) => + item && typeof item === "object" && !Array.isArray(item) && "visualTemplateKey" in item, + )?.i; + const previous = + index === undefined ? undefined : (history[index] as { visualTemplateKey?: unknown }); + if (!previous || typeof previous.visualTemplateKey !== "string") + throw new BadRequestException("没有可回滚的视觉模板版本"); + await this.prisma.$transaction([ + this.prisma.brandProfile.update({ + where: { id: brandId }, + data: { + config: { + ...config, + visualTemplateKey: previous.visualTemplateKey, + } as Prisma.InputJsonValue, + }, + }), + ...(installation + ? [ + this.prisma.brandTemplateInstallation.update({ + where: { id: installation.id }, + data: { previousSnapshots: history.slice(0, index) }, + }), + ] + : []), + ]); + return { visualTemplateKey: previous.visualTemplateKey, rolledBack: true }; + } @Get("visual-template/preview") getVisualTemplatePreview(@Req() request: FastifyRequest): unknown { this.requireAdmin(request); diff --git a/docs/template-runtime-progress.md b/docs/template-runtime-progress.md index 1d2be3f..9a41720 100644 --- a/docs/template-runtime-progress.md +++ b/docs/template-runtime-progress.md @@ -48,6 +48,7 @@ - [x] 增加视觉模板预览接口,并在品牌配置更新时合并 config,避免覆盖其他配置。 - [x] 后台视觉模板切换增加确认和差异说明,避免误操作覆盖业务内容。 - [x] 视觉模板切换记录历史快照,并提供后台历史查询接口。 +- [x] 后台展示视觉模板历史并支持安全回滚。 - [ ] Phase 4:Capability Plugin。 - [ ] Phase 5:林境门作与第二行业双模板验证。 - [ ] Phase 6:Stitch 到 Manifest 的转换流水线。 diff --git a/packages/api-client/src/index.ts b/packages/api-client/src/index.ts index 0ab6024..f056a27 100644 --- a/packages/api-client/src/index.ts +++ b/packages/api-client/src/index.ts @@ -383,6 +383,9 @@ export class ApiClient { }> { return this.request("/admin/config/visual-template/history"); } + rollbackAdminVisualTemplate(): Promise<{ visualTemplateKey: string; rolledBack: boolean }> { + return this.request("/admin/config/visual-template/rollback", { method: "POST" }); + } updateAdminBrand(body: BrandUpdate): Promise { return this.request("/admin/config/brand", { method: "PATCH", body }); }