perf(afterSale): 售后导出流式写临时文件上传,避免内存 Excel 堆积,进度回调 await 化确保缓存进度不丢

This commit is contained in:
liufumingren 2026-09-03 17:03:52 +08:00
parent b91c3f4124
commit 4259628396

View File

@ -1,5 +1,10 @@
'use strict';
const fs = require('fs');
const fsp = require('fs/promises');
const os = require('os');
const path = require('path');
const crypto = require('crypto');
const Service = require('egg').Service;
/**
@ -2015,13 +2020,13 @@ class AfterSaleService extends Service {
async exportAfterSales(options = {}) {
const { where = {}, sort = {}, onProgress } = options;
const reportProgress = (status, progress, message, extra = {}) => {
const reportProgress = async (status, progress, message, extra = {}) => {
if (typeof onProgress === 'function') {
onProgress({ status, progress, message, ...extra });
await onProgress({ status, progress, message, ...extra });
}
};
reportProgress('running', 5, '正在准备导出数据...');
await reportProgress('running', 5, '正在准备导出数据...');
// 先统计总数
const { count: totalCount } = await this.ctx.model.AfterSale.findAndCountAll({
@ -2029,7 +2034,7 @@ class AfterSaleService extends Service {
raw: true,
});
reportProgress('running', 10, `共 ${totalCount} 条记录,开始导出...`, { totalCount, currentCount: 0 });
await reportProgress('running', 10, `共 ${totalCount} 条记录,开始导出...`, { totalCount, currentCount: 0 });
// 分页查询并导出
const pageSize = 500;
@ -2042,7 +2047,7 @@ class AfterSaleService extends Service {
allRows.push(...rows);
const progress = 10 + Math.floor(((page / totalPages) * 80));
reportProgress('running', progress, `正在导出... (${allRows.length}/${totalCount})`, {
await reportProgress('running', progress, `正在导出... (${allRows.length}/${totalCount})`, {
totalCount,
currentCount: allRows.length,
});
@ -2051,12 +2056,12 @@ class AfterSaleService extends Service {
await new Promise(resolve => setImmediate(resolve));
}
reportProgress('running', 90, '正在上传文件...', { totalCount, currentCount: allRows.length });
await reportProgress('running', 90, '正在上传文件...', { totalCount, currentCount: allRows.length });
// 生成 Excel 文件并上传 COS
const exportResult = await this._generateAndUploadExcel(allRows);
reportProgress('success', 100, '导出完成', {
await reportProgress('success', 100, '导出完成', {
totalCount,
currentCount: allRows.length,
result: exportResult,
@ -2071,16 +2076,28 @@ class AfterSaleService extends Service {
/**
* 生成 Excel 并上传 COS,返回下载信息。
* 与其他导出(order/return_received 等)口径一致:流式写临时文件 → uploadFile → uploadSuccessNotify。
*
* @param {Array<Object>} rows 售后单数据行
* @return {Promise<Object>} { url, fileKey, fileName }
* @return {Promise<Object>} { filename, fileKey, url, cdnUrl, cosUrl }
*/
async _generateAndUploadExcel(rows = []) {
const { ctx } = this;
const tempDir = await fsp.mkdtemp(path.join(os.tmpdir(), 'wms-after-sale-export-'));
const now = new Date();
const dateToken = `${now.getFullYear()}${String(now.getMonth() + 1).padStart(2, '0')}${String(now.getDate()).padStart(2, '0')}${String(now.getHours()).padStart(2, '0')}${String(now.getMinutes()).padStart(2, '0')}${String(now.getSeconds()).padStart(2, '0')}`;
const filename = `售后单导出_${dateToken}.xlsx`;
const filePath = path.join(tempDir, filename);
const ExcelJS = require('exceljs');
const workbook = new ExcelJS.stream.xlsx.WorkbookWriter({
filename: filePath,
useStyles: false,
useSharedStrings: false,
});
try {
const ExcelJS = require('exceljs');
const workbook = new ExcelJS.Workbook();
const worksheet = workbook.addWorksheet('售后单列表');
// 定义列
@ -2110,46 +2127,70 @@ class AfterSaleService extends Service {
// 写入数据行
for (const row of rows) {
worksheet.addRow(row);
worksheet.addRow(row).commit();
}
worksheet.commit();
await workbook.commit();
// ---- 上传文件(与 order/return_received 导出口径一致)----
const uploadService = ctx?.service?.systems?.upload;
if (!uploadService || typeof uploadService.uploadFile !== 'function') {
throw new Error('上传服务不可用');
}
// 生成 buffer
const buffer = await workbook.xlsx.writeBuffer();
const stat = await fsp.stat(filePath);
const randomToken = crypto.randomBytes(6).toString('hex');
const uploadKey = `exports/after_sale/${Date.now()}_${randomToken}.xlsx`;
const uploadStream = fs.createReadStream(filePath);
uploadStream.filename = filename;
uploadStream.mimeType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
uploadStream.fileSize = stat.size;
// 上传到 COS
const now = new Date();
const dateStr = `${now.getFullYear()}${String(now.getMonth() + 1).padStart(2, '0')}${String(now.getDate()).padStart(2, '0')}`;
const timestamp = Date.now();
const fileName = `after_sale_export_${dateStr}_${timestamp}.xlsx`;
const fileKey = `export/after_sale/${fileName}`;
const uploadResult = await uploadService.uploadFile(uploadStream, {
key: uploadKey,
category: 'document',
mimetype: 'xls,xlsx,csv',
});
let url = '';
try {
if (ctx.service.systems && ctx.service.systems.upload && typeof ctx.service.systems.upload.uploadToCos === 'function') {
const uploadResult = await ctx.service.systems.upload.uploadToCos({
buffer,
fileKey,
fileName,
mimeType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
});
url = uploadResult && uploadResult.url ? uploadResult.url : '';
}
} catch (uploadError) {
ctx.logger.warn(`[afterSale.export] COS 上传失败: ${uploadError.message}`);
let fileRecord = null;
if (typeof uploadService.uploadSuccessNotify === 'function') {
fileRecord = await uploadService.uploadSuccessNotify({
url: uploadResult.fileName,
fileKey: uploadResult.fileName,
fileName: filename,
fileType: 'document',
mimeType: uploadStream.mimeType,
fileSize: stat.size,
category: 'document',
businessType: 'afterSaleExport',
description: `售后单导出,共 ${rows.length} 条记录`,
});
}
// 优先取 uploadFile 返回的完整 URL,其次取 uploadSuccessNotify 落库记录,最后回退 COS key
const safeUploadResult = typeof uploadResult === 'object' && uploadResult !== null ? uploadResult : {};
const resolvedFileRecord = (fileRecord && (fileRecord.data || fileRecord)) || null;
const fileUrlFromRecord = resolvedFileRecord
? (resolvedFileRecord.file_url || resolvedFileRecord.fileUrl || resolvedFileRecord.url || '')
: '';
const uploadResultUrl = safeUploadResult.url || safeUploadResult.cdnUrl || safeUploadResult.cosUrl || '';
return {
url,
fileKey,
fileName,
filename,
fileKey: safeUploadResult.fileName || uploadKey,
url: uploadResultUrl || fileUrlFromRecord || safeUploadResult.fileName || uploadKey,
cdnUrl: safeUploadResult.cdnUrl || safeUploadResult.url || '',
cosUrl: safeUploadResult.cosUrl || '',
};
} catch (error) {
ctx.logger.error(`[afterSale.export] 生成 Excel 失败: ${error.message}`);
return {
url: '',
fileKey: '',
fileName: '',
};
const workbookStream = workbook.stream;
if (workbookStream && typeof workbookStream.destroy === 'function') {
workbookStream.destroy();
}
ctx.logger.error(`[afterSale.export] 生成/上传 Excel 失败: ${error.message}`);
throw error;
} finally {
await fsp.rm(tempDir, { recursive: true, force: true });
}
}