fix: 追溯码列表短链改为动态生成,修复新生码链接列为空

- 生码不再固化 url 后列表仍读 DB 空值导致链接缺失,抽出 shortLinkOrigin 每页计算一次并动态拼接
- updateOne 返回值同样补动态 url;固化「短链一律动态生成」契约
This commit is contained in:
Nixevol
2026-07-10 09:51:29 +08:00
parent ec8789ccd9
commit 3955065fbb
2 changed files with 16 additions and 5 deletions
+14 -5
View File
@@ -51,16 +51,20 @@ export class TraceCodeService {
return `${base}/t/${encodeURIComponent(shortCode)}`;
}
async buildDynamicShortLink(shortCode: string): Promise<string> {
// 短链 origin 以系统设置 clientBaseUrl 为准(换域名后所有链接自动跟随),失败回退 env baseUrl
private async shortLinkOrigin(): Promise<string> {
try {
const clientBaseUrl = await this.settings.getClientBaseUrl();
const origin = new URL(clientBaseUrl).origin;
return `${origin}/t/${encodeURIComponent(shortCode)}`;
return new URL(clientBaseUrl).origin;
} catch {
return this.buildShortLink(shortCode);
return (this.config.get<string>('baseUrl') || 'http://localhost:3000').replace(/\/$/, '');
}
}
async buildDynamicShortLink(shortCode: string): Promise<string> {
return `${await this.shortLinkOrigin()}/t/${encodeURIComponent(shortCode)}`;
}
private mapOutImages(code: { outImages?: unknown }) {
const keys = Array.isArray(code.outImages)
? (code.outImages as Array<{ key?: string; name?: string; type?: string }>)
@@ -344,7 +348,7 @@ export class TraceCodeService {
await this.prisma.traceCode.update({ where: { id: traceCodeId }, data });
}
return this.prisma.traceCode.findUnique({
const updated = await this.prisma.traceCode.findUnique({
where: { id: traceCodeId },
include: {
artwork: { select: { id: true, title: true, archiveNo: true } },
@@ -352,6 +356,8 @@ export class TraceCodeService {
group: { select: { id: true, name: true } },
},
});
if (!updated) return updated;
return { ...updated, url: await this.buildDynamicShortLink(updated.shortCode) };
}
async removeOne(id: string) {
@@ -458,6 +464,8 @@ export class TraceCodeService {
}),
this.prisma.traceCode.count({ where }),
]);
// 短链统一动态生成(DB 不固化 url,换域名后列表/导出/二维码自动跟随);origin 每页只计算一次
const origin = await this.shortLinkOrigin();
return buildPage(
list.map((code) => {
const { proofs, ...rest } = code;
@@ -465,6 +473,7 @@ export class TraceCodeService {
const outImages = this.mapOutImages(code);
return {
...rest,
url: `${origin}/t/${encodeURIComponent(code.shortCode)}`,
outImageUrl: outImages[0]?.url ?? null,
outImages,
latestProof: latestOnchainProof,