From a9a3af33f2b43f0ae71627736fcdbb5957a011c0 Mon Sep 17 00:00:00 2001 From: tianyi <13521030702@163.com> Date: Tue, 14 Apr 2026 10:24:11 +0800 Subject: [PATCH] 11 --- components/print/index.js | 196 ++++++++++++++++++++++++++++++++ components/tab-bar/delivery.vue | 27 ++--- 2 files changed, 203 insertions(+), 20 deletions(-) create mode 100644 components/print/index.js diff --git a/components/print/index.js b/components/print/index.js new file mode 100644 index 0000000..560edb4 --- /dev/null +++ b/components/print/index.js @@ -0,0 +1,196 @@ + + import PrinterJobs from '@/components/print/printerjobs.js' + import printerUtil from '@/components/print/printerutil.js' + import util from '@/components/print/util.js' + import drawQrcode from '@/components/print/weapp.qrcode.esm.js' + import Bluetooth from '@/components/print/bluetooth.js' + let bluetooth = new Bluetooth(); + let isPrint= false + export default { + getData() { + var that = this; + if(uni.getStorageSync('bluetoothDeviceId')){ + let id = uni.getStorageSync('bluetoothDeviceId') + this.bindViewTap(id,false) + }else{ + uni.showModal({ + title: '提示', + content: '您还没有连接打印机,将无法为您打印小票!' + }); + } + }, + async bindViewTap(deviceId, state) { + var _this = this; + console.log(deviceId) + try { + //1.链接设备 + await bluetooth.createBLEConnection(deviceId).then((res) => { + bluetooth.deviceId = deviceId; + }).catch((e) => { + throw e; + }); + + let server = []; + //2.寻找服务 + await bluetooth.getBLEDeviceServices().then((res) => { + console.log(res) + bluetooth.notifyId = ''; + bluetooth.writeId = ''; + bluetooth.num = 0; + bluetooth.serviceList = res + }).catch((e) => { + throw e; + }); + + //3.获取蓝牙设备所有服务 + let result3 = await bluetooth.getBLEDeviceCharacteristics().then(res => { + uni.setStorageSync('bluetoothDeviceId', deviceId); + this.pickUpOnce() + }).catch((e) => { + throw e; + }); + } catch (e) { + //TODO handle the exception + console.log("e: " + JSON.stringify(e)); + } + }, + //打印一次 + pickUpOnce() { + isPrint = true; + bluetooth.notifyBLECharacteristicValue(); + let self = this; + setTimeout(() => { + self.writeBLECharacteristicValue(); + }, 500); + }, + //写入控制命令 + async writeBLECharacteristicValue() { + let data = { + "settled_name": "#0112配送", + "detail": { + "total_amount": "137.00", + "other_amount": "0.00", + "discount_amount": "0.00", + "actual_amount": "137.00", + "order_sn": "20220512165337", + "goods_count": 3, + "order_time": "2022-05-12 16:53:00", + "supplier_id": 1 + }, + "code": "https://www.oulaisong.com/purchase?id=8", + "list": [{ + "id": 8, + "goods_name": "红烧带鱼", + "article_number": "1", + 'sum_price':'16.00' + }, { + "id": 9, + "goods_name": "女童外套", + "article_number": "2", + + "sum_price": "34.00" + }, { + "id": 10, + "goods_name": "25", + "article_number": "3", + "sum_price": "3.00" + }] + } + let printerJobs = new PrinterJobs(); + printerJobs.setAlign('ct').setSize(2, 2).setLineSpacing(80).print('#0112配送') + .setAlign('lt').setSize(1, 1).setLineSpacing(40).print('预计时间:2026-02-01 13:41:00') + .setAlign('lt').setSize(1, 1).setLineSpacing(40).print('七里香快餐明珠店') + .print(printerUtil.fillLine('-', 1, 50)) + .setAlign('lt').setSize(2, 2).setLineSpacing(80).print('王先生') + .setAlign('lt').setSize(1, 1).setLineSpacing(40).print('联系电话: *******0775') + .setAlign('lt').setSize(2, 2).setLineSpacing(80).print('地址:黑背生东川建设集团有限公司黑恶生运河区****') + .setAlign('lt').setSize(1, 1).setLineSpacing(40).print('为保护隐私,顾客电话及地址已被隐藏,您可登录商家端或骑手端查看') + .print(printerUtil.fillLine('-', 1, 50)); + + + for (var i = 0; i < data.list.length; i++) { + printerJobs.setAlign('lt').setSize(1, 1).setLineSpacing(40).print(data.list[i].goods_name) + + + let setLineSpacing = 40 + + printerJobs.setLineSpacing(setLineSpacing).print(printerUtil.inlineArr([{ + 'text': 'X' + data.list[i].article_number, + 'width': 20, + }, { + 'text': data.list[i].sum_price, + 'width': 10, + }], 1, 50)); + } + printerJobs.print(printerUtil.fillLine('-', 1, 50)) + .setAlign('lt').setSize(1, 1).setLineSpacing(40).print('配送费:1圆') + .setAlign('lt').setSize(1, 1).setLineSpacing(40).print('合计金额:17圆') + .print(printerUtil.fillLine('-', 1, 50)) + .setAlign('lt').setSize(1, 1).setLineSpacing(40).print('商品取到后,如有任何商品问题,请及时联系商家和平台,客服将为您服务,客服电话15533910775,谢谢您的惠顾') + .println(); + let buffer = printerJobs.buffer(); + this.sendPrint(buffer); + }, + sendPrint(buffer) { + // 1.并行调用多次会存在写失败的可能性 + // 2.建议每次写入不超过20字节 + // 分包处理,延时调用 + let that = this + const maxChunk = 20; + const delay = 20; + isPrint = true; + 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); + setTimeout(this.sendPrintData, j * delay, subPackage); + } + isPrint = false; + }, + sendPrintData(buffer) { + bluetooth.writeBLECharacteristicValue(buffer).then(res => {}); + }, + startSearch(state) { + let that = this + bluetooth.openBluetoothAdapter().then(e => { + + uni.startBluetoothDevicesDiscovery({ + success: res => { + uni.onBluetoothDeviceFound(result => { + let arr = that.list; + let devices = []; + let list = result.devices; + for (let i = 0; i < list.length; ++i) { + console.log(list[i]) + if (list[i].name && list[i].name != "未知设备") { + let arrNew = arr.filter((item) => { + return item.deviceId == list[i].deviceId; + }); + if (arrNew.length == 0) { + devices.push(list[i]); + } + if (uni.getStorageSync('bluetoothDeviceId') == list[i] + .deviceId) { + // that.bindViewTap(list[i].deviceId, state) + } + } + } + that.list = arr.concat(devices); + }); + }, + fail: res => { + uni.hideLoading(); + uni.showToast({ + title: `搜索设备失败` + JSON.stringify(err) + }) + } + }) + }).catch(e => { + console.log(e) + }); + }, + // 停止搜索 + stopSearch() { + + uni.hideLoading(); + bluetooth.stopBluetoothDevicesDiscovery(); + } + } \ No newline at end of file diff --git a/components/tab-bar/delivery.vue b/components/tab-bar/delivery.vue index f27dd4e..46ead4d 100644 --- a/components/tab-bar/delivery.vue +++ b/components/tab-bar/delivery.vue @@ -63,7 +63,7 @@ shopName:'', searchStatus:10, shopId:uni.getStorageSync('shopId'), - regionId:JSON.parse(uni.getStorageSync('area')).id + regionId:JSON.parse(uni.getStorageSync('area')).regionId }, totalPages: 1, orderList:[], @@ -72,16 +72,8 @@ name: '全部', value: 10, checked: true - },{ - name: '待支付', - value: 0, - checked: true - }, - { - name: '待成团', - value: 1, - checked: false }, + { name: '待消费', value: 2, @@ -93,12 +85,12 @@ checked: false }, { - name: '待取货', + name: '待发货', value: 4, checked: false }, { - name: '待送达', + name: '已发货', value: 5, checked: false }, @@ -112,11 +104,6 @@ value: 7, checked: false }, - { - name: '已退款', - value: 8, - checked: false - }, { name: '已取消', value: 9, @@ -190,7 +177,7 @@ }, goDetail(id){ uni.navigateTo({ - url:'/package2/shop/orderDetail?id=' + id + url:'/pages/shop/orderDetail?id=' + id }) }, handleStatusClick(index,value) { @@ -222,7 +209,7 @@ .title { background: url('https://jewel-shop.oss-cn-beijing.aliyuncs.com/8bc15960c2dc40268e295d6dd23aecce.png') no-repeat; width: 100%; - height: 20%; + height: 400rpx; } .title-sreach { @@ -242,7 +229,7 @@ .title-tab { display: flex; - margin: -120rpx auto 0; + margin: -200rpx auto 0; } .status-scroll-view {