Commit 8948269a by liangjianmin

js

parent 55a52af1
Showing with 75 additions and 38 deletions
...@@ -43,8 +43,8 @@ ...@@ -43,8 +43,8 @@
export default { export default {
data() { data() {
return { return {
num: 1, print_number: 0,
uuid: '', serviceId: '',
deviceId: '', deviceId: '',
characteristics: '', characteristics: '',
text: '总箱数', text: '总箱数',
...@@ -56,6 +56,7 @@ ...@@ -56,6 +56,7 @@
traySelectOption: [], traySelectOption: [],
sendData64: '', sendData64: '',
platform: 'android', platform: 'android',
printLabelData: [],
form: { form: {
erp_order_sn_pre: '', erp_order_sn_pre: '',
print_type: 1, print_type: 1,
...@@ -71,7 +72,7 @@ ...@@ -71,7 +72,7 @@
}, },
methods: { methods: {
getTraySelectOption() { 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) { if (res.err_code === 0) {
this.traySelectOption = res.data; this.traySelectOption = res.data;
} }
...@@ -83,8 +84,6 @@ ...@@ -83,8 +84,6 @@
this.form.tray_remark = this.traySelectOption[e.target.value].wstylt_id; this.form.tray_remark = this.traySelectOption[e.target.value].wstylt_id;
}, },
submit() { submit() {
this.openBluetoothAdapter();
return false;
if (!this.form.erp_order_sn_pre) { if (!this.form.erp_order_sn_pre) {
uni.showToast({ uni.showToast({
icon: 'error', icon: 'error',
...@@ -99,7 +98,6 @@ ...@@ -99,7 +98,6 @@
}); });
return false; return false;
} }
if (!/(^[0-9]*$)/.test(this.form.erp_order_sn_number)) { if (!/(^[0-9]*$)/.test(this.form.erp_order_sn_number)) {
uni.showToast({ uni.showToast({
icon: 'error', icon: 'error',
...@@ -131,9 +129,9 @@ ...@@ -131,9 +129,9 @@
}); });
return false; return false;
} }
this.request(API.printLabel, 'POST', this.form, false).then(res => {
this.request(API.printLabel, 'POST', this.form, true).then(res => {
if (res.err_code === 0) { if (res.err_code === 0) {
this.printLabelData = res.data;
this.openBluetoothAdapter(); this.openBluetoothAdapter();
} else { } else {
uni.showToast({ uni.showToast({
...@@ -144,6 +142,7 @@ ...@@ -144,6 +142,7 @@
}); });
}, },
openBluetoothAdapter() { openBluetoothAdapter() {
//先查看系统蓝牙状态
let that = this; let that = this;
uni.openBluetoothAdapter({ uni.openBluetoothAdapter({
success(res) { success(res) {
...@@ -158,24 +157,36 @@ ...@@ -158,24 +157,36 @@
}); });
}, },
startBluetoothDevicesDiscovery() { startBluetoothDevicesDiscovery() {
//搜索附近的蓝牙设备并且监听寻找到新设备的事件
let that = this; let that = this;
uni.showLoading({
title: '连接设备中',
mask: true
});
uni.startBluetoothDevicesDiscovery({ uni.startBluetoothDevicesDiscovery({
success: function(res) { success: function(res) {
uni.showLoading({ console.log('搜索设备start', res);
title: '连接蓝牙中...'
});
uni.onBluetoothDeviceFound(function(el) { uni.onBluetoothDeviceFound(function(el) {
//找到对应蓝牙设备名字 //找到对应蓝牙设备名字
if (el.devices[0].name == 'HM-A300-87d9') { if (el.devices[0].name == 'HM-A300-87d9') {
that.deviceId = el.devices[0].deviceId; //成功后存储设备id that.deviceId = el.devices[0].deviceId; //成功后存储设备id
that.stop(); //关闭搜索 that.stopBluetoothDevicesDiscovery(); //关闭搜索
console.log("成功搜索设备", el);
console.log(el.devices[0].deviceId); console.log(el.devices[0].deviceId);
} }
}); });
},
fail: function() {
uni.hideLoading();
uni.showToast({
title: '请断开蓝牙,重新连接',
icon: 'error'
});
} }
}); });
}, },
stop() { stopBluetoothDevicesDiscovery() {
//成功找到对应蓝牙设备后 停止搜寻附近的蓝牙外围设备
let that = this; let that = this;
uni.stopBluetoothDevicesDiscovery({ uni.stopBluetoothDevicesDiscovery({
success(res) { success(res) {
...@@ -185,6 +196,7 @@ ...@@ -185,6 +196,7 @@
}); });
}, },
getBLEDeviceServices() { getBLEDeviceServices() {
//来获取蓝牙设备所有服务
let that = this; let that = this;
uni.createBLEConnection({ uni.createBLEConnection({
deviceId: this.deviceId, deviceId: this.deviceId,
...@@ -196,64 +208,73 @@ ...@@ -196,64 +208,73 @@
deviceId: that.deviceId, deviceId: that.deviceId,
success: res => { success: res => {
console.log('获取蓝牙设备所有服务', res); console.log('获取蓝牙设备所有服务', res);
that.uuid = res.services[0].uuid; that.serviceId = res.services[0].uuid;
that.getServices(); that.getBLEDeviceCharacteristics();
} }
}); });
}, 1000); }, 0);
} }
}); });
}, },
getServices() { getBLEDeviceCharacteristics() {
//获取蓝牙设备某个服务中所有特征值
let that = this; let that = this;
uni.getBLEDeviceCharacteristics({ uni.getBLEDeviceCharacteristics({
deviceId: that.deviceId, deviceId: that.deviceId,
serviceId: that.uuid, serviceId: that.serviceId,
success: res => { success: res => {
uni.hideLoading();
that.characteristics = res.characteristics[0].uuid; that.characteristics = res.characteristics[0].uuid;
console.log('服务功能', res); that.sendDataChange();
//包装数据 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();
}, },
fail: res => { fail: res => {
console.log('失败', 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() { writeBLECharacteristicValue() {
//写入二进制数据
let that = this; let that = this;
uni.writeBLECharacteristicValue({ uni.writeBLECharacteristicValue({
deviceId: that.deviceId, deviceId: that.deviceId,
serviceId: that.uuid, serviceId: that.serviceId,
characteristicId: that.characteristics, characteristicId: that.characteristics,
value: that.sendData64, value: that.sendData64,
success(res) { success(res) {
console.log('writeBLECharacteristicValue success', res.errMsg) console.log('writeBLECharacteristicValue success', res.errMsg)
that.num++; that.print_number++;
if (that.num < 3) { if (that.print_number < that.printLabelData.length) {
uni.closeBLEConnection({ uni.closeBLEConnection({
deviceId: that.deviceId, deviceId: that.deviceId,
success(res) { success(res) {
console.log(res) console.log(res)
setTimeout(function() { setTimeout(function() {
that.openBluetoothAdapter(); that.openBluetoothAdapter();
}, 1000); }, 0);
} }
}) })
} else { } else {
uni.hideLoading();
uni.showModal({ uni.showModal({
title: '提示', title: '提示',
content: '提交成功,打印中,请查看标签打印机', content: '提交成功,打印中,请查看标签打印机',
...@@ -274,6 +295,22 @@ ...@@ -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