From e56f23b915e05cf28c8851a933f8298c90f097c0 Mon Sep 17 00:00:00 2001 From: tianyi <13521030702@163.com> Date: Tue, 7 Jul 2026 15:46:40 +0800 Subject: [PATCH] 1 --- components/print/escpos.js | 2 +- components/print/index.js | 180 ++++++++++++++++++++------------ components/tab-bar/myCenter.vue | 8 +- 3 files changed, 117 insertions(+), 73 deletions(-) diff --git a/components/print/escpos.js b/components/print/escpos.js index b3e235f..e32cbf6 100644 --- a/components/print/escpos.js +++ b/components/print/escpos.js @@ -4,7 +4,7 @@ */ const encode = require('./encoding'); var _ = { - LF: [0x0a], + LF: [0x0D, 0x0A], FS: [0x1c], FF: [0x0c], GS: [0x1d], diff --git a/components/print/index.js b/components/print/index.js index 9691016..ab20c2f 100644 --- a/components/print/index.js +++ b/components/print/index.js @@ -106,54 +106,72 @@ }, //写入控制命令 async writeBLECharacteristicValue(onDone) { - if(data1.receiverName == null){ - data1.receiverName = "***" - } - if(data1.receiverAddress == null){ - data1.receiverAddress = '***' - } - if(data1.deliveryInfo == null){ - - data1.deliveryInfo = { - mustFinishTime:"***" - } - } - - - let printerJobs = new PrinterJobs(); - printerJobs.setAlign('ct').setSize(2, 2).setLineSpacing(80).print(data1.numberCode + ((data1.status == 7 || data1.status == 11)? '待退款' :data1.deliveryType == 1?'配送':data1.isPack== 0?'自取-堂食':data1.isPack== 1?'自取-打包':'自取')) - .setAlign('lt').setSize(1, 1).setLineSpacing(40).print('预计时间:'+ (data1.deliveryInfo == null?'暂无':this.formatTime(data1.deliveryInfo.mustFinishTime))) - .setAlign('lt').setSize(1, 1).setLineSpacing(40).print(data1.shopName) - .print(printerUtil.fillLine('-', 1, 50)) - .setAlign('lt').setSize(2, 2).setLineSpacing(80).print(data1.receiverName == null?'暂无':data1.receiverName) - .setAlign('lt').setSize(1, 1).setLineSpacing(40).print('联系电话: '+data1.receiverPhone) - .setAlign('lt').setSize(2, 2).setLineSpacing(80).print('地址:'+(data1.receiverAddress == null?'暂无':data1.receiverAddress)) - .setAlign('lt').setSize(1, 1).setLineSpacing(40).print('为保护隐私,顾客电话及地址已被隐藏,您可登录商家端或骑手端查看') - .print(printerUtil.fillLine('-', 1, 50)); - - - for (var i = 0; i < data1.goodsList.length; i++) { - printerJobs.setAlign('lt').setSize(1, 1).setLineSpacing(40).print(data1.goodsList[i].productName) - if(!data1.goodsList[i].specs){ - 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) - .setAlign('lt').setSize(1, 1).setLineSpacing(40).print('合计金额:'+ data1.totalAmount) - .print(printerUtil.fillLine('-', 1, 50)) - .setAlign('lt').setSize(1, 1).setLineSpacing(40).print('商品取到后,如有任何商品问题,请及时联系商家和平台,客服将为您服务,客服电话15533910775,谢谢您的惠顾') - .println(); - let buffer = printerJobs.buffer(); - this.sendPrint(buffer, onDone); + console.log('数据啊----------', data1) + + let printerJobs = new PrinterJobs(); + + printerJobs.setAlign('ct').setSize(2, 2).setLineSpacing(60).print( + data1.numberCode + ( + (data1.status == 7 || data1.status == 11) ? '待退款' : + data1.deliveryType == 1 ? '配送' : + data1.isPack == 0 ? '到店-堂食' : + data1.isPack == 1 ? '到店-打包' : '到店' + ) + ); + printerJobs.setSize(1, 1).setLineSpacing(40); + + printerJobs.setAlign('lt').print( + '预计时间:' + (data1.deliveryInfo == null ? '暂无' : this.formatTime(data1.deliveryInfo.mustFinishTime)) + ); + printerJobs.setAlign('lt').print(data1.shopName); + + printerJobs.print(printerUtil.fillLine('-', 1, 32)); + + printerJobs.setAlign('lt').setSize(2, 2).setLineSpacing(60).print( + data1.receiverName == null ? '暂无' : data1.receiverName + ); + printerJobs.setSize(1, 1).setLineSpacing(40); + + printerJobs.setAlign('lt').print('联系电话: ' + data1.receiverPhone); + + printerJobs.setAlign('lt').setSize(2, 2).setLineSpacing(60).print( + '地址:' + (data1.receiverAddress == null ? '暂无' : data1.receiverAddress) + ); + printerJobs.setSize(1, 1).setLineSpacing(40); + + printerJobs.setAlign('lt').print('顾客电话及地址已被隐藏,您可登录商家端或骑手端查看'); + + printerJobs.print(printerUtil.fillLine('-', 1, 32)); + + for (var i = 0; i < data1.goodsList.length; i++) { + printerJobs.setAlign('lt').print(data1.goodsList[i].productName); + if (data1.goodsList[i].specs) { + printerJobs.print(this.delNode(data1.goodsList[i].specs)); + } + printerJobs.setLineSpacing(30).print( + printerUtil.inlineArr([{ + 'text': data1.goodsList[i].price, + 'width': 10, + }, { + 'text': 'X' + data1.goodsList[i].quantity, + 'width': 20, + }], 1, 32) + ); + } + + printerJobs.print(printerUtil.fillLine('-', 1, 32)); + + printerJobs.setAlign('lt').print('配送费:' + data1.deliveryFee); + printerJobs.print('合计金额:' + data1.totalAmount); + + printerJobs.print(printerUtil.fillLine('-', 1, 32)); + + printerJobs.setAlign('lt').print('商品取到后,如有任何商品问题,请及时联系商家和平台,客服将为您服务,客服电话 15533910775,谢谢您的惠顾'); + + printerJobs.lineFeed(3); + + let buffer = printerJobs.buffer(); + this.sendPrint(buffer, onDone); }, formatTime(value) { if (!value) return ''; @@ -199,27 +217,51 @@ return result; }, sendPrint(buffer, onDone) { - // 1.并行调用多次会存在写失败的可能性 - // 2.建议每次写入不超过20字节 - // 分包处理,延时调用 - let that = this - const maxChunk = 20; - const delay = 20; - isPrint = true; - const totalPackages = Math.ceil(buffer.byteLength / maxChunk); - for (let i = 0, j = 0, length = buffer.byteLength; i < length; i += maxChunk, j++) { - let subPackage = buffer.slice(i, i + maxChunk <= length ? (i + maxChunk) : length); - // 最后一个分包发送完成后,标记打印结束并触发回调 - if (j === totalPackages - 1) { - setTimeout(() => { - that.sendPrintData(subPackage); - isPrint = false; - if (typeof onDone === 'function') onDone(); - }, j * delay); - } else { - setTimeout(this.sendPrintData, j * delay, subPackage); - } - } + const maxChunk = 20; // 每次20字节 + const delay = 50; // 间隔50ms + isPrint = true; + + const totalLength = buffer.byteLength; + let currentIndex = 0; + + // 创建源数据的视图,方便读取 + const srcView = new Uint8Array(buffer); + + const sendNextChunk = () => { + if (currentIndex >= totalLength) { + // 发送完毕 + isPrint = false; + if (typeof onDone === 'function') onDone(); + return; + } + + const end = Math.min(currentIndex + maxChunk, totalLength); + const chunkLength = end - currentIndex; + + // 创建一个全新的、长度精确的 ArrayBuffer + const valueToSend = new ArrayBuffer(chunkLength); + const dstView = new Uint8Array(valueToSend); + + // 将源数据拷贝到新的 ArrayBuffer 中 + dstView.set(srcView.subarray(currentIndex, end)); + + console.log('发送分包:', currentIndex, '长度:', chunkLength); + + // 【核心修复】:直接调用 bluetooth 对象封装好的方法 + // 它内部会自动使用正确的 bluetooth.deviceId, bluetooth.serviceId, bluetooth.writeId + bluetooth.writeBLECharacteristicValue(valueToSend).then(() => { + currentIndex = end; + // 成功后,延迟再发下一个 + setTimeout(sendNextChunk, delay); + }).catch((err) => { + console.error('发送失败:', err); + isPrint = false; + if (typeof onDone === 'function') onDone(err); + }); + }; + + // 开始发送第一个包 + sendNextChunk(); }, sendPrintData(buffer) { bluetooth.writeBLECharacteristicValue(buffer).then(res => {}); diff --git a/components/tab-bar/myCenter.vue b/components/tab-bar/myCenter.vue index 208abfa..d212ee9 100644 --- a/components/tab-bar/myCenter.vue +++ b/components/tab-bar/myCenter.vue @@ -49,17 +49,17 @@ --> - +