/** -姜枫 2021.06.13- **/ import { addPrintNum } from "@/api/order"; var util = require("@/pagesOrder/js/util.js"); import drawQrcode from "@/pagesOrder/js/weapp.qrcode.esm"; var toArrayBuffer = require('to-array-buffer'); var PrinterJobs = require("@/pagesOrder/js/printerjobs.js"); var printerUtil = require("@/pagesOrder/js/printerutil.js"); export default { data() { return { BleVal:'', nameBLE:'未连接', devices: [], currDev: null, isAutoBle: false, connId: "", piaojuText: [''], }; }, onShow() { this.devices = []; this.isAutoBle = false; if(uni.getStorageSync('BLE')){ this.nameBLE = uni.getStorageSync('BLE').name; } }, methods: { //断开蓝牙链接 destroyed(BleVal) { this.BleVal = BleVal; let that = this; if(!uni.getStorageSync('BLE')){ that.$util.confirmModal({content:'请先连接蓝牙'},() => { uni.navigateTo({url:'/pagesOrder/print'}); }) return; } uni.closeBLEConnection({deviceId: uni.getStorageSync('BLE').deviceId,complete(res) { console.log(res); that.autoLinkBle(BleVal); } }); // if (this.connId != "") { // uni.closeBLEConnection({deviceId: uni.getStorageSync('BLE').deviceId,success(res) { // this.autoLinkBle(); // } // }); // } }, //初始化蓝牙 searchBle() { uni.showToast({ title: '搜索中...', duration: 2000 }); var that = this; that.isAutoBle = false; uni.openBluetoothAdapter({ success(res) { console.log(res,"打开 蓝牙模块"); //that.onDevice();//监听搜索新设备 //获取本机蓝牙适配器状态。 uni.getBluetoothAdapterState({ success: function(res) { console.log(res); if (res.available) { if (res.discovering) { that.stopFindBule(); } //搜索蓝牙 //开始搜寻附近的蓝牙外围设备 console.log("开始搜寻附近的蓝牙外围设备"); uni.startBluetoothDevicesDiscovery({ success(res) { console.log(res); that.getBluetoothDevices() } }); } else { console.log("本机蓝牙不可用"); } } }); } }); }, getBluetoothDevices(){ let that = this; uni.getBluetoothDevices({ success: re => { //let devices = res.devices; console.log(re,'设备列表'); //var re = JSON.parse(JSON.stringify(devices)); if(re.devices && re.devices.length > 0){ re.devices.forEach(function(item){    let name = item.name?item.name:'未知设备'; if (name != "未知设备" && name != "") { let deviceId = item.deviceId; switch(uni.getSystemInfoSync().platform){ case 'android': that.devices.push({name: name,deviceId: deviceId,services: []}); break; case 'ios': for (let i=0;i{ // // that.$util.unique(that.devices); // // }) // } // }); // }, //自动链接蓝牙-初始化蓝牙模块 autoLinkBle(BleVal){ this.BleVal = BleVal; let that = this; if(!uni.getStorageSync('BLE')){ that.$util.confirmModal({content:'请先连接蓝牙'},() => { uni.navigateTo({url:'/pagesOrder/print'}) }) return; } uni.showLoading({title: '打印中...'}); that.isAutoBle = true; let deviceId = uni.getStorageSync('BLE'); //初始化蓝牙模块 uni.openBluetoothAdapter({ success(res) { console.log('初始化成功',res) that.autoLinkBleFun(deviceId); },fail(e){ console.log('初始化失败',e) } }); }, //自动链接蓝牙 autoLinkBleFun(item) { console.log('自动链接 444444444444444444444444444444444444444444444444444') var that = this; let deviceId = item.deviceId; uni.createBLEConnection({ deviceId: deviceId, complete(res) { console.log(res,'自动连接成功') if (res.errMsg == "createBLEConnection:ok") { uni.setStorageSync('BLE',item); setTimeout(function() { that.getBLEServices(deviceId); }, 2000); } } }); }, stopFindBule() { console.log("停止搜寻附近的蓝牙外围设备---------------"); uni.stopBluetoothDevicesDiscovery({ success(res) { console.log(res); } }); }, onConn(item) { console.log('正在连接 333333333333333333333333333333333333333333333333333333333333333333333333') uni.showLoading({title: '正在连接...'}); var that = this; // console.log("连接蓝牙---------------" + item.deviceId); let deviceId = item.deviceId; uni.createBLEConnection({ deviceId: deviceId, complete(res) { if (res.errMsg == "createBLEConnection:ok") { that.nameBLE = item.name; // console.log("连接蓝牙-[" + item.name + "]--成功"); // that.$msg("连接蓝牙-[" + item.name + "]--成功"); that.connId = deviceId; that.currDev = item; setTimeout(function() { that.getBLEServices(deviceId); }, 2000); } else { console.log(res); } //连接成功 关闭搜索 that.stopFindBule(); } }); }, getBLEServices(_deviceId) { var that = this; let deviceId = _deviceId; console.log("获取蓝牙设备所有服务(service)"); console.log('获取蓝牙设备所有服务 222222222222222222222222222222222222222222222222222222222') uni.getBLEDeviceServices({ // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接 deviceId: deviceId, success(res) { console.log('getBLEDeviceServices成功:',res) for (var s = 0; s < res.services.length; s++) { console.log(res.services[s].uuid); let serviceId = res.services[s].uuid; uni.getBLEDeviceCharacteristics({ // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接 deviceId: deviceId, // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取 serviceId: serviceId, success(res) { var re = JSON.parse(JSON.stringify(res)); console.log("deviceId = [" + deviceId + "] serviceId = [" + serviceId + "]") for (var c = 0; c < re.characteristics.length; c++) { if (re.characteristics[c].properties.write == true) { let uuid = re.characteristics[c].uuid; console.log(" deviceId = [" +deviceId +"] serviceId = [" +serviceId +"] characteristics = [" +uuid +"]"); //是否是直接打印 if(that.isAutoBle){ console.log('打印订单 11111111111111111111111111111111111111111111111111111111111111') that.senBleLabel() uni.hideLoading() that.isAutoBle = false } for (var index in that.devices) { if (that.devices[index].deviceId == deviceId) { that.devices[index].services.push({serviceId: serviceId,characteristicId: uuid}); that.$nextTick(()=>{ uni.setStorageSync('BLE',that.devices[index]); that.$msg("连接蓝牙-[" + that.devices[index].name + "]--成功"); }) break; } } console.log(JSON.stringify(that.devices)); } } } }); } }, fail(res) { console.log('getBLEDeviceServices失败:',res) uni.hideLoading() uni.showLoading({title:"打印失败..."}) setTimeout(function(){ uni.hideLoading() },2000) // if(res.errCode && res.errCode == 10004){ // uni.showLoading({title:"重打中..."}) // that.destroyed(that.BleVal) // } } }); }, _writeBLECharacteristicValue(data) { uni.writeBLECharacteristicValue({ deviceId: data.deviceId, serviceId: data.serviceId, characteristicId: data.characteristicId, value: data.buffer, success(res) { console.log('writeBLECharacteristicValue success', res) }, fail(res) { console.log('writeBLECharacteristicValue fail', res) } }) }, senBleLabel() { let BleVal = this.BleVal; let that = this; console.log(BleVal,'BleValBleVal') let currDev = uni.getStorageSync('BLE'); let deviceId = currDev.deviceId; let serviceId = currDev.services[0].serviceId; let characteristicId = currDev.services[0].characteristicId; let currentRealPrice = BleVal.debt == 1 ? '0.00' : BleVal.realPrice; let printerJobs = new PrinterJobs(); printerJobs .setSize(2, 2) .print(BleVal.sendNum) .setSize(1, 1) .print(printerUtil.fillLine()) .setSize(2, 2) .print(BleVal.custom.fullAddress) .print(BleVal.custom.customName) .print(BleVal.custom.customMobile) .setSize(1, 1) .print("备注:") .setSize(2, 2) .print(BleVal.remark) .setSize(1, 1) .setAlign('lt') .print(printerUtil.fillAround('花材 数量 价格')); if(BleVal.product.length > 0){ BleVal.product.forEach(function(currentItem){ printerJobs.print(printerUtil.inline(currentItem.name+' '+currentItem.num, currentItem.price)); }) } printerJobs.print(printerUtil.fillLine()); //printerJobs.setAlign('rt'); printerJobs.setAlign('lt') if(BleVal.sendCost>0){ printerJobs.print('运费:¥'+BleVal.sendCost); } printerJobs.print('总金额:¥'+BleVal.prePrice); if(BleVal.discountAmount > 0){ printerJobs.print('优惠:¥'+BleVal.discountAmount); } printerJobs.print('应付金额:¥'+BleVal.realPrice); printerJobs.print('实付金额:¥'+currentRealPrice); if(BleVal.debtAmount > 0){ printerJobs.print('累计欠款:¥'+BleVal.debtAmount); } printerJobs.print(printerUtil.fillLine()); printerJobs.print('订单日期:'+BleVal.date); printerJobs.print('订单编号:'+BleVal.orderSn); printerJobs.print('门店名称:纯彩花艺'); printerJobs.print('门店地址:'+BleVal.address); printerJobs.println().println(); let buffer = printerJobs.buffer(); // 1.并行调用多次会存在写失败的可能性 // 2.建议每次写入不超过20字节 // 分包处理,延时调用 const maxChunk = 20; const delay = 20; 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._writeBLECharacteristicValue, j * delay, {deviceId:deviceId, serviceId:serviceId, characteristicId:characteristicId,buffer:subPackage}); } //增加打印次数 addPrintNum({id:that.orderId}).then(res=>{ if(!that.$util.isEmpty(that.detailInfo)){ that.detailInfo.printNum++; } if(!that.$util.isEmpty(that.list.data)){ that.list.data.forEach(ele=>{ if (ele.id==that.orderId) { ele.printNum++; } }) } }) // let arrPrint = []; // //初始化打印机 // arrPrint.push(util.sendDirective([0x1B, 0x40])); //16进制 // //正文 // arrPrint.push(util.sendDirective([0x1B, 0x0E])); // //字体宽高加倍 // arrPrint.push(util.sendDirective([0x1d, 0x21, 0x11])); // arrPrint.push(util.hexStringToBuff(BleVal.sendNum)); // //字体不放大,宽高不再加倍 // arrPrint.push(util.sendDirective([0x1d, 0x21, 0x00])); // arrPrint.push(util.sendDirective([0x1B, 0x14])); // arrPrint.push(util.hexStringToBuff(" |纯彩花艺\n")); // arrPrint.push(util.sendDirective([0x1B, 0x14])); // arrPrint.push(util.hexStringToBuff("--------------------------------\n")); // arrPrint.push(util.sendDirective([0x1B, 0x0E])); // //字体宽高加倍 // arrPrint.push(util.sendDirective([0x1d, 0x21, 0x11])); // arrPrint.push(util.hexStringToBuff(BleVal.custom.fullAddress+ "\n")); // arrPrint.push(util.sendDirective([0x1B, 0x0E])); // arrPrint.push(util.hexStringToBuff(BleVal.custom.customName+"\n"+BleVal.custom.customMobile+ "\n")); // //字体不放大,宽高不再加倍 // arrPrint.push(util.sendDirective([0x1d, 0x21, 0x00])); // if(BleVal.remark.length > 0){ // arrPrint.push(util.sendDirective([0x1B, 0x14])); // arrPrint.push(util.hexStringToBuff("--------------------------------\n")); // arrPrint.push(util.hexStringToBuff("备注:\n")); // arrPrint.push(util.sendDirective([0x1B, 0x0E])); // //字体宽高加倍 // arrPrint.push(util.sendDirective([0x1d, 0x21, 0x11])); // arrPrint.push(util.hexStringToBuff(BleVal.remark+"\n")); // //字体不放大,宽高不再加倍 // arrPrint.push(util.sendDirective([0x1d, 0x21, 0x00])); // } // arrPrint.push(util.sendDirective([0x1B, 0x14])); // arrPrint.push(util.hexStringToBuff("--------------------------------\n")); // arrPrint.push(util.hexStringToBuff(util.printThreeData('花材', '数量', '价格'))); // if(BleVal.product.length > 0){ // BleVal.product.forEach(function(currentItem){ // arrPrint.push(util.hexStringToBuff(util.printThreeData(currentItem.name, currentItem.num, currentItem.price))) // }) // } // arrPrint.push(util.hexStringToBuff("--------------------------------\n")); // arrPrint.push(util.hexStringToBuff(util.printTwoData("运费:", BleVal.sendCost))); // arrPrint.push(util.hexStringToBuff(util.printTwoData("总金额:", BleVal.prePrice))); // arrPrint.push(util.hexStringToBuff(util.printTwoData("优惠:", BleVal.discountAmount))); // arrPrint.push(util.hexStringToBuff(util.printTwoData("应付金额:", BleVal.realPrice))); // arrPrint.push(util.hexStringToBuff(util.printTwoData("实付金额:", BleVal.debt == 1 ? '0.00' : BleVal.realPrice))); // if(BleVal.debtAmount > 0){ // //加粗 // //arrPrint.push(util.sendDirective([0x1b, 0x45, 0x01])); // arrPrint.push(util.hexStringToBuff(util.printTwoData("累计欠款:", BleVal.debtAmount))); // //取消加粗 // //arrPrint.push(util.sendDirective([0x1b, 0x45, 0x00])); // } // arrPrint.push(util.hexStringToBuff(util.printTwoData("订单日期:", BleVal.date))); // arrPrint.push(util.hexStringToBuff(util.printTwoData("订单编号:", BleVal.orderSn))); // arrPrint.push(util.sendDirective([0x1B, 0x61, 0x00])); // arrPrint.push(util.hexStringToBuff("门店地址:"+BleVal.address+"\n")); // arrPrint.push(util.hexStringToBuff("\n\n\n\n")); // const ctx = uni.createCanvasContext('canvas'); // ctx.clearRect(0, 0, 130, 130); // let tthis = this; // drawQrcode({ // canvasId: 'canvas', // text: 'https://shop.huahb.cn/#/main/index', // width: 130, // height: 130, // callback(e) { // console.log(111111123) // setTimeout(() => { // // 获取图片数据 // uni.canvasGetImageData({ // canvasId: 'canvas', // x: 0, // y: 0, // width: 130, // height: 130, // success(res) { // let arr = tthis.convert4to1(res.data); // let data = tthis.convert8to1(arr); // const cmds = [].concat([27, 97, 1], [29, 118, 48, 0, 20, 0, 160, 0], data, [27, 74, 3], [27, 64]); // const buffer = toArrayBuffer(Buffer.from(cmds, 'gb2312')); // console.log(123456789) // arrPrint.push(util.sendDirective([0x1B, 0x40])); // for (let i = 0; i < buffer.byteLength; i = i + 20) { // arrPrint.push(buffer.slice(i, i + 20)); // } // arrPrint.push(util.hexStringToBuff("\n")); // arrPrint.push(util.sendDirective([0x1B, 0x61, 0x01])); //居中 // arrPrint.push(util.hexStringToBuff("扫码采购花材\n")); // arrPrint.push(util.hexStringToBuff("\n")); // arrPrint.push(util.hexStringToBuff("\n")); // } // }) // }, 3000); // } // }); // this.printInfo({deviceId:deviceId, serviceId:serviceId, characteristicId:characteristicId}, arrPrint); }, //4合1 convert4to1(res) { let arr = []; for (let i = 0; i < res.length; i++) { if (i % 4 == 0) { let rule = 0.29900 * res[i] + 0.58700 * res[i + 1] + 0.11400 * res[i + 2]; if (rule > 200) { res[i] = 0; } else { res[i] = 1; } arr.push(res[i]); } } return arr; }, //8合1 convert8to1(arr) { let data = []; for (let k = 0; k < arr.length; k += 8) { let temp = arr[k] * 128 + arr[k + 1] * 64 + arr[k + 2] * 32 + arr[k + 3] * 16 + arr[k + 4] * 8 + arr[k + 5] * 4 + arr[k + 6] * 2 + arr[k + 7] * 1 data.push(temp); } return data; }, printInfo(device, arr, callback) { let tthis = this; if (arr.length > 0) { tthis.sendStr(device, arr[0], function(success) { arr.shift(); tthis.printInfo(device, arr, callback); }, function(error) { console.log(error); }); } else { callback ? callback() : ''; } }, //发送数据 sendStr(device, bufferstr, success, fail) { let tthis = this; uni.writeBLECharacteristicValue({ deviceId: device.deviceId, serviceId: device.serviceId, characteristicId: device.characteristicId, value: bufferstr, success: function(res) { success(res); console.log('sendStr', bufferstr) }, failed: function(res) { fail(res) console.log("数据发送失败:" + JSON.stringify(res)) } }) }, } }