fix: route PRINT_COMPONENT through PDF printing

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
liufumingren 2026-11-25 02:27:10 +08:00
parent e9d081ed4e
commit 60a2027741
4 changed files with 81 additions and 33 deletions

View File

@ -67,7 +67,7 @@ const PLATFORM_MAP = Object.freeze({
aliases: [ 'xhs', 'xiaohongshu', 'red' ], aliases: [ 'xhs', 'xiaohongshu', 'red' ],
template_file: 'xhs.json', template_file: 'xhs.json',
}), }),
shopify: Object.freeze({ PRINT_COMPONENT: Object.freeze({
name: '本机组件', name: '本机组件',
aliases: [ 'shopify' ], aliases: [ 'shopify' ],
template_file: 'shopify.json', template_file: 'shopify.json',
@ -222,7 +222,7 @@ function normalizePlatformAlias(inputPlatform) {
for (const platform of Object.keys(PLATFORM_MAP)) { for (const platform of Object.keys(PLATFORM_MAP)) {
const meta = PLATFORM_MAP[platform]; const meta = PLATFORM_MAP[platform];
if (platform === raw || meta.aliases.includes(raw)) { if (platform.toLowerCase() === raw || meta.aliases.some(alias => String(alias).toLowerCase() === raw)) {
return { platform, meta, raw }; return { platform, meta, raw };
} }
} }

View File

@ -282,11 +282,17 @@ class PrintService extends MainService {
return lowerPlatformCode; return lowerPlatformCode;
} }
isShopifyPdfPlatform(platformCode) { /**
return String(platformCode || '').trim().toLowerCase() === 'shopify'; * 判断平台或组件编码是否应走 PDF 直打。
* @param {string} platformCode 平台或组件编码。
* @return {boolean} 是否走 PDF 直打。
*/
isPdfPrintPlatform(platformCode) {
const normalizedPlatformCode = String(platformCode || '').trim().toUpperCase();
return normalizedPlatformCode === 'PRINT_COMPONENT' || normalizedPlatformCode === 'SHOPIFY';
} }
buildShopifyPdfPayload(payload = {}) { buildPdfPrintPayload(payload = {}) {
const normalizedPayload = payload && typeof payload === 'object' ? payload : {}; const normalizedPayload = payload && typeof payload === 'object' ? payload : {};
const orders = Array.isArray(normalizedPayload.orders) ? normalizedPayload.orders : []; const orders = Array.isArray(normalizedPayload.orders) ? normalizedPayload.orders : [];
const orderIds = Array.isArray(normalizedPayload.orderIds) ? normalizedPayload.orderIds : []; const orderIds = Array.isArray(normalizedPayload.orderIds) ? normalizedPayload.orderIds : [];
@ -301,7 +307,7 @@ class PrintService extends MainService {
const pdfOrders = orders.map((order, index) => { const pdfOrders = orders.map((order, index) => {
if (!order || typeof order !== 'object' || Array.isArray(order)) { if (!order || typeof order !== 'object' || Array.isArray(order)) {
throw new Error(`第 ${index + 1} 个 Shopify 面单数据无效`); throw new Error(`第 ${index + 1} 个 PDF 面单数据无效`);
} }
let contents = order.contents; let contents = order.contents;
@ -309,7 +315,7 @@ class PrintService extends MainService {
try { try {
contents = JSON.parse(contents); contents = JSON.parse(contents);
} catch (error) { } catch (error) {
throw new Error(`Shopify 面单 contents 格式无效: ${error.message}`); throw new Error(`PDF 面单 contents 格式无效: ${error.message}`);
} }
} }
const contentRows = Array.isArray(contents) const contentRows = Array.isArray(contents)
@ -331,7 +337,7 @@ class PrintService extends MainService {
|| order.documentID || order.documentID
|| order.document_id; || order.document_id;
if (!pdfBase64) { if (!pdfBase64) {
throw new Error(`Shopify 面单 ${documentId || index + 1} 缺少 pdf_base64`); throw new Error(`PDF 面单 ${documentId || index + 1} 缺少 pdf_base64`);
} }
return { return {
@ -1002,7 +1008,7 @@ class PrintService extends MainService {
} }
const platformCodes = this.collectExpressPlatformCodes(data.waveGroups); const platformCodes = this.collectExpressPlatformCodes(data.waveGroups);
const componentPlatformCodes = platformCodes.filter(platformCode => !this.isShopifyPdfPlatform(platformCode)); const componentPlatformCodes = platformCodes.filter(platformCode => !this.isPdfPrintPlatform(platformCode));
const platformComponentsResult = platformCodes.length > 0 && componentPlatformCodes.length === 0 const platformComponentsResult = platformCodes.length > 0 && componentPlatformCodes.length === 0
? { success: true, data: {} } ? { success: true, data: {} }
: await this.loadExpressPlatformComponents({ platformCodes: componentPlatformCodes }); : await this.loadExpressPlatformComponents({ platformCodes: componentPlatformCodes });
@ -2352,12 +2358,12 @@ class PrintService extends MainService {
this.ctx.logger.info('[print.send] 平台快递组发送开始', groupSummary); this.ctx.logger.info('[print.send] 平台快递组发送开始', groupSummary);
const platformCode = String(platformGroup && platformGroup.platform ? platformGroup.platform : '').trim(); const platformCode = String(platformGroup && platformGroup.platform ? platformGroup.platform : '').trim();
const isShopifyPdf = this.isShopifyPdfPlatform(platformCode); const isPdfPrint = this.isPdfPrintPlatform(platformCode);
const component = String( const component = String(
normalizedPayload.platformComponents normalizedPayload.platformComponents
&& normalizedPayload.platformComponents[platformCode] && normalizedPayload.platformComponents[platformCode]
? normalizedPayload.platformComponents[platformCode] ? normalizedPayload.platformComponents[platformCode]
: (isShopifyPdf ? 'PDF' : '') : (isPdfPrint ? 'PDF' : '')
).trim(); ).trim();
if (!component) { if (!component) {
this.ctx.logger.error('[print.send] 平台快递组发送失败-打印组件缺失', { this.ctx.logger.error('[print.send] 平台快递组发送失败-打印组件缺失', {
@ -2437,8 +2443,8 @@ class PrintService extends MainService {
let requestPayload; let requestPayload;
let currentTaskCode = ''; let currentTaskCode = '';
try { try {
if (isShopifyPdf) { if (isPdfPrint) {
requestPayload = this.buildShopifyPdfPayload({ requestPayload = this.buildPdfPrintPayload({
waveGroup, waveGroup,
platformGroup, platformGroup,
orders: batchOrders, orders: batchOrders,
@ -2458,7 +2464,7 @@ class PrintService extends MainService {
} }
} }
currentTaskCode = await this.reserveUniquePrintTaskCode(); currentTaskCode = await this.reserveUniquePrintTaskCode();
if (isShopifyPdf) { if (isPdfPrint) {
requestPayload.printId = currentTaskCode; requestPayload.printId = currentTaskCode;
} }
if (requestPayload.task && typeof requestPayload.task === 'object' && !Array.isArray(requestPayload.task)) { if (requestPayload.task && typeof requestPayload.task === 'object' && !Array.isArray(requestPayload.task)) {
@ -2473,7 +2479,7 @@ class PrintService extends MainService {
...batchSummary, ...batchSummary,
platform: platformGroup && platformGroup.platform ? platformGroup.platform : '', platform: platformGroup && platformGroup.platform ? platformGroup.platform : '',
ordersCount: batchOrders.length, ordersCount: batchOrders.length,
documentCount: isShopifyPdf ? batchOrders.length : payloadSummary.documentCount, documentCount: isPdfPrint ? batchOrders.length : payloadSummary.documentCount,
}); });
} catch (error) { } catch (error) {
this.ctx.logger.error('[print.send] 组装 payload 失败', { this.ctx.logger.error('[print.send] 组装 payload 失败', {
@ -2509,7 +2515,7 @@ class PrintService extends MainService {
platform_name: platformName, platform_name: platformName,
sys_logistics_code: sysLogisticsCode, sys_logistics_code: sysLogisticsCode,
sys_logistics_name: sysLogisticsName, sys_logistics_name: sysLogisticsName,
data: isShopifyPdf data: isPdfPrint
? { ? {
...requestPayload, ...requestPayload,
component, component,
@ -2548,7 +2554,7 @@ class PrintService extends MainService {
}; };
} }
const printData = isShopifyPdf const printData = isPdfPrint
? { ? {
...requestPayload, ...requestPayload,
component, component,
@ -2570,7 +2576,7 @@ class PrintService extends MainService {
warehouse: normalizedPayload.warehouseId, warehouse: normalizedPayload.warehouseId,
computerId: normalizedPayload.printStation.computer_id, computerId: normalizedPayload.printStation.computer_id,
data: printData, data: printData,
printMethod: isShopifyPdf ? 'printer.printPdf' : 'printer.print', printMethod: isPdfPrint ? 'printer.printPdf' : 'printer.print',
}); });
} catch (error) { } catch (error) {
const sendDurationMs = Date.now() - sendStartAt; const sendDurationMs = Date.now() - sendStartAt;

View File

@ -2427,13 +2427,14 @@ ${waveFilterSql}
} }
/** /**
* 判断平台编码是否为 Shopify(PDF 面单,直接透传 pdf_base64)。 * 判断是否使用 PDF 直打组件。
* *
* @param {string} platformCode 平台编码。 * @param {string} platformCode 平台或打印组件编码。
* @return {boolean} 是否为 Shopify 平台。 * @return {boolean} 是否使用 PDF 直打。
*/ */
isShopifyPlatformCode(platformCode = '') { isPdfPrintPlatformCode(platformCode = '') {
return PrinterDataBuilder.normalizePlatformAlias(platformCode).platform === 'shopify'; const normalizedPlatformCode = String(platformCode || '').trim().toUpperCase();
return normalizedPlatformCode === 'PRINT_COMPONENT' || normalizedPlatformCode === 'SHOPIFY';
} }
/** /**
@ -3633,7 +3634,7 @@ ${waveFilterSql}
* @return {Object} 模板样例对象。 * @return {Object} 模板样例对象。
*/ */
loadTemplateSampleByPlatform(platformCode, cache) { loadTemplateSampleByPlatform(platformCode, cache) {
if (this.isShopifyPlatformCode(platformCode)) { if (this.isPdfPrintPlatformCode(platformCode)) {
return {}; return {};
} }
@ -3754,8 +3755,8 @@ ${waveFilterSql}
}, },
}); });
let printData = this.extractSinglePlatformPrintData(printPayload); let printData = this.extractSinglePlatformPrintData(printPayload);
if (this.isShopifyPlatformCode(platformCode)) { if (this.isPdfPrintPlatformCode(platformCode)) {
// Shopify PDF 放入前端可提取的 document.contents 结构中,不套平台模板骨架 // PDF 面单放入前端可提取的 document.contents 结构中,不套平台模板骨架
const pdfBase64 = String(orderExpress.pdf_base64 || '').trim(); const pdfBase64 = String(orderExpress.pdf_base64 || '').trim();
printData = { printData = {
documentID: documentId, documentID: documentId,

View File

@ -3,18 +3,59 @@
const assert = require('assert'); const assert = require('assert');
const PrintService = require('../app/service/systemMethods/print'); const PrintService = require('../app/service/systemMethods/print');
const PrintClientService = require('../app/service/systems/print'); const PrintClientService = require('../app/service/systems/print');
const OrderPrintSystemService = require('../app/service/systems/order_print');
const PrinterDataBuilder = require('../app/lib/printer_data_builder');
describe('Shopify PDF print payload', () => { describe('PDF print payload', () => {
const service = Object.create(PrintService.prototype); const service = Object.create(PrintService.prototype);
it('recognizes Shopify platform codes without case sensitivity', () => { it('recognizes PDF print component codes without case sensitivity', () => {
assert.strictEqual(service.isShopifyPdfPlatform('SHOPIFY'), true); assert.strictEqual(service.isPdfPrintPlatform('PRINT_COMPONENT'), true);
assert.strictEqual(service.isShopifyPdfPlatform('shopify'), true); assert.strictEqual(service.isPdfPrintPlatform('SHOPIFY'), true);
assert.strictEqual(service.isShopifyPdfPlatform('amazon'), false); assert.strictEqual(service.isPdfPrintPlatform('shopify'), true);
assert.strictEqual(service.isPdfPrintPlatform('amazon'), false);
});
it('maps PRINT_COMPONENT to Shopify PDF metadata and handling', () => {
const normalized = PrinterDataBuilder.normalizePlatformAlias('PRINT_COMPONENT');
const orderPrintService = Object.create(OrderPrintSystemService.prototype);
assert.strictEqual(normalized.platform, 'PRINT_COMPONENT');
assert.strictEqual(normalized.meta.name, '本机组件');
assert.strictEqual(orderPrintService.isPdfPrintPlatformCode('PRINT_COMPONENT'), true);
assert.strictEqual(orderPrintService.isPdfPrintPlatformCode('SHOPIFY'), true);
});
it('returns PDF contents when the stored platform code is PRINT_COMPONENT', async () => {
const orderPrintService = Object.create(OrderPrintSystemService.prototype);
orderPrintService.ctx = {
app: { config: {} },
model: { Store: { findByPk: async () => null } },
};
const result = await orderPrintService.buildOrderPlatformPrintItem({
orderRow: {
id: 1924092,
warehouse_id: 1000028,
sys_logistics_code: 'KJKYSD',
sys_logistics_name: '跨源速递',
},
orderExpress: {
platform_code: 'PRINT_COMPONENT',
waybill_code: 'WB-1924092',
pdf_base64: 'JVBERi0xLjQ=',
},
platformCode: 'PRINT_COMPONENT',
templateSample: {},
});
assert.strictEqual(result.platform, 'PRINT_COMPONENT');
assert.strictEqual(result.platform_name, '本机组件');
assert.deepStrictEqual(result.print_data.contents, [{ pdf_base64: 'JVBERi0xLjQ=' }]);
}); });
it('converts contents pdf_base64 into the printPdf payload shape', () => { it('converts contents pdf_base64 into the printPdf payload shape', () => {
const result = service.buildShopifyPdfPayload({ const result = service.buildPdfPrintPayload({
waveGroup: { wave_id: 12, wave_no: 'W-12' }, waveGroup: { wave_id: 12, wave_no: 'W-12' },
platformGroup: { platform: 'SHOPIFY', platform_name: 'Shopify' }, platformGroup: { platform: 'SHOPIFY', platform_name: 'Shopify' },
orders: [{ orders: [{
@ -36,7 +77,7 @@ describe('Shopify PDF print payload', () => {
it('rejects Shopify orders without PDF data', () => { it('rejects Shopify orders without PDF data', () => {
assert.throws( assert.throws(
() => service.buildShopifyPdfPayload({ () => service.buildPdfPrintPayload({
orders: [{ documentID: 'WAYBILL-1', contents: [] }], orders: [{ documentID: 'WAYBILL-1', contents: [] }],
}), }),
/缺少 pdf_base64/ /缺少 pdf_base64/