import gbk from '@/components/kk-printer/utils/printUtil-GBK.js'; import * as blesdk from './utils/bluetoolth'; import util from './utils/util.js'; var bufferData = [] var deviceType = 'ios' export default { onPrint() { let that = this that.yNum = 10 let strCmd = blesdk.CreatCPCLPage(440, 1000, 1, 0); strCmd += blesdk.addCPCLSETMAG(2, 2) strCmd += blesdk.addCPCLLocation(2); strCmd += blesdk.addCPCLSETBOLD('400'); // strCmd += blesdk.addCPCLText('0', that.yNum, '24', '2', 0, getApp().globalData.printData[0].order_id) strCmd += blesdk.addCPCLText('0', that.yNum, '24', '2', 0, "#0112 外卖单") strCmd += blesdk.addCPCLLocation(0); strCmd += blesdk.addCPCLSETBOLD('0') strCmd += blesdk.addCPCLSETMAG(1, 1) strCmd += blesdk.addCPCLText('0', that.yNum+=60, '24', '0', 0, '七里香快餐明珠精品馆') strCmd += blesdk.addCPCLSETMAG(1, 1) strCmd += blesdk.addCPCLText('0', that.yNum += 30, '24', '0', 0, "预计时间:2026-02-01 13:14:00") strCmd += blesdk.addCPCLSETMAG(1, 1) strCmd += blesdk.addCPCLText('0', that.yNum += 30, '24', '0', 0, "*********") strCmd += blesdk.addCPCLSETMAG(2, 2) strCmd += blesdk.addCPCLText('0', that.yNum += 30, '24', '1', 0, '王 先生') strCmd += blesdk.addCPCLSETMAG(1, 1) strCmd += blesdk.addCPCLText('0', that.yNum += 50, '24', '0', 0, '联系电话: *******0775') strCmd += blesdk.addCPCLSETMAG(2, 2) strCmd += blesdk.addCPCLText('0', that.yNum += 50, '24', '0', 0, '地址:河北东川建设集团有限公司(河北省沧州市运河区)******') strCmd += blesdk.addCPCLSETMAG(1, 1) strCmd += blesdk.addCPCLText('0', that.yNum += 50, '24', '0', 0, '为保护隐私,顾客电话及地址已隐藏,') strCmd += blesdk.addCPCLSETMAG(1, 1) strCmd += blesdk.addCPCLText('0', that.yNum += 30, '24', '0', 0, '您可登陆商家端或骑手端查看') strCmd += blesdk.addCPCLSETMAG(1, 1) strCmd += blesdk.addCPCLText('0', that.yNum += 30, '24', '0', 0, '*******************************************') strCmd += blesdk.addCPCLSETMAG(2, 2) strCmd += blesdk.addCPCLText('0', that.yNum += 30, '24', '0', 0, '1、红烧黛玉') strCmd += blesdk.addCPCLSETMAG(2, 2) strCmd += blesdk.addCPCLText('0', that.yNum += 50, '24', '0', 0, 'X1 16元') strCmd += blesdk.addCPCLSETMAG(1, 1) strCmd += blesdk.addCPCLText('0', that.yNum += 50, '24', '0', 0, '*******************************************') strCmd += blesdk.addCPCLSETMAG(1, 1) strCmd += blesdk.addCPCLText('0', that.yNum += 30, '24', '0', 0, '配送费: 1元') strCmd += blesdk.addCPCLSETMAG(1, 1) strCmd += blesdk.addCPCLText('0', that.yNum += 30, '24', '0', 0, '合计金额: 17元') strCmd += blesdk.addCPCLSETMAG(1, 1) strCmd += blesdk.addCPCLText('0', that.yNum += 30, '24', '0', 0, '*******************************************') strCmd += blesdk.addCPCLSETMAG(1, 1) strCmd += blesdk.addCPCLText('0', that.yNum += 30, '24', '0', 0, '商品取到后,如有任何商品问题,请') strCmd += blesdk.addCPCLSETMAG(1, 1) strCmd += blesdk.addCPCLText('0', that.yNum += 30, '24', '0', 0, '联系商家或平台,客服将为您服务客') strCmd += blesdk.addCPCLSETMAG(1, 1) strCmd += blesdk.addCPCLText('0', that.yNum += 30, '24', '0', 0, '服电话13521030702,谢谢您的惠顾') strCmd += blesdk.addCPCLPrint(); bufferData.push(strCmd); this.handlePrintTap() }, //点击打印按钮 handlePrintTap() { let that = this uni.showLoading({ mask: true, title: '打印中...' }); uni.getSystemInfo({ success(res) { console.log('设备类型',res.osName) deviceType = res.osName },fail(err){ console.log('getSystemInfo失败') } }) // 初始化蓝牙模块 uni.openBluetoothAdapter({ //111 mode: 'central', success: (res2) => { console.log('openBluetoothAdapter成功') that.jiantingshebei() }, fail: (res) => { console.log('openBluetoothAdapter失败') if (res.errCode == '10001') { uni.showToast({ duration: 2000, title: "请打开蓝牙后,再进行连接", icon: 'none', mask: true }) } else { that.tui.toast('连接失败,请重启蓝牙或删除小程序重新进入-1') that.isShowSearch = true } setTimeout(res => { uni.hideLoading(); }, 2000) } }) }, /** * jiantingshebei方法,判断缓存中有没有deviceId等数据的时候, * 有: 直接拿到缓存中的deviceId等,直接请求 uni.createBLEConnection * 没有:才会调用 uni.onBluetoothDeviceFound 搜索 */ jiantingshebei() { let that = this; console.log('jiantingshebei设备',uni.getStorageSync('deviceId')) if (uni.getStorageSync('deviceId')) { // that.huoqufuwu(uni.getStorageSync('deviceId')) that.lianejieshebei(uni.getStorageSync('deviceId')) } else { uni.showToast({ duration: 2000, title: "蓝牙连接失败,请去商家主页尝试重新连接", icon: 'none', mask: true }) // uni.hideLoading(); // that.isShowSearch = true // // 监听扫描到新设备事件 // uni.onBluetoothDeviceFound((res4) => { //333 // res4.devices.forEach((device) => { // if (!device.name && !device.localName) { // return // } // that.devicesList.push(device) // }) // }) } }, lianejieshebei(deviceId) { let that = this; uni.showLoading({ mask: true, title: '加载中...' }); // let timerId = setTimeout(() => { // uni.closeBLEConnection({ // deviceId: deviceId, // success(res2) { // },fail(err1){ // } // }) // uni.closeBluetoothAdapter({ // success(res) { // },fail(err2){ // } // }) // uni.removeStorageSync('deviceId') // that.handlePrintTap() // that.isShowSearch = true // return // }, 5000); // 连接设备 uni.createBLEConnection({ //555 deviceId: deviceId, // 搜索到设备的 deviceId success: () => { console.log('createBLEConnection成功') // 连接成功就清除定时器 // clearTimeout(timerId); // that.isShowSearch = false that.huoqufuwu(deviceId) }, fail: (res) => { uni.showToast({ duration: 2000, title: "蓝牙连接失败,请查看打印机状态或重新连接", icon: 'none', mask: true }) // console.log('deviceId对吗?',deviceId) // console.log('createBLEConnection失败',res) // clearTimeout(timerId); // // if(res.errno == '1509007'){ // that.huoqufuwu(deviceId) // // } // if(res.errno == '1509003'){ // that.lianejieshebei(deviceId) // } } }) }, huoqufuwu(deviceId) { let that = this; //获取服务 uni.getBLEDeviceServices({ //666 deviceId: deviceId, // 搜索到设备的 deviceId success: (res5) => { console.log('getBLEDeviceServices成功') that.duxietezhengzhi(deviceId, res5) }, fail: (res) => { uni.showToast({ duration: 2000, title: "蓝牙连接失败,请查看打印机状态或重新连接", icon: 'none', mask: true }) // console.log('getBLEDeviceServices失败') // that.tui.toast('连接失败,请重启蓝牙或删除小程序重新进入-4',res) // uni.closeBLEConnection({ // deviceId: uni.getStorageSync('deviceId'), // success(res1) { // } // }) // uni.removeStorageSync('deviceId') // that.sousuo() // that.isShowSearch = true // setTimeout(res => { // uni.hideLoading(); // }, 2000) } }) }, duxietezhengzhi(deviceId, res5) { let that = this; //读写特征值 uni.getBLEDeviceCharacteristics({ //777 deviceId: deviceId, // 搜索到设备的 deviceId serviceId: res5.services[2].uuid, // 上一步中找到的某个服务 success: (res6) => { console.log('getBLEDeviceCharacteristics成功',res6) for (let i = 0; i < res6.characteristics.length; i++) { let item = res6.characteristics[i] if (item.properties.write) { // 该特征值可写 that.startNoticeBle(deviceId, res5.services[2].uuid, res6.characteristics[2].uuid) uni.setStorageSync('deviceId', deviceId); uni.setStorageSync('serviceId', res5.services[2].uuid); uni.setStorageSync('characteristicId', item.uuid); that.xierushuju() return; } } }, fail: (res) => { uni.showToast({ duration: 2000, title: "蓝牙连接失败,请查看打印机状态或重新连接", icon: 'none', mask: true }) // console.log('getBLEDeviceCharacteristics失败') // that.tui.toast('连接失败,请重启蓝牙或删除小程序重新进入-5',res) // uni.closeBLEConnection({ // deviceId: uni.getStorageSync('deviceId'), // success(res) { // } // }) // uni.removeStorageSync('deviceId') // that.sousuo() // that.isShowSearch = true // setTimeout(res => { // uni.hideLoading(); // }, 2000) } }) }, // 开启蓝牙数据监听 startNoticeBle(deviceId, serviceId, characteristicId) { let that = this uni.notifyBLECharacteristicValueChange({ state: true, // 启用 notify 功能 deviceId: deviceId, serviceId: serviceId, characteristicId: characteristicId, success(res) { console.log("监听成功",res) that.GetDataFromBle(); }, fail: function(err) { console.log("监听失败了") } }) }, // 设备返回的数据接收 GetDataFromBle() { var that = this; console.log('哈希之前2222222222222222'); uni.onBLECharacteristicValueChange((res) => { // 此时可以拿到蓝牙设备返回来的数据是一个ArrayBuffer类型数据,所以需要通过一个方法转换成字符串 console.log('哈希之前111111111111',res); var bleData = that.ab2hex(res.value) console.log('哈希了吗?',bleData) uni.hideLoading() setTimeout(res=>{ uni.closeBLEConnection({ deviceId: uni.getStorageSync('deviceId'), success(res) {}, fail(err) {} }) // that.getIsData("0") bufferData = [] },1000) }) }, ab2hex(buffer) { return Array.from(new Uint8Array(buffer)) .map(byte => ('00' + byte.toString(16)).slice(-2)) .join(''); }, xierushuju() { let that = this; //写入数据 console.log("22222222222222",bufferData) var dataStr = '' for (let m = 0; m < bufferData.length; m++) { dataStr += bufferData[m] } const maxChunk = deviceType == 'android' ? 20 : 180; const delay = deviceType == 'android' ? 20 : 180; let buffer = gbk.strToGBKByte(dataStr) 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(that._writeBLECharacteristicValue, j * delay, subPackage); } return; }, _writeBLECharacteristicValue(buffer) { let that = this; uni.writeBLECharacteristicValue({ deviceId: uni.getStorageSync('deviceId'), serviceId: uni.getStorageSync('serviceId'), characteristicId: uni.getStorageSync('characteristicId'), value: buffer, success(res) { console.log("66666666666") }, fail(res) { console.log("777777777",res) uni.showToast({ duration: 2000, title: "打印失败,请查看打印机状态或重新连接", icon: 'none', mask: true }) uni.closeBLEConnection({ deviceId: uni.getStorageSync('deviceId'), success(res) { } }) // if (uni.getStorageSync('deviceId')) { // // that.xierushuju() // } else { // that.jiantingshebei() // } // uni.hideLoading(); } }) }, handleSearchClose() { uni.closeBluetoothAdapter({ success(res) { } }) this.isShowSearch = false }, getIsData(type){ if(type == 0){ // 删除数组中第一个元素 getApp().globalData.printData.shift(); } if(getApp().globalData.printData.length>0){ this.onPrint() } } }