diff --git a/components/print/index.js b/components/print/index.js index 7164abb..97e67dc 100644 --- a/components/print/index.js +++ b/components/print/index.js @@ -134,17 +134,15 @@ for (var i = 0; i < data1.goodsList.length; i++) { printerJobs.setAlign('lt').setSize(1, 1).setLineSpacing(40).print(data1.goodsList[i].productName) - - - let setLineSpacing = 40 - - printerJobs.setLineSpacing(setLineSpacing).print(printerUtil.inlineArr([{ - 'text': 'X' + data1.goodsList[i].quantity, - 'width': 20, - }, { - 'text': data1.goodsList[i].price, - 'width': 10, - }], 1, 50)); + printerJobs.setAlign('lt').setSize(1, 1).setLineSpacing(40).print(this.delNode(data1.goodsList[i].specs)) + let setLineSpacing = 40 + printerJobs.setLineSpacing(setLineSpacing).print(printerUtil.inlineArr([{ + 'text': data1.goodsList[i].price, + 'width': 10, + }, { + 'text': 'X' + data1.goodsList[i].quantity, + 'width': 20, + }], 1, 50)); } printerJobs.print(printerUtil.fillLine('-', 1, 50)) .setAlign('lt').setSize(1, 1).setLineSpacing(40).print('配送费:'+ data1.deliveryFee) @@ -155,6 +153,23 @@ let buffer = printerJobs.buffer(); this.sendPrint(buffer, onDone); }, + delNode(data) { + // 1. 统一转为字符串 + let str; + if (typeof data === 'object' && data !== null) { + // 对象或数组 → JSON 字符串 + str = JSON.stringify(data); + } else if (typeof data === 'string') { + str = data; + } else { + // 其他类型(数字、布尔等)先转字符串 + str = String(data); + } + + // 2. 删除所有大括号 { } 和双引号 " + const result = str.replace(/[{}"]/g, ''); + return result; + }, sendPrint(buffer, onDone) { // 1.并行调用多次会存在写失败的可能性 // 2.建议每次写入不超过20字节