|
|
|
@ -8,7 +8,8 @@ |
|
|
|
let isPrint= false |
|
|
|
let data1 = {} |
|
|
|
export default { |
|
|
|
getData() { |
|
|
|
// onDone: 打印完成(或失败/跳过)后执行的回调,用于队列驱动
|
|
|
|
getData(onDone) { |
|
|
|
var that = this; |
|
|
|
if(uni.getStorageSync('bluetoothDeviceId')){ |
|
|
|
let id = uni.getStorageSync('bluetoothDeviceId') |
|
|
|
@ -25,13 +26,14 @@ |
|
|
|
console.log('连接成功了!!!!!!!!!!!!',res) |
|
|
|
if (res.data.code == 200) { |
|
|
|
data1 = res.data.result |
|
|
|
that.bindViewTap(id,false) |
|
|
|
|
|
|
|
that.bindViewTap(id, false, onDone) |
|
|
|
} else { |
|
|
|
uni.showToast({ |
|
|
|
title: res.data.message, |
|
|
|
icon: 'none' |
|
|
|
}); |
|
|
|
// 请求失败也需要通知队列继续处理下一条
|
|
|
|
if (typeof onDone === 'function') onDone(); |
|
|
|
return; |
|
|
|
} |
|
|
|
uni.hideLoading(); |
|
|
|
@ -39,6 +41,8 @@ |
|
|
|
fail: function(err) { |
|
|
|
console.log(err) |
|
|
|
uni.hideLoading(); |
|
|
|
// 请求失败也需要通知队列继续处理下一条
|
|
|
|
if (typeof onDone === 'function') onDone(); |
|
|
|
} |
|
|
|
}) |
|
|
|
}else{ |
|
|
|
@ -53,11 +57,13 @@ |
|
|
|
url:'/pages/myCenter/setPrint' |
|
|
|
}) |
|
|
|
} |
|
|
|
// 未连接打印机也通知队列继续(跳过当前条)
|
|
|
|
if (typeof onDone === 'function') onDone(); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
}, |
|
|
|
async bindViewTap(deviceId, state) { |
|
|
|
async bindViewTap(deviceId, state, onDone) { |
|
|
|
var _this = this; |
|
|
|
console.log(deviceId) |
|
|
|
try { |
|
|
|
@ -83,26 +89,28 @@ |
|
|
|
//3.获取蓝牙设备所有服务
|
|
|
|
let result3 = await bluetooth.getBLEDeviceCharacteristics().then(res => { |
|
|
|
uni.setStorageSync('bluetoothDeviceId', deviceId); |
|
|
|
this.pickUpOnce() |
|
|
|
this.pickUpOnce(onDone) |
|
|
|
}).catch((e) => { |
|
|
|
throw e; |
|
|
|
}); |
|
|
|
} catch (e) { |
|
|
|
//TODO handle the exception
|
|
|
|
console.log("e: " + JSON.stringify(e)); |
|
|
|
// 蓝牙连接异常也需要通知队列继续
|
|
|
|
if (typeof onDone === 'function') onDone(); |
|
|
|
} |
|
|
|
}, |
|
|
|
//打印一次
|
|
|
|
pickUpOnce() { |
|
|
|
pickUpOnce(onDone) { |
|
|
|
isPrint = true; |
|
|
|
bluetooth.notifyBLECharacteristicValue(); |
|
|
|
let self = this; |
|
|
|
setTimeout(() => { |
|
|
|
self.writeBLECharacteristicValue(); |
|
|
|
self.writeBLECharacteristicValue(onDone); |
|
|
|
}, 500); |
|
|
|
}, |
|
|
|
//写入控制命令
|
|
|
|
async writeBLECharacteristicValue() { |
|
|
|
async writeBLECharacteristicValue(onDone) { |
|
|
|
console.log("走到这里已经拿到数据了",data1) |
|
|
|
if(data1.receiverName == null){ |
|
|
|
console.log("22222") |
|
|
|
@ -154,9 +162,9 @@ |
|
|
|
.setAlign('lt').setSize(1, 1).setLineSpacing(40).print('商品取到后,如有任何商品问题,请及时联系商家和平台,客服将为您服务,客服电话15533910775,谢谢您的惠顾') |
|
|
|
.println(); |
|
|
|
let buffer = printerJobs.buffer(); |
|
|
|
this.sendPrint(buffer); |
|
|
|
this.sendPrint(buffer, onDone); |
|
|
|
}, |
|
|
|
sendPrint(buffer) { |
|
|
|
sendPrint(buffer, onDone) { |
|
|
|
// 1.并行调用多次会存在写失败的可能性
|
|
|
|
// 2.建议每次写入不超过20字节
|
|
|
|
// 分包处理,延时调用
|
|
|
|
@ -164,11 +172,20 @@ |
|
|
|
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); |
|
|
|
setTimeout(this.sendPrintData, j * delay, subPackage); |
|
|
|
// 最后一个分包发送完成后,标记打印结束并触发回调
|
|
|
|
if (j === totalPackages - 1) { |
|
|
|
setTimeout(() => { |
|
|
|
that.sendPrintData(subPackage); |
|
|
|
isPrint = false; |
|
|
|
if (typeof onDone === 'function') onDone(); |
|
|
|
}, j * delay); |
|
|
|
} else { |
|
|
|
setTimeout(this.sendPrintData, j * delay, subPackage); |
|
|
|
} |
|
|
|
} |
|
|
|
isPrint = false; |
|
|
|
}, |
|
|
|
sendPrintData(buffer) { |
|
|
|
bluetooth.writeBLECharacteristicValue(buffer).then(res => {}); |
|
|
|
|