| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584 |
- /** -姜枫 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.devices.length;i++){
- console.log(that.devices[i].name,'----',name)
- if (that.devices[i].name==name) {return}
- }
- that.devices.push({name: name,deviceId: deviceId,services: []});
- break;
- }
-
- }else{
- console.log('name',name)
- console.log('re.devices[0]',re.devices[0])
- }
- });
- }
-
- }
- });
- },
- // onDevice() {
- // var that = this;
- // //监听寻找到新设备的事件
- // uni.onBluetoothDeviceFound(function(devices) {
- // console.log(devices,'设备列表');
- // var re = JSON.parse(JSON.stringify(devices));
- // let name = re.devices[0].name?re.devices[0].name:'未知设备';
- // if (name != "未知设备" && name != "") {
- // let deviceId = re.devices[0].deviceId;
- // switch(uni.getSystemInfoSync().platform){
- // case 'android':
- // that.devices.push({name: name,deviceId: deviceId,services: []});
- // break;
- // case 'ios':
- // for (let i=0;i<that.devices.length;i++){
- // console.log(that.devices[i].name,'----',name)
- // if (that.devices[i].name==name) {return}
- // }
- // that.devices.push({name: name,deviceId: deviceId,services: []});
- // break;
- // }
- // // that.$nextTick(()=>{
- // // 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))
- }
- })
- },
- }
- }
|