fix: 统一追溯码并修正天河链存证跳转

This commit is contained in:
Nixevol
2026-07-13 07:06:43 +08:00
parent 61d813a096
commit 09b9887a30
21 changed files with 79 additions and 98 deletions
@@ -4,8 +4,10 @@ import { Prisma } from '@prisma/client';
import { ArtworkStatus, AuditStatus, QueryResult, ProofStatus, TRACE_CODE_PATTERN, TraceCodeStatus } from '@artveris/shared';
import { PrismaService } from '../prisma/prisma.service';
import { StorageService } from '../storage/storage.service';
import { SettingsService } from '../settings/settings.service';
import { REDIS_CLIENT } from '../redis/redis.module';
import { parseUserAgent } from '../common/utils/ua.util';
import { buildTianheExplorerTxUrl } from '../blockchain/tianhe.provider';
type TraceCodeWithRel = Prisma.TraceCodeGetPayload<{ include: { batch: true; artwork: true } }>;
type ArtworkWithRel = Prisma.ArtworkGetPayload<{
@@ -32,6 +34,7 @@ export class PublicTraceService {
constructor(
private readonly prisma: PrismaService,
private readonly storage: StorageService,
private readonly settings: SettingsService,
@Inject(REDIS_CLIENT) private readonly redis: Redis,
) {}
@@ -183,17 +186,17 @@ export class PublicTraceService {
const result = firstActivate ? QueryResult.FIRST_ACTIVATE : QueryResult.REPEAT;
await this.writeLog(id, artwork.id, shortCode, result, ctx, { ...code, ...updated });
const explorerUrl = await this.resolveExplorerUrl(proof);
return {
status: 'OK' as const,
code: code.code,
queriedAt: now,
antiCounterfeit: {
firstQuery: firstActivate,
queryCount: updated.queryCount,
firstActivatedAt: updated.firstActivatedAt,
},
...this.buildArtworkPayload(artwork, code, proof),
...this.buildArtworkPayload(artwork, code, proof, explorerUrl),
};
}
@@ -235,10 +238,10 @@ export class PublicTraceService {
const now = new Date();
const firstQuery = state === 'inactive';
const explorerUrl = await this.resolveExplorerUrl(proof);
return {
status: 'OK' as const,
preview: true,
code: code.code,
queriedAt: now,
antiCounterfeit: firstQuery
? { firstQuery: true, queryCount: 1, firstActivatedAt: now }
@@ -247,7 +250,7 @@ export class PublicTraceService {
queryCount: code.queryCount > 0 ? code.queryCount : 2,
firstActivatedAt: code.firstActivatedAt ?? now,
},
...this.buildArtworkPayload(artwork, code, proof),
...this.buildArtworkPayload(artwork, code, proof, explorerUrl),
};
}
@@ -266,6 +269,7 @@ export class PublicTraceService {
tokenId: bigint | null;
contractAddress: string | null;
} | null,
explorerUrl: string | null,
) {
return {
artwork: {
@@ -291,7 +295,7 @@ export class PublicTraceService {
blockHeight: proof.blockHeight,
chainName: proof.chainName,
onchainAt: proof.onchainAt,
explorerUrl: proof.explorerUrl,
explorerUrl,
tokenId: proof.tokenId,
contractAddress: proof.contractAddress,
}
@@ -299,6 +303,18 @@ export class PublicTraceService {
};
}
private async resolveExplorerUrl(
proof: { txHash: string | null; chainName: string | null; explorerUrl: string | null } | null,
): Promise<string | null> {
const tianheProof =
proof?.chainName === '天河链' ||
/tianhe|tichain/i.test(proof?.chainName || '') ||
/tianhecloud\.com/i.test(proof?.explorerUrl || '');
if (!proof?.txHash || !tianheProof) return proof?.explorerUrl ?? null;
const settings = await this.settings.getAllRaw();
return buildTianheExplorerTxUrl(settings.tianheBaseUrl || '', proof.txHash, settings.tianheEnv);
}
private buildFields(
fields: Array<{ fieldKey: string; label: string; type: string }>,
dynamic: unknown,
@@ -336,7 +352,6 @@ export class PublicTraceService {
traceCodeId: traceCodeId ?? undefined,
artworkId: artworkId ?? undefined,
shortCode,
code: code?.code ?? undefined,
archiveNo: code?.artwork?.archiveNo ?? undefined,
batchNo: code?.batch?.batchNo ?? undefined,
queryCount: code?.queryCount ?? undefined,