feat: 隔离公开工作空间缓存与品牌解析

This commit is contained in:
que01 committed 2026-09-23 08:54:39 +08:00
1 parent ff5dc4e3ba
commit aeb1605a2a
2 files changed
+14 -2

No files matched your search

+12 -2
View File
@@ -1,6 +1,7 @@
import "reflect-metadata";
import {
BadRequestException,
NotFoundException,
UnauthorizedException,
ForbiddenException,
Body,
@@ -147,13 +148,20 @@ export class PublicTemplateController {
@Controller("public/workspace")
export class PublicWorkspaceController {
constructor(private readonly prisma: PrismaService) {}
private readonly cache = new Map<string, { expiresAt: number; value: unknown }>();
@Get()
async get(@Query("brandSlug") brandSlug?: string): Promise<unknown> {
const configuredId = process.env.DEFAULT_BRAND_ID ?? "00000000-0000-0000-0000-000000000001";
const selected = brandSlug
? await this.prisma.brandProfile.findUnique({ where: { slug: brandSlug }, select: { id: true } })
? await this.prisma.brandProfile.findUnique({
where: { slug: brandSlug },
select: { id: true },
})
: null;
if (brandSlug && !selected) throw new NotFoundException("品牌不存在");
const brandId = selected?.id ?? configuredId;
const cached = this.cache.get(brandId);
if (cached && cached.expiresAt > Date.now()) return cached.value;
const [
brand,
modules,
@@ -307,7 +315,7 @@ export class PublicWorkspaceController {
},
...(visualTemplate?.layout ? { layout: visualTemplate.layout } : {}),
};
return {
const response = {
...runtimeWorkspace,
contractVersion: "1.0",
brand,
@@ -335,6 +343,8 @@ export class PublicWorkspaceController {
trustContent: trustContents,
inquiryForm,
};
this.cache.set(brandId, { expiresAt: Date.now() + 30_000, value: response });
return response;
}
private jsonObject(value: unknown): Record<string, unknown> {
return value && typeof value === "object" && !Array.isArray(value)