|
|
@@ -1,6 +1,7 @@
|
|
|
/** -姜枫 2021.06.13- **/
|
|
|
var tsc = require("@/pagesOrder/js/gprint/tsc.js");
|
|
|
var esc = require("@/pagesOrder/js/gprint/esc.js");
|
|
|
+var util = require("@/pagesOrder/js/util.js");
|
|
|
export default {
|
|
|
data() {
|
|
|
return {
|
|
|
@@ -10,7 +11,7 @@ export default {
|
|
|
currDev: null,
|
|
|
isAutoBle: false,
|
|
|
connId: "",
|
|
|
- piaojuText: ['测试测试测试测试傻老大傻老大','测试测试测试测试傻老大傻老大','测试测试测试测试傻老大傻老大'],
|
|
|
+ piaojuText: [''],
|
|
|
};
|
|
|
},
|
|
|
onShow() {
|
|
|
@@ -43,7 +44,6 @@ export default {
|
|
|
});
|
|
|
// if (this.connId != "") {
|
|
|
// uni.closeBLEConnection({deviceId: uni.getStorageSync('BLE').deviceId,success(res) {
|
|
|
- // console.log(res);
|
|
|
// this.autoLinkBle();
|
|
|
// }
|
|
|
// });
|
|
|
@@ -142,6 +142,7 @@ export default {
|
|
|
},
|
|
|
//自动链接蓝牙
|
|
|
autoLinkBleFun(item) {
|
|
|
+ console.log('自动链接 444444444444444444444444444444444444444444444444444')
|
|
|
var that = this;
|
|
|
let deviceId = item.deviceId;
|
|
|
uni.createBLEConnection({
|
|
|
@@ -166,6 +167,7 @@ export default {
|
|
|
});
|
|
|
},
|
|
|
onConn(item) {
|
|
|
+ console.log('正在连接 333333333333333333333333333333333333333333333333333333333333333333333333')
|
|
|
uni.showLoading({title: '正在连接...'});
|
|
|
var that = this;
|
|
|
// console.log("连接蓝牙---------------" + item.deviceId);
|
|
|
@@ -196,13 +198,13 @@ export default {
|
|
|
let deviceId = _deviceId;
|
|
|
console.log("获取蓝牙设备所有服务(service)");
|
|
|
|
|
|
+ console.log('获取蓝牙设备所有服务 222222222222222222222222222222222222222222222222222222222')
|
|
|
+
|
|
|
uni.getBLEDeviceServices({
|
|
|
// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
|
|
|
deviceId: deviceId,
|
|
|
complete(res) {
|
|
|
- console.log(res);
|
|
|
- let serviceId = "";
|
|
|
-
|
|
|
+ console.log(res)
|
|
|
for (var s = 0; s < res.services.length; s++) {
|
|
|
console.log(res.services[s].uuid);
|
|
|
let serviceId = res.services[s].uuid;
|
|
|
@@ -214,13 +216,19 @@ export default {
|
|
|
success(res) {
|
|
|
var re = JSON.parse(JSON.stringify(res));
|
|
|
|
|
|
- console.log("deviceId = [" + deviceId + "] serviceId = [" + serviceId + "]");
|
|
|
+ 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);
|
|
|
+ console.log(" deviceId = [" +deviceId +"] serviceId = [" +serviceId +"] characteristics = [" +uuid +"]");
|
|
|
//是否是直接打印
|
|
|
- if(that.isAutoBle){that.senBleLabel();return}
|
|
|
+ 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});
|
|
|
@@ -231,7 +239,6 @@ export default {
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
console.log(JSON.stringify(that.devices));
|
|
|
}
|
|
|
}
|
|
|
@@ -244,46 +251,6 @@ export default {
|
|
|
}
|
|
|
});
|
|
|
},
|
|
|
- senBlData(deviceId, serviceId, characteristicId, uint8Array) {
|
|
|
- console.log("************deviceId = [" +deviceId +"] serviceId = [" +serviceId +"] characteristics=[" +characteristicId +"]");
|
|
|
- var uint8Buf = Array.from(uint8Array);
|
|
|
- function split_array(datas, size) {
|
|
|
- var result = {};
|
|
|
- var j = 0;
|
|
|
- for (var i = 0; i < datas.length; i += size) {
|
|
|
- result[j] = datas.slice(i, i + size);
|
|
|
- j++;
|
|
|
- }
|
|
|
- console.log(result);
|
|
|
- return result;
|
|
|
- }
|
|
|
- var sendloop = split_array(uint8Buf, 20);
|
|
|
- // console.log(sendloop.length)
|
|
|
- function realWriteData(sendloop, i) {
|
|
|
- var data = sendloop[i];
|
|
|
- if (typeof data == "undefined") {
|
|
|
- return;
|
|
|
- }
|
|
|
- console.log("第【" + i + "】次写数据" + data);
|
|
|
- var buffer = new ArrayBuffer(data.length);
|
|
|
- var dataView = new DataView(buffer);
|
|
|
- for (var j = 0; j < data.length; j++) {
|
|
|
- dataView.setUint8(j, data[j]);
|
|
|
- }
|
|
|
- uni.hideLoading();
|
|
|
- uni.writeBLECharacteristicValue({
|
|
|
- deviceId,
|
|
|
- serviceId,
|
|
|
- characteristicId,
|
|
|
- value: buffer,
|
|
|
- success(res) {
|
|
|
- realWriteData(sendloop, i + 1);
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
- var i = 0;
|
|
|
- realWriteData(sendloop, i);
|
|
|
- },
|
|
|
senBleLabel() {
|
|
|
let BleVal = this.BleVal;
|
|
|
console.log(BleVal,'BleValBleVal')
|
|
|
@@ -292,29 +259,67 @@ export default {
|
|
|
let deviceId = currDev.deviceId;
|
|
|
let serviceId = currDev.services[0].serviceId;
|
|
|
let characteristicId = currDev.services[0].characteristicId;
|
|
|
- var command = tsc.jpPrinter.createNew();
|
|
|
- command.setSize(40, 60);
|
|
|
- command.setGap(2);
|
|
|
- command.setCls();
|
|
|
- for (let i=0;i<BleVal.length;i++){
|
|
|
- command.setText(BleVal[i].name);
|
|
|
- command.setText(BleVal[i].phone);
|
|
|
- }
|
|
|
|
|
|
- command.setPagePrint();
|
|
|
- this.senBlData(deviceId, serviceId, characteristicId, command.getData());
|
|
|
+ let arrPrint = [];
|
|
|
+ //初始化打印机
|
|
|
+ arrPrint.push(util.sendDirective([0x1B, 0x40])); //16进制
|
|
|
+ //居中对齐
|
|
|
+ arrPrint.push(util.sendDirective([0x1B, 0x61, 0x01])); //居中
|
|
|
+ //正文
|
|
|
+ arrPrint.push(util.sendDirective([0x1B, 0x0E]));
|
|
|
+ arrPrint.push(util.hexStringToBuff("\n葵花生活\n\n"));
|
|
|
+ arrPrint.push(util.sendDirective([0x1B, 0x14]));
|
|
|
+ arrPrint.push(util.hexStringToBuff('葵花生活测试门店1号店' + "\n"));
|
|
|
+ arrPrint.push(util.hexStringToBuff("订单号" + 'W20154221212222' + "\n"));
|
|
|
+ arrPrint.push(util.hexStringToBuff("--------------------------------\n"));
|
|
|
+ arrPrint.push(util.hexStringToBuff(util.printThreeData('名称', '数量', '价格')));
|
|
|
+ arrPrint.push(util.hexStringToBuff(util.printThreeData('衣物', 10, '3元')))
|
|
|
+ arrPrint.push(util.hexStringToBuff("--------------------------------\n"));
|
|
|
+ arrPrint.push(util.hexStringToBuff(util.printTwoData("总金额:", '3元')));
|
|
|
+ arrPrint.push(util.hexStringToBuff(util.printTwoData("优惠券:", '3元')));
|
|
|
+ arrPrint.push(util.hexStringToBuff(util.printTwoData("实付金额:", '3元')));
|
|
|
+ arrPrint.push(util.hexStringToBuff(util.printTwoData("下单:", '2019-10-20 12:20:20')));
|
|
|
+ arrPrint.push(util.hexStringToBuff(util.printTwoData("支付:", '已支付')));
|
|
|
+ arrPrint.push(util.hexStringToBuff(util.printTwoData("客户:", '张三')));
|
|
|
+ arrPrint.push(util.hexStringToBuff(util.printTwoData("电话:", '13880253762')));
|
|
|
+ arrPrint.push(util.sendDirective([0x1B, 0x61, 0x00]));
|
|
|
+ arrPrint.push(util.hexStringToBuff("1.请妥善保管票单,凭票取物;\n"));
|
|
|
+ arrPrint.push(util.hexStringToBuff("2.顾客购意见及建议,请及时提出,本店积极采纳,全心全意为顾客服务\n"));
|
|
|
+ arrPrint.push(util.hexStringToBuff("\n\n\n"));
|
|
|
+
|
|
|
+ this.printInfo({deviceId:deviceId, serviceId:serviceId, characteristicId:characteristicId}, arrPrint);
|
|
|
+
|
|
|
+ },
|
|
|
+ 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))
|
|
|
+ }
|
|
|
+ })
|
|
|
},
|
|
|
- billPrint() {
|
|
|
- let currDev = uni.getStorageSync('BLE');
|
|
|
- //票据模式
|
|
|
- let deviceId = currDev.deviceId;
|
|
|
- let serviceId = currDev.services[0].serviceId;
|
|
|
- let characteristicId = currDev.services[0].characteristicId;
|
|
|
- var command = esc.jpPrinter.createNew();
|
|
|
- command.init();
|
|
|
- command.setText(this.piaojuText);
|
|
|
- command.setPrintAndFeedRow(1);
|
|
|
- this.senBlData(deviceId, serviceId, characteristicId, command.getData());
|
|
|
- }
|
|
|
}
|
|
|
}
|