Commit f2b558bd by liangjianmin

bug

parent 6513638c
...@@ -137,7 +137,7 @@ ...@@ -137,7 +137,7 @@
<textarea @input="inputChange()" maxlength="200" placeholder="请输入理货备注" placeholder-style="color:#6E767A;" v-model="form.sort_goods_remark"></textarea> <textarea @input="inputChange()" maxlength="200" placeholder="请输入理货备注" placeholder-style="color:#6E767A;" v-model="form.sort_goods_remark"></textarea>
<text class="text">{{ limitword }}/200</text> <text class="text">{{ limitword }}/200</text>
</view> </view>
<view class="box row verCenter" style="border-bottom: none;"> <!-- <view class="box row verCenter" style="border-bottom: none;">
<text class="label">放置托盘</text> <text class="label">放置托盘</text>
<view class="uni-list-cell-db"> <view class="uni-list-cell-db">
<text class="iconfont icon-sanjiaoxing1"></text> <text class="iconfont icon-sanjiaoxing1"></text>
...@@ -145,7 +145,7 @@ ...@@ -145,7 +145,7 @@
<view class="uni-input">{{ traySelectOption[index].tray_sn || '请选择' }}</view> <view class="uni-input">{{ traySelectOption[index].tray_sn || '请选择' }}</view>
</picker> </picker>
</view> </view>
</view> </view> -->
</view> </view>
</view> </view>
<view class="btn row rowCenter verCenter" @click="submit()">确认收货</view> <view class="btn row rowCenter verCenter" @click="submit()">确认收货</view>
...@@ -203,12 +203,26 @@ export default { ...@@ -203,12 +203,26 @@ export default {
}, },
onLoad(options) { onLoad(options) {
this.form.number = options.number || ''; //获取参数入仓号 this.form.number = options.number || ''; //获取参数入仓号
this.getTraySelectOption(); //获取托盘数据 //停止搜索,节省系统资源
uni.stopBluetoothDevicesDiscovery({
success: res => {
console.log('停止搜索', JSON.stringify(res.errMsg));
}
});
try {
uni.removeStorageSync('device');
} catch (e) {
// error
}
}, },
onShow() { onShow() {
const deviceData = uni.getStorageSync('device') || ''; //获取已连接蓝牙设备信息 //获取已连接蓝牙设备信息
const deviceData = uni.getStorageSync('device') || '';
if (deviceData) { if (deviceData) {
this.deviceId = deviceData.deviceId; this.deviceId = deviceData.deviceId;
this.serviceId = deviceData.serviceId;
this.characteristics = deviceData.characteristics;
this.device_name = deviceData.name; this.device_name = deviceData.name;
} else { } else {
this.deviceId = ''; this.deviceId = '';
...@@ -370,15 +384,6 @@ export default { ...@@ -370,15 +384,6 @@ export default {
return false; return false;
} }
if (!this.form.wstylt_id) {
uni.showModal({
title: '提示',
content: '请选择放置托盘',
showCancel: false
});
return false;
}
//截取入仓号 //截取入仓号
if (this.form.number) { if (this.form.number) {
this.printForm.erp_order_sn_pre = this.form.number.substr(0, 1); this.printForm.erp_order_sn_pre = this.form.number.substr(0, 1);
...@@ -430,16 +435,28 @@ export default { ...@@ -430,16 +435,28 @@ export default {
*/ */
print() { print() {
if (!this.device_name) { if (!this.device_name) {
uni.showToast({ uni.showModal({
icon: 'error', title: '提示',
title: '请连接蓝牙' content: '请连接蓝牙设备',
showCancel: false,
confirmText: '关闭',
success: function(res) {
if (res.confirm) {
uni.navigateTo({
url: '/pages/tag/deviceList'
});
} else if (res.cancel) {
console.log('用户点击取消');
}
}
}); });
return false; return false;
} }
this.request(API.printLabel, 'POST', this.printForm, true).then(res => { this.request(API.printLabel, 'POST', this.printForm, true).then(res => {
if (res.err_code === 0) { if (res.err_code === 0) {
this.printLabelData = res.data; this.printLabelData = res.data;
this.getBLEDeviceServices(); //根据获取的设备信息,连接设备 this.sendDataChange(); //发送数据
} else { } else {
uni.showToast({ uni.showToast({
title: res.err_msg, title: res.err_msg,
...@@ -449,89 +466,42 @@ export default { ...@@ -449,89 +466,42 @@ export default {
}); });
}, },
/** /**
* 来获取蓝牙设备所有服务
*/
getBLEDeviceServices() {
let that = this;
uni.showLoading({
title: '设备打印中'
});
uni.createBLEConnection({
deviceId: this.deviceId,
success: res => {
console.log('连接成功', JSON.stringify(res.errMsg));
//需延时连接,不然会报错
setTimeout(() => {
uni.getBLEDeviceServices({
deviceId: that.deviceId,
success: res => {
console.log('获取蓝牙设备所有服务', JSON.stringify(res.errMsg));
that.serviceId = res.services[0].uuid;
that.getBLEDeviceCharacteristics();
}
});
}, 1000);
},
fail: res => {
uni.hideLoading();
uni.showToast({
icon: 'error',
title: '连接设备失败!',
duration: 3000
});
}
});
},
/**
* 获取蓝牙设备某个服务中所有特征值
*/
getBLEDeviceCharacteristics() {
let that = this;
uni.getBLEDeviceCharacteristics({
deviceId: that.deviceId,
serviceId: that.serviceId,
success: res => {
that.characteristics = res.characteristics[0].uuid;
that.sendDataChange();
console.log('服务功能特征值', JSON.stringify(res.errMsg));
},
fail: res => {
uni.hideLoading();
uni.showToast({
icon: 'error',
title: '连接设备失败!',
duration: 3000
});
}
});
},
/**
* 向蓝牙发送数据 * 向蓝牙发送数据
*/ */
sendDataChange() { sendDataChange() {
console.log('第' + this.print_number + '次打印'); try {
let data = []; console.log('第' + this.print_number + '次打印');
if (this.printLabelData.length > 0) { let data = [];
data.push('! 60 200 200 250 1\r\n'); if (this.printLabelData.length > 0) {
data.push('T 8 0 10 20 入仓号:' + this.printLabelData[this.print_number].erp_order_sn + '\r\n'); data.push('! 60 200 200 250 1\r\n');
if (this.form.print_type == 1) { data.push('T 8 0 10 20 入仓号:' + this.printLabelData[this.print_number].erp_order_sn + '\r\n');
data.push('T 8 0 10 70 总箱数:' + this.printLabelData[this.print_number].label_num + '箱\r\n'); if (this.form.print_type == 1) {
} else { data.push('T 8 0 10 70 总箱数:' + this.printLabelData[this.print_number].label_num + '箱\r\n');
data.push('T 8 0 10 70 箱号:' + this.printLabelData[this.print_number].label_sort + '/' + this.printLabelData[this.print_number].label_num + '\r\n'); } else {
data.push('T 8 0 10 70 箱号:' + this.printLabelData[this.print_number].label_sort + '/' + this.printLabelData[this.print_number].label_num + '\r\n');
}
data.push('T 8 0 10 120 时间:' + this.printLabelData[this.print_number].create_time_cn + '\r\n');
data.push('T 8 0 10 170 仓库代码:' + this.printLabelData[this.print_number].warehouse_code + '\r\n');
data.push('T 8 0 10 220 流水号:' + this.printLabelData[this.print_number].wstyptll_id_sn + '\r\n');
data.push('B QR 260 20 M 2 U 8\r\n');
data.push('MA,' + this.printLabelData[this.print_number].erp_order_sn + '#\r\n');
data.push('ENDQR\r\n');
data.push('FORM\r\n');
data.push('PRINT\r\n');
} }
data.push('T 8 0 10 120 时间:' + this.printLabelData[this.print_number].create_time_cn + '\r\n'); let arrayBuffer = uni.base64ToArrayBuffer(ToBase64.encode64gb2312(data.join('')));
data.push('T 8 0 10 170 仓库代码:' + this.printLabelData[this.print_number].warehouse_code + '\r\n'); this.sendData64 = arrayBuffer;
data.push('T 8 0 10 220 流水号:' + this.printLabelData[this.print_number].wstyptll_id_sn + '\r\n'); this.writeBLECharacteristicValue();
data.push('B QR 260 20 M 2 U 8\r\n'); console.log(data.join(''));
data.push('MA,' + this.printLabelData[this.print_number].erp_order_sn + '#\r\n'); } catch (e) {
data.push('ENDQR\r\n'); uni.hideLoading();
data.push('FORM\r\n'); uni.showModal({
data.push('PRINT\r\n'); title: '提示',
content: '数据异常,请重试',
showCancel: false,
confirmText: '关闭'
});
} }
let arrayBuffer = uni.base64ToArrayBuffer(ToBase64.encode64gb2312(data.join('')));
this.sendData64 = arrayBuffer;
this.writeBLECharacteristicValue();
console.log(data.join(''));
}, },
/** /**
* 写入二进制数据 * 写入二进制数据
...@@ -539,39 +509,32 @@ export default { ...@@ -539,39 +509,32 @@ export default {
writeBLECharacteristicValue() { writeBLECharacteristicValue() {
let that = this; let that = this;
uni.writeBLECharacteristicValue({ uni.writeBLECharacteristicValue({
deviceId: that.deviceId, deviceId: this.deviceId,
serviceId: that.serviceId, serviceId: this.serviceId,
characteristicId: that.characteristics, characteristicId: this.characteristics,
value: that.sendData64, value: this.sendData64,
success(res) { success: res => {
that.print_number++; this.print_number++;
console.log('蓝牙写入成功:', JSON.stringify(res.errMsg)); console.log('打印张数:' + this.print_number);
console.log('打印计数:' + that.print_number); if (this.print_number < this.printLabelData.length) {
if (that.print_number < that.printLabelData.length) { this.sendDataChange();
that.sendDataChange();
} else { } else {
uni.hideLoading(); uni.hideLoading();
uni.closeBLEConnection({ //清空数据
deviceId: that.deviceId, that.print_number = 0;
success(res) { that.printLabelData = [];
console.log('关闭蓝牙连接:', JSON.stringify(res.errMsg)); that.sendData64 = '';
//清空数据 uni.showModal({
that.print_number = 0; title: '提示',
that.printLabelData = []; content: '打印成功,请查看标签打印机',
that.sendData64 = ''; showCancel: false,
uni.showModal({ confirmText: '关闭',
title: '提示', success: function(res) {
content: '打印成功,请查看标签打印机', if (res.confirm) {
showCancel: false, console.log('用户点击确定');
confirmText: '关闭', } else if (res.cancel) {
success: function(res) { console.log('用户点击取消');
if (res.confirm) { }
console.log('用户点击确定');
} else if (res.cancel) {
console.log('用户点击取消');
}
}
});
} }
}); });
} }
...@@ -580,7 +543,7 @@ export default { ...@@ -580,7 +543,7 @@ export default {
uni.hideLoading(); uni.hideLoading();
uni.showModal({ uni.showModal({
title: '提示', title: '提示',
content: '无法打印,请查看标签打印机连接状态', content: '打印失败,请检查打印机',
showCancel: false, showCancel: false,
confirmText: '关闭', confirmText: '关闭',
success: function(res) { success: function(res) {
......
...@@ -45,8 +45,9 @@ export default { ...@@ -45,8 +45,9 @@ export default {
status: false, status: false,
deviceName: '', deviceName: '',
list: [], list: [],
isOpenBle: false,
deviceId: '', deviceId: '',
serviceId: '',
characteristics: '',
textArr: [], textArr: [],
current: -1, current: -1,
refreshTransition: false refreshTransition: false
...@@ -65,18 +66,15 @@ export default { ...@@ -65,18 +66,15 @@ export default {
//在页面加载时候初始化蓝牙适配器 //在页面加载时候初始化蓝牙适配器
uni.openBluetoothAdapter({ uni.openBluetoothAdapter({
success: e => { success: res => {
console.log('初始化蓝牙成功:' + e.errMsg); console.log('初始化蓝牙成功:' + res.errMsg);
this.isOpenBle = true;
console.log(this.isOpenBle);
// 初始化完毕开始搜索 // 初始化完毕开始搜索
this.startBluetoothDeviceDiscovery(); this.startBluetoothDeviceDiscovery();
}, },
fail: e => { fail: res => {
console.log('初始化蓝牙失败,错误码:' + (e.errCode || e.errMsg));
wx.showModal({ wx.showModal({
title: '提示', title: '提示',
content: '蓝牙初始化失败,请到设置打开蓝牙', content: '蓝牙初始化失败,请打开系统蓝牙',
showCancel: false showCancel: false
}); });
} }
...@@ -84,60 +82,53 @@ export default { ...@@ -84,60 +82,53 @@ export default {
}, },
methods: { methods: {
/** /**
* 开始搜索蓝牙 * 搜索附近的蓝牙设备 并且监听寻找到新设备的事件
*/ */
startBluetoothDeviceDiscovery() { startBluetoothDeviceDiscovery() {
console.log('搜寻智能设备');
uni.showLoading({ uni.showLoading({
title: '搜索蓝牙设备', title: '搜索蓝牙设备',
mask: true mask: true
}); });
//在页面显示的时候判断是都已经初始化完成蓝牙适配器若成功,则开始查找设备 setTimeout(() => {
let self = this; uni.startBluetoothDevicesDiscovery({
setTimeout(function() { success: res => {
if (self.isOpenBle) { uni.onBluetoothDeviceFound(el => {
console.log('开始搜寻智能设备'); this.getBluetoothDevices();
uni.startBluetoothDevicesDiscovery({ });
success: res => { },
self.onBluetoothDeviceFound(); fail: res => {
}, uni.hideLoading();
fail: res => { uni.showModal({
uni.hideLoading(); title: '提示',
console.log('查找设备失败!'); content: '查找设备失败',
uni.showModal({ showCancel: false,
title: '提示', confirmText: '关闭',
content: '查找设备失败', success: r => {
showCancel: false, if (r.confirm) {
confirmText: '关闭' console.log('用户点击确定');
}); this.startBluetoothDeviceDiscovery();
} } else if (r.cancel) {
}); console.log('用户点击取消');
} else { }
console.log('未初始化蓝牙是配饰器:' + self.isOpenBle); }
} });
}
});
}, 300); }, 300);
}, },
/** /**
/**
* 发现外围设备
*/
onBluetoothDeviceFound() {
uni.onBluetoothDeviceFound(el => {
console.log('开始监听寻找到新设备的事件');
this.getBluetoothDevices();
});
},
/**
* 获取在蓝牙模块生效期间所有已发现的蓝牙设备。包括已经和本机处于连接状态的设备。 * 获取在蓝牙模块生效期间所有已发现的蓝牙设备。包括已经和本机处于连接状态的设备。
*/ */
getBluetoothDevices() { getBluetoothDevices() {
uni.getBluetoothDevices({ uni.getBluetoothDevices({
success: res => { success: res => {
console.log('获取蓝牙设备成功:' + res.errMsg); console.log('获取蓝牙设备成功:' + res.errMsg);
var devices = []; var devices = []; //临时存储设备列表
var textArr = []; //连接状态显示
var num = 0; var num = 0;
var textArr = [];
for (var i = 0; i < res.devices.length; i++) { for (var i = 0; i < res.devices.length; i++) {
//过滤未知设备
if (res.devices[i].name != '未知设备') { if (res.devices[i].name != '未知设备') {
textArr.push('连接'); textArr.push('连接');
devices[num] = res.devices[i]; devices[num] = res.devices[i];
...@@ -146,6 +137,7 @@ export default { ...@@ -146,6 +137,7 @@ export default {
} }
this.list = devices; this.list = devices;
this.textArr = textArr; this.textArr = textArr;
if (devices.length > 0) { if (devices.length > 0) {
uni.hideLoading(); uni.hideLoading();
} }
...@@ -156,36 +148,38 @@ export default { ...@@ -156,36 +148,38 @@ export default {
* 停止搜索蓝牙设备 * 停止搜索蓝牙设备
*/ */
stopBluetoothDevicesDiscovery() { stopBluetoothDevicesDiscovery() {
//成功找到对应蓝牙设备后 停止搜寻附近的蓝牙外围设备
let that = this;
uni.stopBluetoothDevicesDiscovery({ uni.stopBluetoothDevicesDiscovery({
success(res) { success: res => {
console.log('停止搜索', JSON.stringify(res.errMsg)); console.log('停止搜索', JSON.stringify(res.errMsg));
that.getBLEDeviceServices(); this.getBLEDeviceServices();
} }
}); });
}, },
/** /**
* 获取蓝牙服务 * 来获取蓝牙设备所有服务
*/ */
getBLEDeviceServices() { getBLEDeviceServices() {
//来获取蓝牙设备所有服务 //来获取蓝牙设备所有服务
uni.createBLEConnection({ uni.createBLEConnection({
deviceId: this.deviceId, deviceId: this.deviceId,
success: res => { success: res => {
console.log('连接成功', JSON.stringify(res.errMsg)); //需延时连接,不然会报错
uni.showToast({ setTimeout(() => {
title: '蓝牙连接成功', uni.getBLEDeviceServices({
icon: 'success' deviceId: this.deviceId,
}); success: res => {
this.status = true; console.log('获取服务', JSON.stringify(res.errMsg));
this.$set(this.textArr, this.current, '已连接'); //改变数组蓝牙连接状态 this.serviceId = res.services[0].uuid;
uni.setStorageSync('device', { name: this.deviceName, deviceId: this.deviceId }); this.getBLEDeviceCharacteristics();
}
});
}, 1000);
}, },
fail: error => { fail: error => {
uni.hideLoading();
uni.showModal({ uni.showModal({
title: '提示', title: '提示',
content: '连接设备失败', content: '连接设备失败,请重试',
showCancel: false, showCancel: false,
confirmText: '关闭' confirmText: '关闭'
}); });
...@@ -193,60 +187,104 @@ export default { ...@@ -193,60 +187,104 @@ export default {
}); });
}, },
/** /**
* 获取蓝牙设备某个服务中所有特征值
*/
getBLEDeviceCharacteristics() {
uni.getBLEDeviceCharacteristics({
deviceId: this.deviceId,
serviceId: this.serviceId,
success: res => {
this.characteristics = res.characteristics[0].uuid; //存储蓝牙所有特征值
this.status = true; //连接状态
this.$set(this.textArr, this.current, '已连接'); //改变数组蓝牙连接状态
//存储蓝牙数据
uni.setStorageSync('device', {
name: this.deviceName,
deviceId: this.deviceId,
serviceId: this.serviceId,
characteristics: this.characteristics
});
uni.showToast({
title: '蓝牙连接成功',
icon: 'success'
});
setTimeout(() => {
uni.navigateBack({
delta: 1
});
}, 2000);
console.log('蓝牙特征值', JSON.stringify(res.errMsg));
},
fail: res => {
uni.hideLoading();
uni.showToast({
icon: 'error',
content: '连接设备失败,请重试',
duration: 3000
});
}
});
},
/**
* 连接蓝牙 * 连接蓝牙
*/ */
connectChange(name, deviceId, index) { connectChange(name, deviceId, index) {
uni.showLoading({
title: '连接设备中'
});
//判断设备当前是否有连接 //判断设备当前是否有连接
if (this.deviceId) { if (this.textArr[index] == '连接') {
//先断开现有蓝牙连接 uni.showLoading({
uni.closeBLEConnection({ title: '连接设备中'
deviceId: deviceId, });
success: res => { if (!this.deviceId) {
console.log('断开蓝牙连接:', JSON.stringify(res.errMsg)); this.deviceName = name; //设备名称
this.deviceName = name; //设备名称 this.deviceId = deviceId; //设备id
this.deviceId = deviceId; //设备id this.current = index; //记录当前连接设备索引
this.current = index;
this.stopBluetoothDevicesDiscovery(); //先停止蓝牙搜索 this.stopBluetoothDevicesDiscovery(); //先停止蓝牙搜索
//恢复蓝牙连接状态的初始值 //恢复蓝牙连接状态的初始值
if (this.textArr.length > 0) { if (this.textArr.length > 0) {
var tempArr = []; var tempArr = [];
for (let i = 0; i < this.textArr.length; i++) { for (let i = 0; i < this.textArr.length; i++) {
tempArr.push('连接'); tempArr.push('连接');
}
this.textArr = tempArr;
} }
}, this.textArr = tempArr;
fail: error => {
uni.hideLoading();
uni.showModal({
title: '提示',
content: '断开设备失败',
showCancel: false,
confirmText: '关闭'
});
}
});
} else {
this.deviceName = name; //设备名称
this.deviceId = deviceId; //设备id
this.current = index;
this.stopBluetoothDevicesDiscovery(); //先停止蓝牙搜索
//恢复蓝牙连接状态的初始值
if (this.textArr.length > 0) {
var tempArr = [];
for (let i = 0; i < this.textArr.length; i++) {
tempArr.push('连接');
} }
this.textArr = tempArr; } else {
uni.closeBLEConnection({
deviceId: deviceId,
success: res => {
console.log('断开蓝牙连接:', JSON.stringify(res.errMsg));
this.deviceName = name; //设备名称
this.deviceId = deviceId; //设备id
this.current = index; //记录当前连接设备索引
this.stopBluetoothDevicesDiscovery(); //先停止蓝牙搜索
//恢复蓝牙连接状态的初始值
if (this.textArr.length > 0) {
var tempArr = [];
for (let i = 0; i < this.textArr.length; i++) {
tempArr.push('连接');
}
this.textArr = tempArr;
}
},
fail: error => {
uni.hideLoading();
uni.showModal({
title: '提示',
content: '断开设备失败',
showCancel: false,
confirmText: '关闭'
});
}
});
} }
} else {
uni.showToast({
title: '已连接状态',
icon: 'none'
});
} }
}, },
/** /**
...@@ -257,7 +295,7 @@ export default { ...@@ -257,7 +295,7 @@ export default {
success: res => { success: res => {
console.log('停止搜索', JSON.stringify(res.errMsg)); console.log('停止搜索', JSON.stringify(res.errMsg));
this.list = []; this.list = [];
this.startBluetoothDeviceDiscovery(); this.startBluetoothDeviceDiscovery(); //重新搜索蓝牙
//刷新图标交互 //刷新图标交互
this.refreshTransition = true; this.refreshTransition = true;
...@@ -271,13 +309,19 @@ export default { ...@@ -271,13 +309,19 @@ export default {
* 断开连接 * 断开连接
*/ */
closeBLEConnection() { closeBLEConnection() {
uni.showLoading({
title: '断开中...'
});
uni.closeBLEConnection({ uni.closeBLEConnection({
deviceId: this.deviceId, deviceId: this.deviceId,
success: res => { success: res => {
console.log('断开蓝牙连接成功:', JSON.stringify(res.errMsg)); console.log('断开蓝牙连接成功:', JSON.stringify(res.errMsg));
//重置基础信息
this.status = false; this.status = false;
this.deviceId = ''; this.deviceId = '';
this.deviceName = ''; this.deviceName = '';
//恢复蓝牙连接状态的初始值 //恢复蓝牙连接状态的初始值
if (this.textArr.length > 0) { if (this.textArr.length > 0) {
var tempArr = []; var tempArr = [];
...@@ -291,6 +335,15 @@ export default { ...@@ -291,6 +335,15 @@ export default {
title: '已断开蓝牙', title: '已断开蓝牙',
icon: 'success' icon: 'success'
}); });
},
fail: res => {
uni.hideLoading();
uni.showModal({
title: '提示',
content: '断开设备失败',
showCancel: false,
confirmText: '关闭'
});
} }
}); });
} }
......
...@@ -36,14 +36,14 @@ ...@@ -36,14 +36,14 @@
<view class="label">{{ text }}</view> <view class="label">{{ text }}</view>
<input type="number" v-model="form.label_num" placeholder-style="font-size:24rpx;color:#404547;" class="uni-input" placeholder="请输入" /> <input type="number" v-model="form.label_num" placeholder-style="font-size:24rpx;color:#404547;" class="uni-input" placeholder="请输入" />
</view> </view>
<view class="box row verCenter"> <!-- <view class="box row verCenter">
<view class="label">放置托盘</view> <view class="label">放置托盘</view>
<view class="el-select row verCenter"> <view class="el-select row verCenter">
<picker @change="bindPickerChange" :value="index" :range="traySelectOption" range-key="tray_sn"> <picker @change="bindPickerChange" :value="index" :range="traySelectOption" range-key="tray_sn">
<view class="uni-input">{{ traySelectOption[index].tray_sn || '请选择' }}</view> <view class="uni-input">{{ traySelectOption[index].tray_sn || '请选择' }}</view>
</picker> </picker>
</view> </view>
</view> </view> -->
</view> </view>
<view class="btn row rowCenter verCenter" @click="submit()">提交打印</view> <view class="btn row rowCenter verCenter" @click="submit()">提交打印</view>
</view> </view>
...@@ -81,7 +81,13 @@ export default { ...@@ -81,7 +81,13 @@ export default {
}; };
}, },
onLoad(option) { onLoad(option) {
this.getTraySelectOption(); //停止搜索,节省系统资源
uni.stopBluetoothDevicesDiscovery({
success: res => {
console.log('停止搜索', JSON.stringify(res.errMsg));
}
});
try { try {
uni.removeStorageSync('device'); uni.removeStorageSync('device');
} catch (e) { } catch (e) {
...@@ -89,9 +95,12 @@ export default { ...@@ -89,9 +95,12 @@ export default {
} }
}, },
onShow() { onShow() {
const deviceData = uni.getStorageSync('device') || ''; //获取已连接蓝牙设备信息 //获取已连接蓝牙设备信息
const deviceData = uni.getStorageSync('device') || '';
if (deviceData) { if (deviceData) {
this.deviceId = deviceData.deviceId; this.deviceId = deviceData.deviceId;
this.serviceId = deviceData.serviceId;
this.characteristics = deviceData.characteristics;
this.device_name = deviceData.name; this.device_name = deviceData.name;
} else { } else {
this.deviceId = ''; this.deviceId = '';
...@@ -121,12 +130,24 @@ export default { ...@@ -121,12 +130,24 @@ export default {
}, },
submit() { submit() {
if (!this.device_name) { if (!this.device_name) {
uni.showToast({ uni.showModal({
icon: 'error', title: '提示',
title: '请连接蓝牙设备' content: '请连接蓝牙设备',
showCancel: false,
confirmText: '关闭',
success: function(res) {
if (res.confirm) {
uni.navigateTo({
url: '/pages/tag/deviceList'
});
} else if (res.cancel) {
console.log('用户点击取消');
}
}
}); });
return false; return false;
} }
if (!this.form.erp_order_sn_pre) { if (!this.form.erp_order_sn_pre) {
uni.showToast({ uni.showToast({
icon: 'error', icon: 'error',
...@@ -165,78 +186,16 @@ export default { ...@@ -165,78 +186,16 @@ export default {
return false; return false;
} }
if (!this.form.tray_remark) {
uni.showToast({
icon: 'error',
title: '请选择放置托盘'
});
return false;
}
this.request(API.printLabel, 'POST', this.form, false).then(res => { this.request(API.printLabel, 'POST', this.form, false).then(res => {
if (res.err_code === 0) { if (res.err_code === 0) {
this.printLabelData = res.data; this.printLabelData = res.data;
this.getBLEDeviceServices(); //根据获取的设备信息,连接设备 this.sendDataChange(); //发送数据
} else { } else {
uni.showToast({ uni.showModal({
title: res.err_msg, title: '提示',
icon: 'error' content: res.err_msg,
}); showCancel: false,
} confirmText: '关闭'
});
},
/**
* 来获取蓝牙设备所有服务
*/
getBLEDeviceServices() {
let that = this;
uni.showLoading({
title: '设备打印中'
});
uni.createBLEConnection({
deviceId: this.deviceId,
success: res => {
console.log('连接成功', JSON.stringify(res.errMsg));
//需延时连接,不然会报错
setTimeout(() => {
uni.getBLEDeviceServices({
deviceId: that.deviceId,
success: res => {
console.log('获取蓝牙设备所有服务', JSON.stringify(res.errMsg));
that.serviceId = res.services[0].uuid;
that.getBLEDeviceCharacteristics();
}
});
}, 1000);
},
fail: res => {
uni.hideLoading();
uni.showToast({
icon: 'error',
title: '连接设备失败!',
duration: 3000
});
}
});
},
/**
* 获取蓝牙设备某个服务中所有特征值
*/
getBLEDeviceCharacteristics() {
let that = this;
uni.getBLEDeviceCharacteristics({
deviceId: that.deviceId,
serviceId: that.serviceId,
success: res => {
that.characteristics = res.characteristics[0].uuid;
that.sendDataChange();
console.log('服务功能特征值', JSON.stringify(res.errMsg));
},
fail: res => {
uni.hideLoading();
uni.showToast({
icon: 'error',
title: '连接设备失败!',
duration: 3000
}); });
} }
}); });
...@@ -245,29 +204,42 @@ export default { ...@@ -245,29 +204,42 @@ export default {
* 向蓝牙发送数据 * 向蓝牙发送数据
*/ */
sendDataChange() { sendDataChange() {
console.log('第' + this.print_number + '次打印'); try {
let data = []; uni.showLoading({
if (this.printLabelData.length > 0) { title: '打印中...'
data.push('! 60 200 200 250 1\r\n'); });
data.push('T 8 0 10 20 入仓号:' + this.printLabelData[this.print_number].erp_order_sn + '\r\n'); console.log('第' + this.print_number + '次打印');
if (this.form.print_type == 1) { let data = [];
data.push('T 8 0 10 70 总箱数:' + this.printLabelData[this.print_number].label_num + '箱\r\n'); if (this.printLabelData.length > 0) {
} else { data.push('! 60 200 200 250 1\r\n');
data.push('T 8 0 10 70 箱号:' + this.printLabelData[this.print_number].label_sort + '/' + this.printLabelData[this.print_number].label_num + '\r\n'); data.push('T 8 0 10 20 入仓号:' + this.printLabelData[this.print_number].erp_order_sn + '\r\n');
if (this.form.print_type == 1) {
data.push('T 8 0 10 70 总箱数:' + this.printLabelData[this.print_number].label_num + '箱\r\n');
} else {
data.push('T 8 0 10 70 箱号:' + this.printLabelData[this.print_number].label_sort + '/' + this.printLabelData[this.print_number].label_num + '\r\n');
}
data.push('T 8 0 10 120 时间:' + this.printLabelData[this.print_number].create_time_cn + '\r\n');
data.push('T 8 0 10 170 仓库代码:' + this.printLabelData[this.print_number].warehouse_code + '\r\n');
data.push('T 8 0 10 220 流水号:' + this.printLabelData[this.print_number].wstyptll_id_sn + '\r\n');
data.push('B QR 260 20 M 2 U 8\r\n');
data.push('MA,' + this.printLabelData[this.print_number].erp_order_sn + '#\r\n');
data.push('ENDQR\r\n');
data.push('FORM\r\n');
data.push('PRINT\r\n');
} }
data.push('T 8 0 10 120 时间:' + this.printLabelData[this.print_number].create_time_cn + '\r\n'); let arrayBuffer = uni.base64ToArrayBuffer(ToBase64.encode64gb2312(data.join('')));
data.push('T 8 0 10 170 仓库代码:' + this.printLabelData[this.print_number].warehouse_code + '\r\n'); this.sendData64 = arrayBuffer;
data.push('T 8 0 10 220 流水号:' + this.printLabelData[this.print_number].wstyptll_id_sn + '\r\n'); this.writeBLECharacteristicValue();
data.push('B QR 260 20 M 2 U 8\r\n'); console.log(data.join(''));
data.push('MA,' + this.printLabelData[this.print_number].erp_order_sn + '#\r\n'); } catch (e) {
data.push('ENDQR\r\n'); uni.hideLoading();
data.push('FORM\r\n'); uni.showModal({
data.push('PRINT\r\n'); title: '提示',
content: '数据异常,请重试',
showCancel: false,
confirmText: '关闭'
});
} }
let arrayBuffer = uni.base64ToArrayBuffer(ToBase64.encode64gb2312(data.join('')));
this.sendData64 = arrayBuffer;
this.writeBLECharacteristicValue();
console.log(data.join(''));
}, },
/** /**
* 写入二进制数据 * 写入二进制数据
...@@ -275,39 +247,32 @@ export default { ...@@ -275,39 +247,32 @@ export default {
writeBLECharacteristicValue() { writeBLECharacteristicValue() {
let that = this; let that = this;
uni.writeBLECharacteristicValue({ uni.writeBLECharacteristicValue({
deviceId: that.deviceId, deviceId: this.deviceId,
serviceId: that.serviceId, serviceId: this.serviceId,
characteristicId: that.characteristics, characteristicId: this.characteristics,
value: that.sendData64, value: this.sendData64,
success(res) { success: res => {
that.print_number++; this.print_number++;
console.log('蓝牙写入成功:', JSON.stringify(res.errMsg)); console.log('打印张数:' + this.print_number);
console.log('打印计数:' + that.print_number); if (this.print_number < this.printLabelData.length) {
if (that.print_number < that.printLabelData.length) { this.sendDataChange();
that.sendDataChange();
} else { } else {
uni.hideLoading(); uni.hideLoading();
uni.closeBLEConnection({ //清空数据
deviceId: that.deviceId, that.print_number = 0;
success(res) { that.printLabelData = [];
console.log('关闭蓝牙连接:', JSON.stringify(res.errMsg)); that.sendData64 = '';
//清空数据 uni.showModal({
that.print_number = 0; title: '提示',
that.printLabelData = []; content: '打印成功,请查看标签打印机',
that.sendData64 = ''; showCancel: false,
uni.showModal({ confirmText: '关闭',
title: '提示', success: function(res) {
content: '打印成功,请查看标签打印机', if (res.confirm) {
showCancel: false, console.log('用户点击确定');
confirmText: '关闭', } else if (res.cancel) {
success: function(res) { console.log('用户点击取消');
if (res.confirm) { }
console.log('用户点击确定');
} else if (res.cancel) {
console.log('用户点击取消');
}
}
});
} }
}); });
} }
...@@ -316,7 +281,7 @@ export default { ...@@ -316,7 +281,7 @@ export default {
uni.hideLoading(); uni.hideLoading();
uni.showModal({ uni.showModal({
title: '提示', title: '提示',
content: '无法打印,请查看标签打印机连接状态', content: '打印失败,请检查打印机',
showCancel: false, showCancel: false,
confirmText: '关闭', confirmText: '关闭',
success: function(res) { success: function(res) {
......
...@@ -63,10 +63,27 @@ export default { ...@@ -63,10 +63,27 @@ export default {
} }
}; };
}, },
onLoad(option) {
//停止搜索,节省系统资源
uni.stopBluetoothDevicesDiscovery({
success: res => {
console.log('停止搜索', JSON.stringify(res.errMsg));
}
});
try {
uni.removeStorageSync('device');
} catch (e) {
// error
}
},
onShow() { onShow() {
const deviceData = uni.getStorageSync('device') || ''; //获取已连接蓝牙设备信息 //获取已连接蓝牙设备信息
const deviceData = uni.getStorageSync('device') || '';
if (deviceData) { if (deviceData) {
this.deviceId = deviceData.deviceId; this.deviceId = deviceData.deviceId;
this.serviceId = deviceData.serviceId;
this.characteristics = deviceData.characteristics;
this.device_name = deviceData.name; this.device_name = deviceData.name;
} else { } else {
this.deviceId = ''; this.deviceId = '';
...@@ -94,9 +111,20 @@ export default { ...@@ -94,9 +111,20 @@ export default {
}, },
submit() { submit() {
if (!this.device_name) { if (!this.device_name) {
uni.showToast({ uni.showModal({
icon: 'error', title: '提示',
title: '请连接蓝牙设备' content: '请连接蓝牙设备',
showCancel: false,
confirmText: '关闭',
success: function(res) {
if (res.confirm) {
uni.navigateTo({
url: '/pages/tag/deviceList'
});
} else if (res.cancel) {
console.log('用户点击取消');
}
}
}); });
return false; return false;
} }
...@@ -142,7 +170,7 @@ export default { ...@@ -142,7 +170,7 @@ export default {
this.request(API.getPrintLabel, 'POST', { erp_order_sn: this.form.erp_order_sn_pre + this.form.erp_order_sn_number, label_sort: this.form.label_sort }, true).then(res => { this.request(API.getPrintLabel, 'POST', { erp_order_sn: this.form.erp_order_sn_pre + this.form.erp_order_sn_number, label_sort: this.form.label_sort }, true).then(res => {
if (res.err_code === 0) { if (res.err_code === 0) {
this.printLabelData = res.data; this.printLabelData = res.data;
this.getBLEDeviceServices(); //根据获取的设备信息,连接设备 this.sendDataChange(); //发送数据
} else { } else {
uni.showToast({ uni.showToast({
title: res.err_msg, title: res.err_msg,
...@@ -152,88 +180,44 @@ export default { ...@@ -152,88 +180,44 @@ export default {
}); });
}, },
/** /**
* 来获取蓝牙设备所有服务
*/
getBLEDeviceServices() {
let that = this;
uni.showLoading({
title: '设备打印中'
});
uni.createBLEConnection({
deviceId: this.deviceId,
success: res => {
console.log('连接成功', JSON.stringify(res));
//需延时连接,不然会报错
setTimeout(function() {
uni.getBLEDeviceServices({
deviceId: that.deviceId,
success: res => {
console.log('获取蓝牙设备所有服务', res);
that.serviceId = res.services[0].uuid;
that.getBLEDeviceCharacteristics();
}
});
}, 1000);
},
fail: res => {
uni.hideLoading();
uni.showToast({
icon: 'error',
title: '连接设备失败!',
duration: 3000
});
}
});
},
/**
* 获取蓝牙设备某个服务中所有特征值
*/
getBLEDeviceCharacteristics() {
let that = this;
uni.getBLEDeviceCharacteristics({
deviceId: that.deviceId,
serviceId: that.serviceId,
success: res => {
that.characteristics = res.characteristics[0].uuid;
that.sendDataChange();
console.log('服务功能特征值', res);
},
fail: res => {
uni.hideLoading();
uni.showToast({
icon: 'error',
title: '连接设备失败!',
duration: 3000
});
}
});
},
/**
* 向蓝牙发送数据 * 向蓝牙发送数据
*/ */
sendDataChange() { sendDataChange() {
console.log('第' + this.print_number + '次打印'); try {
let data = []; uni.showLoading({
if (this.printLabelData.length > 0) { title: '打印中...'
data.push('! 60 200 200 200 1\r\n'); });
data.push('T 8 0 10 20 入仓号:' + this.printLabelData[this.print_number].erp_order_sn + '\r\n'); console.log('第' + this.print_number + '次打印');
if (this.print_type == 1) { let data = [];
data.push('T 8 0 10 70 总箱数:' + this.printLabelData[this.print_number].label_num + '箱\r\n'); if (this.printLabelData.length > 0) {
} else { data.push('! 60 200 200 200 1\r\n');
data.push('T 8 0 10 70 箱号:' + this.printLabelData[this.print_number].label_sort + '/' + this.printLabelData[this.print_number].label_num + '\r\n'); data.push('T 8 0 10 20 入仓号:' + this.printLabelData[this.print_number].erp_order_sn + '\r\n');
if (this.print_type == 1) {
data.push('T 8 0 10 70 总箱数:' + this.printLabelData[this.print_number].label_num + '箱\r\n');
} else {
data.push('T 8 0 10 70 箱号:' + this.printLabelData[this.print_number].label_sort + '/' + this.printLabelData[this.print_number].label_num + '\r\n');
}
data.push('T 8 0 10 120 时间:' + this.printLabelData[this.print_number].create_time_cn + '\r\n');
data.push('T 8 0 10 170 仓库代码:' + this.printLabelData[this.print_number].warehouse_code + '\r\n');
data.push('B QR 290 20 M 2 U 8\r\n');
data.push('MA,' + this.printLabelData[this.print_number].erp_order_sn + '#\r\n');
data.push('ENDQR\r\n');
data.push('FORM\r\n');
data.push('PRINT\r\n');
} }
data.push('T 8 0 10 120 时间:' + this.printLabelData[this.print_number].create_time_cn + '\r\n'); let arrayBuffer = uni.base64ToArrayBuffer(ToBase64.encode64gb2312(data.join('')));
data.push('T 8 0 10 170 仓库代码:' + this.printLabelData[this.print_number].warehouse_code + '\r\n'); this.sendData64 = arrayBuffer;
data.push('B QR 290 20 M 2 U 8\r\n'); this.writeBLECharacteristicValue();
data.push('MA,' + this.printLabelData[this.print_number].erp_order_sn + '#\r\n'); console.log(data.join(''));
data.push('ENDQR\r\n'); } catch (e) {
data.push('FORM\r\n'); uni.hideLoading();
data.push('PRINT\r\n'); uni.showModal({
title: '提示',
content: '数据异常,请重试',
showCancel: false,
confirmText: '关闭'
});
} }
let arrayBuffer = uni.base64ToArrayBuffer(ToBase64.encode64gb2312(data.join('')));
this.sendData64 = arrayBuffer;
this.writeBLECharacteristicValue();
console.log(data.join(''));
}, },
/** /**
* 写入二进制数据 * 写入二进制数据
...@@ -241,41 +225,32 @@ export default { ...@@ -241,41 +225,32 @@ export default {
writeBLECharacteristicValue() { writeBLECharacteristicValue() {
let that = this; let that = this;
uni.writeBLECharacteristicValue({ uni.writeBLECharacteristicValue({
deviceId: that.deviceId, deviceId: this.deviceId,
serviceId: that.serviceId, serviceId: this.serviceId,
characteristicId: that.characteristics, characteristicId: this.characteristics,
value: that.sendData64, value: this.sendData64,
success(res) { success: res => {
that.print_number++; this.print_number++;
console.log('蓝牙写入成功:', JSON.stringify(res.errMsg)); console.log('打印张数:' + this.print_number);
console.log('打印计数:' + that.print_number); if (this.print_number < this.printLabelData.length) {
if (that.print_number < that.printLabelData.length) { this.sendDataChange();
setTimeout(function() {
that.sendDataChange();
}, 0);
} else { } else {
uni.hideLoading(); uni.hideLoading();
uni.closeBLEConnection({ //清空数据
deviceId: that.deviceId, that.print_number = 0;
success(res) { that.printLabelData = [];
console.log('关闭蓝牙连接:', JSON.stringify(res.errMsg)); that.sendData64 = '';
//清空数据 uni.showModal({
that.print_number = 0; title: '提示',
that.printLabelData = []; content: '打印成功,请查看标签打印机',
that.sendData64 = ''; showCancel: false,
uni.showModal({ confirmText: '关闭',
title: '提示', success: function(res) {
content: '打印成功,请查看标签打印机', if (res.confirm) {
showCancel: false, console.log('用户点击确定');
confirmText: '关闭', } else if (res.cancel) {
success: function(res) { console.log('用户点击取消');
if (res.confirm) { }
console.log('用户点击确定');
} else if (res.cancel) {
console.log('用户点击取消');
}
}
});
} }
}); });
} }
...@@ -284,7 +259,7 @@ export default { ...@@ -284,7 +259,7 @@ export default {
uni.hideLoading(); uni.hideLoading();
uni.showModal({ uni.showModal({
title: '提示', title: '提示',
content: '无法打印,请查看标签打印机连接状态', content: '打印失败,请检查打印机',
showCancel: false, showCancel: false,
confirmText: '关闭', confirmText: '关闭',
success: function(res) { success: function(res) {
......
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