wangfukang 1 month ago
parent
commit
dbc61705c4
  1. 42
      App.vue
  2. 41
      components/print/index.js

42
App.vue

@ -69,7 +69,8 @@
$t: '',
userInfo: {},
registrationID: '',
printData: []
printData: [], //
isPrinting: false, //
},
onLaunch: function() {
let context = this;
@ -112,18 +113,22 @@
console.log("通知", result)
if (result.content.includes('订单出餐已超时')) {
this.onPlayAudio(2)
} else{
if(result.content.includes('您有一笔新的订单')){
} else {
if (result.content.includes('您有一笔新的订单')) {
this.onPlayAudio(4)
}else if(result.content.includes('您有一笔新的到店订单')){
} else if (result.content.includes('您有一笔新的到店订单')) {
this.onPlayAudio(3)
} else if(result.content.includes('您有一笔订单申请退款')){
} else if (result.content.includes('您有一笔订单申请退款')) {
this.onPlayAudio(1)
}
//
this.globalData.printData.push(JSON.parse(result.android.inAppExtras))
printer.getData()
console.log("通知入队,当前队列长度:", this.globalData.printData.length)
//
if (!this.globalData.isPrinting) {
this.processPrintQueue()
}
}
console.log("通知数据", this.globalData.printData)
});
jpushModule.isPushStopped(res => {
@ -183,6 +188,29 @@
},
},
methods: {
/**
* 打印队列处理器取队头数据打印打印完成后删除队头继续处理下一条
* 保证同一时间只有一条消息在打印按接收顺序依次执行
*/
processPrintQueue() {
// 退
if (this.globalData.printData.length === 0) {
this.globalData.isPrinting = false
console.log("打印队列已全部处理完毕")
return
}
//
this.globalData.isPrinting = true
console.log("开始打印队头数据,剩余队列长度:", this.globalData.printData.length)
// onDone
printer.getData(() => {
//
this.globalData.printData.shift()
console.log("本条打印完成,队列剩余:", this.globalData.printData.length)
//
this.processPrintQueue()
})
},
onPlayAudio(type) {
var audio = null;
audio = uni.getBackgroundAudioManager();

41
components/print/index.js

@ -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 => {});

Loading…
Cancel
Save