feat: 完成 PRD 差距与修复,新增审核流与日志清理功能,支持出库信息与动态字段,优化 C 端用户体验
This commit is contained in:
@@ -1,12 +1,15 @@
|
||||
import { Inject, Injectable, Logger } from '@nestjs/common';
|
||||
import type Redis from 'ioredis';
|
||||
import { ArtworkStatus, QueryResult, ProofStatus, TraceCodeStatus } from '@artveris/shared';
|
||||
import { Prisma } from '@prisma/client';
|
||||
import { ArtworkStatus, AuditStatus, QueryResult, ProofStatus, TraceCodeStatus } from '@artveris/shared';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { StorageService } from '../storage/storage.service';
|
||||
import { GeoService } from '../geo/geo.service';
|
||||
import { REDIS_CLIENT } from '../redis/redis.module';
|
||||
import { parseUserAgent } from '../common/utils/ua.util';
|
||||
|
||||
type TraceCodeWithBatch = Prisma.TraceCodeGetPayload<{ include: { batch: true } }>;
|
||||
|
||||
export interface TraceContext {
|
||||
ip: string;
|
||||
userAgent?: string;
|
||||
@@ -52,17 +55,17 @@ export class PublicTraceService {
|
||||
return { status: 'INVALID' as const };
|
||||
}
|
||||
|
||||
const code = await this.prisma.traceCode.findUnique({ where: { id } });
|
||||
const code = await this.prisma.traceCode.findUnique({ where: { id }, include: { batch: true } });
|
||||
if (!code || !code.artworkId || code.status === TraceCodeStatus.UNBOUND) {
|
||||
await this.writeLog(id, null, shortCode, QueryResult.INVALID, ctx);
|
||||
await this.writeLog(id, null, shortCode, QueryResult.INVALID, ctx, code ?? undefined);
|
||||
return { status: 'INVALID' as const };
|
||||
}
|
||||
if (code.status === TraceCodeStatus.DISABLED) {
|
||||
await this.writeLog(id, code.artworkId, shortCode, QueryResult.DISABLED, ctx);
|
||||
await this.writeLog(id, code.artworkId, shortCode, QueryResult.DISABLED, ctx, code);
|
||||
return { status: 'DISABLED' as const };
|
||||
}
|
||||
if (code.status === TraceCodeStatus.ABNORMAL) {
|
||||
await this.writeLog(id, code.artworkId, shortCode, QueryResult.ABNORMAL, ctx);
|
||||
await this.writeLog(id, code.artworkId, shortCode, QueryResult.ABNORMAL, ctx, code);
|
||||
return { status: 'ABNORMAL' as const };
|
||||
}
|
||||
|
||||
@@ -74,8 +77,13 @@ export class PublicTraceService {
|
||||
certificates: { orderBy: { id: 'asc' } },
|
||||
},
|
||||
});
|
||||
if (!artwork || artwork.status === ArtworkStatus.DISABLED) {
|
||||
await this.writeLog(id, code.artworkId, shortCode, QueryResult.INVALID, ctx);
|
||||
// 仅展示「未停用」且「审核通过」的作品,否则视为无效,避免未审核数据外泄
|
||||
if (
|
||||
!artwork ||
|
||||
artwork.status === ArtworkStatus.DISABLED ||
|
||||
artwork.auditStatus !== AuditStatus.APPROVED
|
||||
) {
|
||||
await this.writeLog(id, code.artworkId, shortCode, QueryResult.INVALID, ctx, code);
|
||||
return { status: 'INVALID' as const };
|
||||
}
|
||||
|
||||
@@ -107,10 +115,12 @@ export class PublicTraceService {
|
||||
});
|
||||
|
||||
const result = firstActivate ? QueryResult.FIRST_ACTIVATE : QueryResult.REPEAT;
|
||||
await this.writeLog(id, artwork.id, shortCode, result, ctx);
|
||||
await this.writeLog(id, artwork.id, shortCode, result, ctx, { ...code, ...updated });
|
||||
|
||||
return {
|
||||
status: 'OK' as const,
|
||||
code: code.code,
|
||||
queriedAt: now,
|
||||
antiCounterfeit: {
|
||||
firstQuery: firstActivate,
|
||||
queryCount: updated.queryCount,
|
||||
@@ -157,7 +167,19 @@ export class PublicTraceService {
|
||||
) {
|
||||
const data = (dynamic as Record<string, unknown>) || {};
|
||||
return fields
|
||||
.map((f) => ({ label: f.label, type: f.type, value: data[f.fieldKey] }))
|
||||
.map((f) => {
|
||||
let value = data[f.fieldKey];
|
||||
// 图片/文件字段:若存的是存储 key(非绝对/Data URL),转为可访问 URL,避免 C 端 404
|
||||
if (
|
||||
(f.type === 'IMAGE' || f.type === 'FILE') &&
|
||||
typeof value === 'string' &&
|
||||
value &&
|
||||
!/^(https?:|data:|\/)/i.test(value)
|
||||
) {
|
||||
value = this.storage.getUrl(value);
|
||||
}
|
||||
return { label: f.label, type: f.type, value };
|
||||
})
|
||||
.filter((f) => f.value !== undefined && f.value !== null && f.value !== '');
|
||||
}
|
||||
|
||||
@@ -167,6 +189,7 @@ export class PublicTraceService {
|
||||
shortCode: string,
|
||||
result: QueryResult,
|
||||
ctx: TraceContext,
|
||||
code?: Partial<TraceCodeWithBatch>,
|
||||
): Promise<void> {
|
||||
try {
|
||||
const device = parseUserAgent(ctx.userAgent);
|
||||
@@ -175,11 +198,19 @@ export class PublicTraceService {
|
||||
traceCodeId: traceCodeId ?? undefined,
|
||||
artworkId: artworkId ?? undefined,
|
||||
shortCode,
|
||||
code: code?.code ?? undefined,
|
||||
sku: code?.sku ?? undefined,
|
||||
archiveNo: code?.archiveNo ?? undefined,
|
||||
batchNo: code?.batch?.batchNo ?? undefined,
|
||||
queryCount: code?.queryCount ?? undefined,
|
||||
firstActivatedAt: code?.firstActivatedAt ?? undefined,
|
||||
queryResult: result,
|
||||
ip: ctx.ip,
|
||||
region: this.geo.resolveRegion(ctx.ip),
|
||||
userAgent: ctx.userAgent,
|
||||
deviceType: device.deviceType,
|
||||
browser: device.browser,
|
||||
os: device.os,
|
||||
},
|
||||
});
|
||||
} catch (e) {
|
||||
|
||||
Reference in New Issue
Block a user