feat: 支持视觉模板历史回滚
This commit is contained in:
1 parent
a951504563
commit
a30c939f90
4 files changed
+86
No files matched your search
@@ -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() {
|
||||
)}
|
||||
</Space>
|
||||
<MutationError error={visualTemplate.error} />
|
||||
<Space direction="vertical" style={{ width: "100%" }}>
|
||||
<Typography.Text strong>视觉模板切换历史</Typography.Text>
|
||||
{visualHistory.data?.history.length ? (
|
||||
visualHistory.data.history.map((item) => (
|
||||
<Tag key={`${item.visualTemplateKey}-${item.savedAt}`}>
|
||||
{item.visualTemplateKey} · {item.savedAt}
|
||||
</Tag>
|
||||
))
|
||||
) : (
|
||||
<Typography.Text type="secondary">暂无历史版本</Typography.Text>
|
||||
)}
|
||||
{visualHistory.data?.history.length ? (
|
||||
<Button
|
||||
danger
|
||||
size="small"
|
||||
onClick={() => rollbackVisual.mutate()}
|
||||
loading={rollbackVisual.isPending}
|
||||
>
|
||||
回滚上一版视觉模板
|
||||
</Button>
|
||||
) : null}
|
||||
<MutationError error={rollbackVisual.error} />
|
||||
</Space>
|
||||
<Modal
|
||||
title="确认切换视觉模板"
|
||||
open={visualTarget !== null}
|
||||
|
||||
@@ -702,6 +702,53 @@ export class AdminConfigController {
|
||||
: [],
|
||||
}));
|
||||
}
|
||||
@Post("visual-template/rollback")
|
||||
async rollbackVisualTemplate(@Req() request: FastifyRequest): Promise<unknown> {
|
||||
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<string, unknown>)
|
||||
: {};
|
||||
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);
|
||||
|
||||
@@ -48,6 +48,7 @@
|
||||
- [x] 增加视觉模板预览接口,并在品牌配置更新时合并 config,避免覆盖其他配置。
|
||||
- [x] 后台视觉模板切换增加确认和差异说明,避免误操作覆盖业务内容。
|
||||
- [x] 视觉模板切换记录历史快照,并提供后台历史查询接口。
|
||||
- [x] 后台展示视觉模板历史并支持安全回滚。
|
||||
- [ ] Phase 4:Capability Plugin。
|
||||
- [ ] Phase 5:林境门作与第二行业双模板验证。
|
||||
- [ ] Phase 6:Stitch 到 Manifest 的转换流水线。
|
||||
|
||||
@@ -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<BrandProfile> {
|
||||
return this.request("/admin/config/brand", { method: "PATCH", body });
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user