feat: 追溯码客户端预览(TOTP式令牌保护,不激活不计数,可切换激活状态预览)
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
import { Controller, Get, Param, Req, Res } from '@nestjs/common';
|
||||
import { Controller, Get, Param, Query, Req, Res } from '@nestjs/common';
|
||||
import { Request, Response } from 'express';
|
||||
import { ApiOperation, ApiTags } from '@nestjs/swagger';
|
||||
import { Throttle } from '@nestjs/throttler';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { Public } from '../common/decorators/public.decorator';
|
||||
import { extractIp } from '../common/utils/ua.util';
|
||||
import { isValidPreviewToken } from '../common/utils/preview-token.util';
|
||||
import { PublicTraceService } from './public-trace.service';
|
||||
import { SettingsService } from '../settings/settings.service';
|
||||
|
||||
@@ -13,6 +15,7 @@ export class PublicTraceController {
|
||||
constructor(
|
||||
private readonly service: PublicTraceService,
|
||||
private readonly settings: SettingsService,
|
||||
private readonly config: ConfigService,
|
||||
) {}
|
||||
|
||||
@Public()
|
||||
@@ -26,6 +29,22 @@ export class PublicTraceController {
|
||||
return this.service.trace(shortCode, { ip, userAgent });
|
||||
}
|
||||
|
||||
// 后台预览:凭时间令牌访问,零副作用(不激活/不计数/不写日志)。令牌过期返回 PREVIEW_EXPIRED。
|
||||
@Public()
|
||||
@Get('public/trace/:shortCode/preview')
|
||||
@Throttle({ default: { limit: 60, ttl: 60_000 } })
|
||||
@ApiOperation({ summary: 'C 端溯源预览(令牌保护,不激活不计数)' })
|
||||
preview(
|
||||
@Param('shortCode') shortCode: string,
|
||||
@Query('token') token?: string,
|
||||
@Query('state') state?: string,
|
||||
) {
|
||||
if (!isValidPreviewToken(this.config.get<string>('jwt.secret') || '', token)) {
|
||||
return { status: 'PREVIEW_EXPIRED' as const };
|
||||
}
|
||||
return this.service.preview(shortCode, state === 'active' ? 'active' : 'inactive');
|
||||
}
|
||||
|
||||
// 短链跳转:扫码打开 /t/:shortCode -> 重定向到 C 端溯源页(不在 /api 前缀下)
|
||||
@Public()
|
||||
@Get('t/:shortCode')
|
||||
|
||||
@@ -8,6 +8,13 @@ import { REDIS_CLIENT } from '../redis/redis.module';
|
||||
import { parseUserAgent } from '../common/utils/ua.util';
|
||||
|
||||
type TraceCodeWithRel = Prisma.TraceCodeGetPayload<{ include: { batch: true; artwork: true } }>;
|
||||
type ArtworkWithRel = Prisma.ArtworkGetPayload<{
|
||||
include: {
|
||||
template: { include: { fields: true } };
|
||||
traceNodes: true;
|
||||
certificates: true;
|
||||
};
|
||||
}>;
|
||||
|
||||
export interface TraceContext {
|
||||
ip: string;
|
||||
@@ -186,6 +193,81 @@ export class PublicTraceService {
|
||||
queryCount: updated.queryCount,
|
||||
firstActivatedAt: updated.firstActivatedAt,
|
||||
},
|
||||
...this.buildArtworkPayload(artwork, code, proof),
|
||||
};
|
||||
}
|
||||
|
||||
// 后台预览:与 trace() 相同的数据组装,但零副作用——不激活、不计数、不写查询日志。
|
||||
// state 决定防伪区块以「首次扫码」还是「重复访问」形态展示,antiCounterfeit 数值为演示占位/库中现值。
|
||||
async preview(shortCode: string, state: 'inactive' | 'active') {
|
||||
if (!TRACE_CODE_PATTERN.test(shortCode)) return { status: 'INVALID' as const };
|
||||
const id = await this.resolveId(shortCode);
|
||||
if (id === null) return { status: 'INVALID' as const };
|
||||
|
||||
const code = await this.prisma.traceCode.findUnique({
|
||||
where: { id },
|
||||
include: { batch: true, artwork: true },
|
||||
});
|
||||
if (!code) return { status: 'INVALID' as const };
|
||||
if (code.status === TraceCodeStatus.DISABLED) return { status: 'DISABLED' as const };
|
||||
if (code.status === TraceCodeStatus.ABNORMAL) return { status: 'ABNORMAL' as const };
|
||||
|
||||
const artwork = await this.prisma.artwork.findFirst({
|
||||
where: { id: code.artworkId, deletedAt: null },
|
||||
include: {
|
||||
template: { include: { fields: { orderBy: { sortOrder: 'asc' } } } },
|
||||
traceNodes: { orderBy: { sortOrder: 'asc' } },
|
||||
certificates: { orderBy: { id: 'asc' } },
|
||||
},
|
||||
});
|
||||
if (
|
||||
!artwork ||
|
||||
artwork.status === ArtworkStatus.DISABLED ||
|
||||
artwork.auditStatus !== AuditStatus.APPROVED
|
||||
) {
|
||||
return { status: 'INVALID' as const };
|
||||
}
|
||||
|
||||
const proof = await this.prisma.blockchainProof.findFirst({
|
||||
where: { traceCodeId: id, status: ProofStatus.ONCHAIN },
|
||||
orderBy: { id: 'desc' },
|
||||
});
|
||||
|
||||
const now = new Date();
|
||||
const firstQuery = state === 'inactive';
|
||||
return {
|
||||
status: 'OK' as const,
|
||||
preview: true,
|
||||
code: code.code,
|
||||
queriedAt: now,
|
||||
antiCounterfeit: firstQuery
|
||||
? { firstQuery: true, queryCount: 1, firstActivatedAt: now }
|
||||
: {
|
||||
firstQuery: false,
|
||||
queryCount: code.queryCount > 0 ? code.queryCount : 2,
|
||||
firstActivatedAt: code.firstActivatedAt ?? now,
|
||||
},
|
||||
...this.buildArtworkPayload(artwork, code, proof),
|
||||
};
|
||||
}
|
||||
|
||||
// trace/preview 共用的作品展示数据(作品信息 / 时间线 / 证书 / 区块链存证)
|
||||
private buildArtworkPayload(
|
||||
artwork: ArtworkWithRel,
|
||||
code: { outTime?: Date | null; outImages?: unknown },
|
||||
proof: {
|
||||
txHash: string | null;
|
||||
dataHash: string;
|
||||
blockHash: string | null;
|
||||
blockHeight: bigint | null;
|
||||
chainName: string | null;
|
||||
onchainAt: Date | null;
|
||||
explorerUrl: string | null;
|
||||
tokenId: bigint | null;
|
||||
contractAddress: string | null;
|
||||
} | null,
|
||||
) {
|
||||
return {
|
||||
artwork: {
|
||||
title: artwork.title,
|
||||
artist: artwork.artist,
|
||||
|
||||
Reference in New Issue
Block a user