fix: 优化后台体验与导出日志交互
This commit is contained in:
@@ -71,7 +71,7 @@ export class QueryTraceCodeDto extends PaginationDto {
|
||||
@IsInt()
|
||||
groupId?: number;
|
||||
|
||||
@ApiPropertyOptional({ description: '关键词(短码/追溯码/档案号/作品名称)' })
|
||||
@ApiPropertyOptional({ description: '关键词(追溯码/完整编码/档案号/作品名称)' })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
keyword?: string;
|
||||
|
||||
@@ -125,7 +125,7 @@ export class TraceCodeController {
|
||||
|
||||
@Get('trace-codes/export')
|
||||
@Roles(RoleType.OPERATOR)
|
||||
@ApiOperation({ summary: '批量导出追溯码(Excel / CSV,可按筛选或所选)' })
|
||||
@ApiOperation({ summary: '批量导出追溯码 ZIP(xlsx + 二维码图片)' })
|
||||
export(@Query() query: ExportTraceCodeDto, @Res() res: Response) {
|
||||
return this.service.exportCodes(query, res);
|
||||
}
|
||||
|
||||
@@ -6,12 +6,14 @@ import { Prisma } from '@prisma/client';
|
||||
import { Response } from 'express';
|
||||
import * as ExcelJS from 'exceljs';
|
||||
import * as QRCode from 'qrcode';
|
||||
import AdmZip from 'adm-zip';
|
||||
import dayjs from 'dayjs';
|
||||
import { ArtworkStatus, BatchStatus, ErrorCode, JobType, QUEUE_NAMES, TraceCodeStatus } from '@artveris/shared';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { StorageService } from '../storage/storage.service';
|
||||
import { BusinessException } from '../common/exceptions/business.exception';
|
||||
import { buildPage } from '../common/utils/pagination.util';
|
||||
import { buildOrderBy } from '../common/utils/sort.util';
|
||||
import { JobsService } from '../jobs/jobs.service';
|
||||
import {
|
||||
BatchDeleteDto,
|
||||
@@ -360,10 +362,11 @@ export class TraceCodeService {
|
||||
const where: Prisma.BatchWhereInput = {};
|
||||
if (query.artworkId) where.artworkId = query.artworkId;
|
||||
if (query.keyword) where.batchNo = { contains: query.keyword };
|
||||
const orderBy = buildOrderBy(query.sortField, query.sortOrder, ['createdAt'] as const, { id: 'desc' });
|
||||
const [list, total] = await this.prisma.$transaction([
|
||||
this.prisma.batch.findMany({
|
||||
where,
|
||||
orderBy: { id: 'desc' },
|
||||
orderBy,
|
||||
skip: query.skip,
|
||||
take: query.take,
|
||||
}),
|
||||
@@ -402,6 +405,7 @@ export class TraceCodeService {
|
||||
|
||||
async list(query: QueryTraceCodeDto) {
|
||||
const where = this.buildTraceCodeWhere(query);
|
||||
const orderBy = buildOrderBy(query.sortField, query.sortOrder, ['createdAt', 'outTime', 'firstActivatedAt'] as const, { id: 'desc' });
|
||||
const [list, total] = await this.prisma.$transaction([
|
||||
this.prisma.traceCode.findMany({
|
||||
where,
|
||||
@@ -414,7 +418,7 @@ export class TraceCodeService {
|
||||
take: 10,
|
||||
},
|
||||
},
|
||||
orderBy: { id: 'desc' },
|
||||
orderBy,
|
||||
skip: query.skip,
|
||||
take: query.take,
|
||||
}),
|
||||
@@ -478,15 +482,16 @@ export class TraceCodeService {
|
||||
{ header: '名称', key: 'title', width: 20 },
|
||||
{ header: '批次编号', key: 'batchNo', width: 22 },
|
||||
{ header: '分组', key: 'group', width: 14 },
|
||||
{ header: '追溯码', key: 'code', width: 24 },
|
||||
{ header: '随机短码', key: 'shortCode', width: 16 },
|
||||
{ header: '短链', key: 'url', width: 40 },
|
||||
{ header: '追溯码', key: 'shortCode', width: 16 },
|
||||
{ header: '完整编码', key: 'code', width: 24 },
|
||||
{ header: '链接', key: 'url', width: 40 },
|
||||
{ header: '二维码', key: 'qr', width: 18 },
|
||||
{ header: '生成时间', key: 'createdAt', width: 22 },
|
||||
];
|
||||
ws.getRow(1).font = { bold: true };
|
||||
ws.getColumn('qr').width = 18;
|
||||
|
||||
const qrImages: Array<{ name: string; buffer: Buffer }> = [];
|
||||
for (const c of codes) {
|
||||
const url = c.url || this.buildTraceUrl(c.shortCode);
|
||||
const row = ws.addRow({
|
||||
@@ -501,8 +506,9 @@ export class TraceCodeService {
|
||||
createdAt: dayjs(c.createdAt).format('YYYY-MM-DD HH:mm:ss'),
|
||||
});
|
||||
row.height = 72;
|
||||
const qr = await QRCode.toDataURL(url, { margin: 1, width: 220 });
|
||||
const imageId = wb.addImage({ base64: qr, extension: 'png' });
|
||||
const qr = await QRCode.toBuffer(url, { type: 'png', margin: 1, width: 320 });
|
||||
qrImages.push({ name: `images/${c.shortCode}.png`, buffer: qr });
|
||||
const imageId = wb.addImage({ buffer: qr as unknown as ExcelJS.Buffer, extension: 'png' });
|
||||
ws.addImage(imageId, {
|
||||
tl: { col: 7.15, row: row.number - 0.9 },
|
||||
ext: { width: 84, height: 84 },
|
||||
@@ -510,12 +516,13 @@ export class TraceCodeService {
|
||||
}
|
||||
|
||||
const stamp = dayjs().format('YYYYMMDDHHmmss');
|
||||
res.setHeader(
|
||||
'Content-Type',
|
||||
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
||||
);
|
||||
res.setHeader('Content-Disposition', `attachment; filename="trace-codes-${stamp}.xlsx"`);
|
||||
await wb.xlsx.write(res);
|
||||
res.end();
|
||||
const archiveNos = new Set(codes.map((c) => c.artwork?.archiveNo).filter(Boolean));
|
||||
const baseName = archiveNos.size === 1 ? [...archiveNos][0]! : 'ARTVERIS';
|
||||
const zip = new AdmZip();
|
||||
zip.addFile(`${baseName}_${stamp}.xlsx`, Buffer.from(await wb.xlsx.writeBuffer()));
|
||||
for (const image of qrImages) zip.addFile(image.name, image.buffer);
|
||||
res.setHeader('Content-Type', 'application/zip');
|
||||
res.setHeader('Content-Disposition', `attachment; filename="${baseName}_${stamp}.zip"`);
|
||||
res.end(zip.toBuffer());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user