wangfukang 3 years ago
parent
commit
0ffdd6a5f0
  1. BIN
      components/.DS_Store
  2. 145
      components/kk-printer/index.vue

BIN
components/.DS_Store

Binary file not shown.

145
components/kk-printer/index.vue

@ -70,7 +70,7 @@
writeId: '', writeId: '',
readId: '', readId: '',
iosNo: false, iosNo: false,
deviceType:'ios' deviceType: 'ios'
} }
}, },
props: { props: {
@ -104,7 +104,14 @@
mounted() { mounted() {
}, },
onLoad(){ watch: {
isShowSearch(newVal, oldVal) {
if(newVal == false){
this.$parent.$parent.isShow = true
}
}
},
onLoad() {
}, },
beforeDestroy() { beforeDestroy() {
@ -118,15 +125,17 @@
//点击打印按钮 //点击打印按钮
handlePrintTap() { handlePrintTap() {
let that = this let that = this
console.log(this.$parent)
this.$parent.$parent.clickShow();
uni.showLoading({ uni.showLoading({
mask:true, mask: true,
title: '加载中...' title: '加载中...'
}); });
uni.getSystemInfo({ uni.getSystemInfo({
success (res) { success(res) {
that.deviceType = res.osName that.deviceType = res.osName
console.log('设备类型',res.osName) console.log('设备类型', res.osName)
} }
}) })
// 初始化蓝牙模块 // 初始化蓝牙模块
wx.openBluetoothAdapter({ //111 wx.openBluetoothAdapter({ //111
@ -145,7 +154,7 @@
}, },
// 开始搜索附近的蓝牙外围设备 // 开始搜索附近的蓝牙外围设备
sousuo(){ sousuo() {
let that = this; let that = this;
wx.startBluetoothDevicesDiscovery({ //222 wx.startBluetoothDevicesDiscovery({ //222
success(res3) { success(res3) {
@ -156,20 +165,20 @@
console.log('获取设备失败', err) console.log('获取设备失败', err)
that.tui.toast('获取设备失败', err) that.tui.toast('获取设备失败', err)
wx.closeBLEConnection({ wx.closeBLEConnection({
deviceId:uni.getStorageSync('deviceId'), deviceId: uni.getStorageSync('deviceId'),
success (res) { success(res) {
console.log(res) console.log(res)
} }
}) })
uni.removeStorageSync('deviceId'); uni.removeStorageSync('deviceId');
this.isShowSearch = true that.isShowSearch = true
this.sousuo() that.sousuo()
uni.hideLoading(); uni.hideLoading();
} }
}) })
}, },
//停止搜索蓝牙设备 //停止搜索蓝牙设备
stopSearchBtnTap(){ stopSearchBtnTap() {
wx.stopBluetoothDevicesDiscovery() wx.stopBluetoothDevicesDiscovery()
}, },
/** /**
@ -177,13 +186,13 @@
* 有: 直接拿到缓存中的deviceId等,直接请求 wx.createBLEConnection * 有: 直接拿到缓存中的deviceId等,直接请求 wx.createBLEConnection
* 没有:才会调用 wx.onBluetoothDeviceFound 搜索 * 没有:才会调用 wx.onBluetoothDeviceFound 搜索
*/ */
jiantingshebei(){ jiantingshebei() {
let that = this; let that = this;
if(uni.getStorageSync('deviceId')){ if (uni.getStorageSync('deviceId')) {
console.log("111") console.log("111")
// that.huoqufuwu(uni.getStorageSync('deviceId')) // that.huoqufuwu(uni.getStorageSync('deviceId'))
that.lianejieshebei(uni.getStorageSync('deviceId')) that.lianejieshebei(uni.getStorageSync('deviceId'))
}else{ } else {
console.log("222") console.log("222")
uni.hideLoading(); uni.hideLoading();
this.isShowSearch = true this.isShowSearch = true
@ -192,7 +201,7 @@
console.log('监听新设备列表,拿到想要的就自动停止', res4) console.log('监听新设备列表,拿到想要的就自动停止', res4)
res4.devices.forEach((device) => { res4.devices.forEach((device) => {
if (!device.name && !device.localName) { if (!device.name && !device.localName) {
return return
} }
that.devicesList.push(device) that.devicesList.push(device)
}) })
@ -200,70 +209,70 @@
} }
}, },
lianejieshebei(deviceId){ lianejieshebei(deviceId) {
let that = this; let that = this;
// 连接设备 // 连接设备
wx.createBLEConnection({//555 wx.createBLEConnection({ //555
deviceId: deviceId, // 搜索到设备的 deviceId deviceId: deviceId, // 搜索到设备的 deviceId
success: () => { success: () => {
this.isShowSearch = false that.isShowSearch = false
console.log('连接设备成功') console.log('连接设备成功')
that.huoqufuwu(deviceId) that.huoqufuwu(deviceId)
}, },
fail: (res) => { fail: (res) => {
console.log('连接设备失败',res) console.log('连接设备失败', res)
that.tui.toast('连接设备失败', res) that.tui.toast('连接设备失败', res)
if(uni.getStorageSync('deviceId')){ if (uni.getStorageSync('deviceId')) {
this.huoqufuwu(uni.getStorageSync('deviceId')) that.huoqufuwu(uni.getStorageSync('deviceId'))
}else{ } else {
wx.closeBLEConnection({ wx.closeBLEConnection({
deviceId:uni.getStorageSync('deviceId'), deviceId: uni.getStorageSync('deviceId'),
success (res) { success(res) {
console.log(res) console.log(res)
} }
}) })
uni.removeStorageSync('deviceId') uni.removeStorageSync('deviceId')
this.sousuo() that.sousuo()
this.isShowSearch = true that.isShowSearch = true
} }
uni.hideLoading(); uni.hideLoading();
} }
}) })
}, },
huoqufuwu(deviceId){ huoqufuwu(deviceId) {
let that = this; let that = this;
//获取服务 //获取服务
wx.getBLEDeviceServices({ //666 wx.getBLEDeviceServices({ //666
deviceId: deviceId, // 搜索到设备的 deviceId deviceId: deviceId, // 搜索到设备的 deviceId
success: (res5) => { success: (res5) => {
console.log('获取服务成功',res5) console.log('获取服务成功', res5)
that.duxietezhengzhi(deviceId,res5) that.duxietezhengzhi(deviceId, res5)
}, },
fail: (res) => { fail: (res) => {
console.log(res) console.log(res)
that.tui.toast('获取服务失败', res) that.tui.toast('获取服务失败', res)
wx.closeBLEConnection({ wx.closeBLEConnection({
deviceId:uni.getStorageSync('deviceId'), deviceId: uni.getStorageSync('deviceId'),
success (res1) { success(res1) {
console.log(res1) console.log(res1)
} }
}) })
this.isShowSearch = true that.isShowSearch = true
uni.hideLoading(); uni.hideLoading();
} }
}) })
}, },
duxietezhengzhi(deviceId,res5){ duxietezhengzhi(deviceId, res5) {
let that = this; let that = this;
//读写特征值 //读写特征值
wx.getBLEDeviceCharacteristics({ //777 wx.getBLEDeviceCharacteristics({ //777
deviceId:deviceId, // 搜索到设备的 deviceId deviceId: deviceId, // 搜索到设备的 deviceId
serviceId:res5.services[0].uuid, // 上一步中找到的某个服务 serviceId: res5.services[0].uuid, // 上一步中找到的某个服务
success: (res6) => { success: (res6) => {
console.log('读写特征值成功',res6) console.log('读写特征值成功', res6)
for (let i =0; i <res6.characteristics.length; i++) { for (let i = 0; i < res6.characteristics.length; i++) {
let item =res6.characteristics[i] let item = res6.characteristics[i]
if (item.properties.write) { // 该特征值可写 if (item.properties.write) { // 该特征值可写
console.log('该特征值可写') console.log('该特征值可写')
uni.setStorageSync('deviceId', deviceId); uni.setStorageSync('deviceId', deviceId);
@ -277,34 +286,34 @@
fail: (res) => { fail: (res) => {
that.tui.toast('读写特征值失败', res) that.tui.toast('读写特征值失败', res)
wx.closeBLEConnection({ wx.closeBLEConnection({
deviceId:uni.getStorageSync('deviceId'), deviceId: uni.getStorageSync('deviceId'),
success (res) { success(res) {
console.log(res) console.log(res)
} }
}) })
this.isShowSearch = true that.isShowSearch = true
uni.hideLoading(); uni.hideLoading();
} }
}) })
}, },
xierushuju(){ xierushuju() {
let that = this; let that = this;
//写入数据 //写入数据
if(this.defaultText == '打印小票'){ if (this.defaultText == '打印小票') {
that.$emit('onPrintSmall'); that.$emit('onPrintSmall');
}else if(this.defaultText == '确认打印'){ } else if (this.defaultText == '确认打印') {
that.$emit('goPrint'); that.$emit('goPrint');
}else{ } else {
that.$emit('onPrint'); that.$emit('onPrint');
} }
console.log('查看数据是否正确',that.bufferData) console.log('查看数据是否正确', that.bufferData)
that.$nextTick(()=>{ that.$nextTick(() => {
var dataStr = '' var dataStr = ''
for(let m=0;m<that.bufferData.length;m++){ for (let m = 0; m < that.bufferData.length; m++) {
dataStr += that.bufferData[m] dataStr += that.bufferData[m]
} }
const maxChunk = that.deviceType== 'android'?20:180; const maxChunk = that.deviceType == 'android' ? 20 : 180;
const delay = that.deviceType== 'android'?20:180; const delay = that.deviceType == 'android' ? 20 : 180;
let buffer = gbk.strToGBKByte(dataStr) let buffer = gbk.strToGBKByte(dataStr)
for (let i = 0, j = 0, length = buffer.byteLength; i < length; i += maxChunk, j++) { for (let i = 0, j = 0, length = buffer.byteLength; i < length; i += maxChunk, j++) {
@ -323,28 +332,28 @@
characteristicId: uni.getStorageSync('characteristicId'), characteristicId: uni.getStorageSync('characteristicId'),
value: buffer, value: buffer,
success(res) { success(res) {
console.log('打印buffer',buffer) console.log('打印buffer', buffer)
console.log('writeBLECharacteristicValue success', res) console.log('writeBLECharacteristicValue success', res)
}, },
fail(res) { fail(res) {
console.log('writeBLECharacteristicValue fail', res) console.log('writeBLECharacteristicValue fail', res)
wx.closeBLEConnection({ wx.closeBLEConnection({
deviceId:uni.getStorageSync('deviceId'), deviceId: uni.getStorageSync('deviceId'),
success (res) { success(res) {
console.log(res) console.log(res)
} }
}) })
if(uni.getStorageSync('deviceId')){ if (uni.getStorageSync('deviceId')) {
console.log("111") console.log("111")
that.xierushuju() that.xierushuju()
}else{ } else {
that.jiantingshebei() that.jiantingshebei()
} }
} }
}) })
uni.hideLoading(); uni.hideLoading();
}, },
handleSearchClose(){ handleSearchClose() {
this.isShowSearch = false this.isShowSearch = false
} }
} }

Loading…
Cancel
Save