Commit 8948269a by liangjianmin

js

parent 55a52af1
Showing with 75 additions and 38 deletions
......@@ -43,8 +43,8 @@
export default {
data() {
return {
num: 1,
uuid: '',
print_number: 0,
serviceId: '',
deviceId: '',
characteristics: '',
text: '总箱数',
......@@ -56,6 +56,7 @@
traySelectOption: [],
sendData64: '',
platform: 'android',
printLabelData: [],
form: {
erp_order_sn_pre: '',
print_type: 1,
......@@ -71,7 +72,7 @@
},
methods: {
getTraySelectOption() {
this.request(API.getTraySelectOption, 'POST', { erp_order_sn: this.form.erp_order_sn_pre + this.form.erp_order_sn_number }, false).then(res => {
this.request(API.getTraySelectOption, 'POST', {}, false).then(res => {
if (res.err_code === 0) {
this.traySelectOption = res.data;
}
......@@ -83,8 +84,6 @@
this.form.tray_remark = this.traySelectOption[e.target.value].wstylt_id;
},
submit() {
this.openBluetoothAdapter();
return false;
if (!this.form.erp_order_sn_pre) {
uni.showToast({
icon: 'error',
......@@ -99,7 +98,6 @@
});
return false;
}
if (!/(^[0-9]*$)/.test(this.form.erp_order_sn_number)) {
uni.showToast({
icon: 'error',
......@@ -131,9 +129,9 @@
});
return false;
}
this.request(API.printLabel, 'POST', this.form, true).then(res => {
this.request(API.printLabel, 'POST', this.form, false).then(res => {
if (res.err_code === 0) {
this.printLabelData = res.data;
this.openBluetoothAdapter();
} else {
uni.showToast({
......@@ -144,6 +142,7 @@
});
},
openBluetoothAdapter() {
//先查看系统蓝牙状态
let that = this;
uni.openBluetoothAdapter({
success(res) {
......@@ -158,24 +157,36 @@
});
},
startBluetoothDevicesDiscovery() {
//搜索附近的蓝牙设备并且监听寻找到新设备的事件
let that = this;
uni.startBluetoothDevicesDiscovery({
success: function(res) {
uni.showLoading({
title: '连接蓝牙中...'
title: '连接设备中',
mask: true
});
uni.startBluetoothDevicesDiscovery({
success: function(res) {
console.log('搜索设备start', res);
uni.onBluetoothDeviceFound(function(el) {
//找到对应蓝牙设备名字
if (el.devices[0].name == 'HM-A300-87d9') {
that.deviceId = el.devices[0].deviceId; //成功后存储设备id
that.stop(); //关闭搜索
that.stopBluetoothDevicesDiscovery(); //关闭搜索
console.log("成功搜索设备", el);
console.log(el.devices[0].deviceId);
}
});
},
fail: function() {
uni.hideLoading();
uni.showToast({
title: '请断开蓝牙,重新连接',
icon: 'error'
});
}
});
},
stop() {
stopBluetoothDevicesDiscovery() {
//成功找到对应蓝牙设备后 停止搜寻附近的蓝牙外围设备
let that = this;
uni.stopBluetoothDevicesDiscovery({
success(res) {
......@@ -185,6 +196,7 @@
});
},
getBLEDeviceServices() {
//来获取蓝牙设备所有服务
let that = this;
uni.createBLEConnection({
deviceId: this.deviceId,
......@@ -196,64 +208,73 @@
deviceId: that.deviceId,
success: res => {
console.log('获取蓝牙设备所有服务', res);
that.uuid = res.services[0].uuid;
that.getServices();
that.serviceId = res.services[0].uuid;
that.getBLEDeviceCharacteristics();
}
});
}, 1000);
}, 0);
}
});
},
getServices() {
getBLEDeviceCharacteristics() {
//获取蓝牙设备某个服务中所有特征值
let that = this;
uni.getBLEDeviceCharacteristics({
deviceId: that.deviceId,
serviceId: that.uuid,
serviceId: that.serviceId,
success: res => {
uni.hideLoading();
that.characteristics = res.characteristics[0].uuid;
console.log('服务功能', res);
//包装数据
var data = "! 0 200 200 450 1\r\n" +
"T 8 0 10 10 入仓号:" + new Date().getTime() + "\r\n" +
"T 8 0 10 56 箱号:01/20\r\n" +
"T 8 0 10 96 时间:202201101627\r\n" +
"T 8 0 10 142 仓库代码:HK01\r\n" +
"B QR 290 10 M 2 U 10\r\n" +
"MA,SB\r\n" +
"ENDQR\r\n" +
"PRINT\r\n";
var arrayBuffer = uni.base64ToArrayBuffer(ToBase64.encode64gb2312(data))
that.sendData64 = arrayBuffer;
that.writeBLECharacteristicValue();
that.sendDataChange();
console.log('服务功能特征值', res);
},
fail: res => {
console.log('失败', res);
}
});
},
sendDataChange() {
console.log('第' + this.print_number + '次打印');
//发送数据 二进制
let data = "";
if (this.printLabelData.length > 0) {
data = "! 0 200 200 450 1\r\n" +
"T 8 0 10 10 入仓号:" + this.printLabelData[this.print_number].erp_order_sn + "\r\n" +
"T 8 0 10 56 箱号:" + this.printLabelData[this.print_number].label_sort + '/' + this.printLabelData[0].label_num + "\r\n" +
"T 8 0 10 96 时间:" + this.printLabelData[this.print_number].create_time_cn + "\r\n" +
"T 8 0 10 142 仓库代码:" + this.printLabelData[this.print_number].warehouse_code + "\r\n" +
"B QR 290 10 M 2 U 10\r\n" +
"MA," + this.printLabelData[this.print_number].erp_order_sn + "\r\n" +
"ENDQR\r\n" +
"PRINT\r\n";
}
console.log(data)
let arrayBuffer = uni.base64ToArrayBuffer(ToBase64.encode64gb2312(data));
this.sendData64 = arrayBuffer;
this.writeBLECharacteristicValue();
},
writeBLECharacteristicValue() {
//写入二进制数据
let that = this;
uni.writeBLECharacteristicValue({
deviceId: that.deviceId,
serviceId: that.uuid,
serviceId: that.serviceId,
characteristicId: that.characteristics,
value: that.sendData64,
success(res) {
console.log('writeBLECharacteristicValue success', res.errMsg)
that.num++;
if (that.num < 3) {
that.print_number++;
if (that.print_number < that.printLabelData.length) {
uni.closeBLEConnection({
deviceId: that.deviceId,
success(res) {
console.log(res)
setTimeout(function() {
that.openBluetoothAdapter();
}, 1000);
}, 0);
}
})
} else {
uni.hideLoading();
uni.showModal({
title: '提示',
content: '提交成功,打印中,请查看标签打印机',
......@@ -274,6 +295,22 @@
}
});
}
},
fail() {
uni.hideLoading();
uni.showModal({
title: '提示',
content: '无法打印,请查看标签打印机连接状态',
showCancel: false,
confirmText: '关闭',
success: function(res) {
if (res.confirm) {
} else if (res.cancel) {
console.log('用户点击取消');
}
}
});
}
})
},
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment