feat: 接入天河链存证接口

This commit is contained in:
Nixevol
2026-07-06 18:25:55 +08:00
parent 3efc1ecd88
commit 07114b759d
26 changed files with 412 additions and 236 deletions
+19 -61
View File
@@ -2,18 +2,11 @@ import { Injectable } from '@nestjs/common';
import { InjectQueue } from '@nestjs/bullmq';
import { Queue } from 'bullmq';
import { Response } from 'express';
import { createHash } from 'crypto';
import * as ExcelJS from 'exceljs';
import AdmZip from 'adm-zip';
import sharp from 'sharp';
import { Prisma, RoleType } from '@prisma/client';
import {
ErrorCode,
JobType,
OUTBOUND_IMPORT_COLUMNS,
ProofStatus,
QUEUE_NAMES,
} from '@artveris/shared';
import { ErrorCode, JobType, OUTBOUND_IMPORT_COLUMNS, QUEUE_NAMES } from '@artveris/shared';
import { PrismaService } from '../prisma/prisma.service';
import { StorageService } from '../storage/storage.service';
import { JobsService } from '../jobs/jobs.service';
@@ -50,42 +43,10 @@ export class OutboundService {
: Prisma.JsonNull;
}
if ('remark' in dto) data.remark = dto.remark || null;
const updated = await this.prisma.traceCode.update({
return this.prisma.traceCode.update({
where: { id: traceCodeId },
data,
});
if (dto.txHash) {
await this.recordProof(traceCodeId, code.code, {
txHash: dto.txHash,
onchainAt: dto.onchainAt ? new Date(dto.onchainAt) : new Date(),
blockHeight: dto.blockHeight,
});
}
return updated;
}
private async recordProof(
traceCodeId: bigint,
code: string,
p: { txHash: string; onchainAt: Date; blockHeight?: number },
) {
const dup = await this.prisma.blockchainProof.findFirst({
where: { traceCodeId, txHash: p.txHash },
});
if (dup) return;
const dataHash = '0x' + createHash('sha256').update(`${code}:${p.txHash}`).digest('hex');
await this.prisma.blockchainProof.create({
data: {
traceCodeId,
dataHash,
txHash: p.txHash,
blockHeight: p.blockHeight !== undefined ? BigInt(p.blockHeight) : null,
chainName: '手工登记',
onchainAt: p.onchainAt,
status: ProofStatus.ONCHAIN,
rawResponse: { source: 'manual' },
},
});
}
async generateTemplate(res: Response): Promise<void> {
@@ -94,23 +55,17 @@ export class OutboundService {
ws.columns = OUTBOUND_IMPORT_COLUMNS.map((c) => ({ header: c.header, key: c.key, width: c.key === 'outImages' ? 36 : 22 }));
ws.getRow(1).font = { bold: true };
ws.addRow({
shortCode: '请替换为真实追溯码1',
shortCode: '请替换为真实追溯码',
batchNo: 'B20260704001',
outTime: '2026-07-04 10:00',
outImages: 'images/out-001.jpg',
txHash: '0xabc123',
onchainAt: '2026-07-04 10:30',
blockHeight: '8500001',
remark: '单图示例',
});
ws.addRow({
shortCode: '请替换为真实追溯码2',
shortCode: '请替换为真实追溯码',
batchNo: 'B20260704001',
outTime: '2026-07-04 11:00',
outImages: 'images/out-002.png;images/out-003.jpg',
txHash: '',
onchainAt: '',
blockHeight: '',
remark: '多图示例',
});
@@ -127,17 +82,21 @@ export class OutboundService {
const zip = new AdmZip();
zip.addFile('outbound-import-template.xlsx', Buffer.from(await wb.xlsx.writeBuffer()));
zip.addFile('说明.txt', Buffer.from(
[
'出库批量导入说明',
'',
'1. ZIP 根目录必须包含一个 xlsx 文件和 images 文件夹。',
'2. 出库实况图路径填写 images/xxx.jpg,多张图片使用英文分号 ; 分隔。',
'3. 不允许使用绝对路径、..、反斜杠或 images 目录之外的路径。',
'4. 上传 ZIP 后系统会先预览校验,确认无误后才正式导入。',
].join('\r\n'),
'utf8',
));
zip.addFile(
'说明.txt',
Buffer.from(
[
'出库批量导入说明',
'',
'1. ZIP 根目录必须包含一个 xlsx 文件和 images 文件夹。',
'2. 出库实况图路径填写 images/xxx.jpg,多张图片使用英文分号 ; 分隔。',
'3. 不允许使用绝对路径、..、反斜杠或 images 目录之外的路径。',
'4. 上传 ZIP 后系统会先预览校验,确认无误后才正式导入。',
'5. 链上哈希、存证链接和上链时间由天河链接口返回后自动写入,不在导入模板中手工填写。',
].join('\r\n'),
'utf8',
),
);
zip.addFile('images/out-001.jpg', await this.sampleImage('#13a8a8'));
zip.addFile('images/out-002.png', await this.sampleImage('#3b82f6', 'png'));
zip.addFile('images/out-003.jpg', await this.sampleImage('#f59e0b'));
@@ -182,7 +141,6 @@ export class OutboundService {
return { sessionId: session.id, jobId: job.id };
}
// 兼容旧入口:上传后只做预览,不直接入库。
startImport(file: Express.Multer.File, userId: number) {
return this.startPreview(file, userId);
}