diff --git a/app/lib/printer_data_builder.js b/app/lib/printer_data_builder.js index 6b5ad20..d12e97e 100644 --- a/app/lib/printer_data_builder.js +++ b/app/lib/printer_data_builder.js @@ -67,7 +67,7 @@ const PLATFORM_MAP = Object.freeze({ aliases: [ 'xhs', 'xiaohongshu', 'red' ], template_file: 'xhs.json', }), - shopify: Object.freeze({ + PRINT_COMPONENT: Object.freeze({ name: '本机组件', aliases: [ 'shopify' ], template_file: 'shopify.json', @@ -222,7 +222,7 @@ function normalizePlatformAlias(inputPlatform) { for (const platform of Object.keys(PLATFORM_MAP)) { 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 }; } } diff --git a/app/service/systemMethods/print.js b/app/service/systemMethods/print.js index 3e1e5f1..f20e460 100644 --- a/app/service/systemMethods/print.js +++ b/app/service/systemMethods/print.js @@ -282,11 +282,17 @@ class PrintService extends MainService { 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 orders = Array.isArray(normalizedPayload.orders) ? normalizedPayload.orders : []; const orderIds = Array.isArray(normalizedPayload.orderIds) ? normalizedPayload.orderIds : []; @@ -301,7 +307,7 @@ class PrintService extends MainService { const pdfOrders = orders.map((order, index) => { if (!order || typeof order !== 'object' || Array.isArray(order)) { - throw new Error(`第 ${index + 1} 个 Shopify 面单数据无效`); + throw new Error(`第 ${index + 1} 个 PDF 面单数据无效`); } let contents = order.contents; @@ -309,7 +315,7 @@ class PrintService extends MainService { try { contents = JSON.parse(contents); } catch (error) { - throw new Error(`Shopify 面单 contents 格式无效: ${error.message}`); + throw new Error(`PDF 面单 contents 格式无效: ${error.message}`); } } const contentRows = Array.isArray(contents) @@ -331,7 +337,7 @@ class PrintService extends MainService { || order.documentID || order.document_id; if (!pdfBase64) { - throw new Error(`Shopify 面单 ${documentId || index + 1} 缺少 pdf_base64`); + throw new Error(`PDF 面单 ${documentId || index + 1} 缺少 pdf_base64`); } return { @@ -1002,7 +1008,7 @@ class PrintService extends MainService { } 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 ? { success: true, data: {} } : await this.loadExpressPlatformComponents({ platformCodes: componentPlatformCodes }); @@ -2352,12 +2358,12 @@ class PrintService extends MainService { this.ctx.logger.info('[print.send] 平台快递组发送开始', groupSummary); const platformCode = String(platformGroup && platformGroup.platform ? platformGroup.platform : '').trim(); - const isShopifyPdf = this.isShopifyPdfPlatform(platformCode); + const isPdfPrint = this.isPdfPrintPlatform(platformCode); const component = String( normalizedPayload.platformComponents && normalizedPayload.platformComponents[platformCode] ? normalizedPayload.platformComponents[platformCode] - : (isShopifyPdf ? 'PDF' : '') + : (isPdfPrint ? 'PDF' : '') ).trim(); if (!component) { this.ctx.logger.error('[print.send] 平台快递组发送失败-打印组件缺失', { @@ -2437,8 +2443,8 @@ class PrintService extends MainService { let requestPayload; let currentTaskCode = ''; try { - if (isShopifyPdf) { - requestPayload = this.buildShopifyPdfPayload({ + if (isPdfPrint) { + requestPayload = this.buildPdfPrintPayload({ waveGroup, platformGroup, orders: batchOrders, @@ -2458,7 +2464,7 @@ class PrintService extends MainService { } } currentTaskCode = await this.reserveUniquePrintTaskCode(); - if (isShopifyPdf) { + if (isPdfPrint) { requestPayload.printId = currentTaskCode; } if (requestPayload.task && typeof requestPayload.task === 'object' && !Array.isArray(requestPayload.task)) { @@ -2473,7 +2479,7 @@ class PrintService extends MainService { ...batchSummary, platform: platformGroup && platformGroup.platform ? platformGroup.platform : '', ordersCount: batchOrders.length, - documentCount: isShopifyPdf ? batchOrders.length : payloadSummary.documentCount, + documentCount: isPdfPrint ? batchOrders.length : payloadSummary.documentCount, }); } catch (error) { this.ctx.logger.error('[print.send] 组装 payload 失败', { @@ -2509,7 +2515,7 @@ class PrintService extends MainService { platform_name: platformName, sys_logistics_code: sysLogisticsCode, sys_logistics_name: sysLogisticsName, - data: isShopifyPdf + data: isPdfPrint ? { ...requestPayload, component, @@ -2548,7 +2554,7 @@ class PrintService extends MainService { }; } - const printData = isShopifyPdf + const printData = isPdfPrint ? { ...requestPayload, component, @@ -2570,7 +2576,7 @@ class PrintService extends MainService { warehouse: normalizedPayload.warehouseId, computerId: normalizedPayload.printStation.computer_id, data: printData, - printMethod: isShopifyPdf ? 'printer.printPdf' : 'printer.print', + printMethod: isPdfPrint ? 'printer.printPdf' : 'printer.print', }); } catch (error) { const sendDurationMs = Date.now() - sendStartAt; diff --git a/app/service/systems/order_print.js b/app/service/systems/order_print.js index 8787293..b542ffd 100644 --- a/app/service/systems/order_print.js +++ b/app/service/systems/order_print.js @@ -2427,13 +2427,14 @@ ${waveFilterSql} } /** - * 判断平台编码是否为 Shopify(PDF 面单,直接透传 pdf_base64)。 + * 判断是否使用 PDF 直打组件。 * - * @param {string} platformCode 平台编码。 - * @return {boolean} 是否为 Shopify 平台。 + * @param {string} platformCode 平台或打印组件编码。 + * @return {boolean} 是否使用 PDF 直打。 */ - isShopifyPlatformCode(platformCode = '') { - return PrinterDataBuilder.normalizePlatformAlias(platformCode).platform === 'shopify'; + isPdfPrintPlatformCode(platformCode = '') { + const normalizedPlatformCode = String(platformCode || '').trim().toUpperCase(); + return normalizedPlatformCode === 'PRINT_COMPONENT' || normalizedPlatformCode === 'SHOPIFY'; } /** @@ -3633,7 +3634,7 @@ ${waveFilterSql} * @return {Object} 模板样例对象。 */ loadTemplateSampleByPlatform(platformCode, cache) { - if (this.isShopifyPlatformCode(platformCode)) { + if (this.isPdfPrintPlatformCode(platformCode)) { return {}; } @@ -3754,8 +3755,8 @@ ${waveFilterSql} }, }); let printData = this.extractSinglePlatformPrintData(printPayload); - if (this.isShopifyPlatformCode(platformCode)) { - // Shopify PDF 放入前端可提取的 document.contents 结构中,不套平台模板骨架 + if (this.isPdfPrintPlatformCode(platformCode)) { + // PDF 面单放入前端可提取的 document.contents 结构中,不套平台模板骨架 const pdfBase64 = String(orderExpress.pdf_base64 || '').trim(); printData = { documentID: documentId, diff --git a/test/print_shopify_pdf.unit.test.js b/test/print_shopify_pdf.unit.test.js index 8a8dd11..461660a 100644 --- a/test/print_shopify_pdf.unit.test.js +++ b/test/print_shopify_pdf.unit.test.js @@ -3,18 +3,59 @@ const assert = require('assert'); const PrintService = require('../app/service/systemMethods/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); - it('recognizes Shopify platform codes without case sensitivity', () => { - assert.strictEqual(service.isShopifyPdfPlatform('SHOPIFY'), true); - assert.strictEqual(service.isShopifyPdfPlatform('shopify'), true); - assert.strictEqual(service.isShopifyPdfPlatform('amazon'), false); + it('recognizes PDF print component codes without case sensitivity', () => { + assert.strictEqual(service.isPdfPrintPlatform('PRINT_COMPONENT'), true); + assert.strictEqual(service.isPdfPrintPlatform('SHOPIFY'), true); + 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', () => { - const result = service.buildShopifyPdfPayload({ + const result = service.buildPdfPrintPayload({ waveGroup: { wave_id: 12, wave_no: 'W-12' }, platformGroup: { platform: 'SHOPIFY', platform_name: 'Shopify' }, orders: [{ @@ -36,7 +77,7 @@ describe('Shopify PDF print payload', () => { it('rejects Shopify orders without PDF data', () => { assert.throws( - () => service.buildShopifyPdfPayload({ + () => service.buildPdfPrintPayload({ orders: [{ documentID: 'WAYBILL-1', contents: [] }], }), /缺少 pdf_base64/