Commit f2b558bd by liangjianmin

bug

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