This commit is contained in:
aihe 2024-05-13 00:49:19 +08:00
parent d2d632a087
commit 4fe62d1f08
5 changed files with 99 additions and 60 deletions

View File

@ -1,7 +1,7 @@
<template> <template>
<tpx-bluetooth v-model:value="blueToothData"> <tpx-bluetooth ref="curBlueTooth">
<template v-slot:button> <template v-slot:button>
<u-button @click="handlePrint" type="primary" :disabled="!(blueToothData.deviceId && blueToothData.serviceId && blueToothData.characteristicId)">打印</u-button> <u-button @click="handlePrint" type="primary">确认打印</u-button>
</template> </template>
<template v-slot:content> <template v-slot:content>
<view class="pt-20 pb-10">打印内容</view> <view class="pt-20 pb-10">打印内容</view>
@ -52,11 +52,6 @@ export default {
imgData: '', imgData: '',
percentage: 0, // 打印进度 percentage: 0, // 打印进度
threshold: 200, // 阈值(值越大,图片打印越深) threshold: 200, // 阈值(值越大,图片打印越深)
blueToothData: {
deviceId: '',
serviceId: '',
characteristicId: ''
}
} }
}, },
mounted () { mounted () {
@ -71,8 +66,10 @@ export default {
// 打印 // 打印
async handlePrint(){ async handlePrint(){
const that = this const that = this
let { deviceId, serviceId, characteristicId } = this.$refs.curBlueTooth.getInstance()
const opt = { const opt = {
...this.blueToothData, ...{ deviceId, serviceId, characteristicId },
onProgress: (percentage) => { onProgress: (percentage) => {
console.log("打印进度====", percentage) console.log("打印进度====", percentage)
that.percentage = percentage that.percentage = percentage

View File

@ -1,5 +1,5 @@
<template> <template>
<tpx-layout> <tpx-layout v-if="showConnectPage">
<template v-slot:header> <template v-slot:header>
<view class="blcok-white" style="margin-top: 0;"> <view class="blcok-white" style="margin-top: 0;">
<view > <view >
@ -27,8 +27,8 @@
</u-steps> </u-steps>
</view> </view>
</view> </view>
</template> </template>
<template v-slot:body> <template v-slot:body>
<view class="blcok-white"> <view class="blcok-white">
<view v-if="curTab == 0"> <view v-if="curTab == 0">
@ -90,15 +90,16 @@
</view> </view>
</template> </template>
</view> </view>
<slot name="content"></slot> <slot name="content"></slot>
</view> </view>
</template> </template>
</tpx-layout> </tpx-layout>
</template> </template>
<script> <script>
import { showLoading, showToast, hideLoading } from "@/common/untool" import { showLoading, showToast, hideLoading } from "@/common/untool"
import { getSessionBluetoothInfo, setSessionBluetoothInfo } from "@/session"
export default { export default {
computed: { computed: {
@ -113,68 +114,91 @@ export default {
}, },
watch: { watch: {
deviceId(val){ deviceId(val){
this.value.deviceId = val updateBlueSessionInfo()
this.$emit('update:value', this.value)
}, },
serviceId(val){ serviceId(val){
this.value.serviceId = val updateBlueSessionInfo()
this.$emit('update:value', this.value)
}, },
characteristicId(val){ characteristicId(val){
this.value.characteristicId = val updateBlueSessionInfo()
this.$emit('update:value', this.value)
} }
}, },
props:{ props:{
value: {
default () {
return {}
}
},
}, },
data () { data () {
let { deviceId, serviceId, characteristicId } = getSessionBluetoothInfo()
return { return {
showConnectPage: true,
canvasId: 'shareCanvas', canvasId: 'shareCanvas',
canvas_width: 240, canvas_width: 240,
canvas_height: 240, canvas_height: 240,
curTab: 0, curTab: 0,
searchLoading: false, searchLoading: false,
devices: [], devices: [],
deviceId: this.value?.deviceId, deviceId,
serverList: [], serverList: [],
serviceId: this.value?.deviceId, serviceId,
characteristics: [], characteristics: [],
characteristicId: this.value?.deviceId, characteristicId,
} }
}, },
mounted () { async mounted () {
// 初始化蓝牙模块 // 初始化蓝牙模块
this.openBluetoothAdapter() await this.openBluetoothAdapter()
// 自动搜索设备 this.autoConnect()
this.discoveryPrinter()
}, },
unmounted() { unmounted() {
this.stopDiscoveryPrinter() this.stopDiscoveryPrinter()
this.closeBluetoothAdapter() // this.closeBluetoothAdapter()
}, },
methods: { methods: {
updateBlueSessionInfo() {
let { deviceId, serviceId, characteristicId } = this
setSessionBluetoothInfo({ deviceId, serviceId, characteristicId })
},
// 自动连接历史蓝牙配置
async autoConnect() {
let conSta = await this.checkConnectStat()
// 自动连接
if(!conSta) {
this.discoveryPrinter()
}
return true
},
// 检查蓝牙 缓存连接状态 【获取不到characteristics说明连接已经过期】
async checkConnectStat(){
let bl = false
try{
await this.getBLEDeviceCharacteristics()
bl = true
}catch(e){
}
return bl
},
changeTab(type) { changeTab(type) {
this.curTab = type this.curTab = type
}, },
openBluetoothAdapter () { openBluetoothAdapter () {
var _this = this var _this = this
uni?.openBluetoothAdapter?.({ return new Promise((resolve, reject)=> {
complete (e) { uni?.openBluetoothAdapter?.({
console.log(e); complete (e) {
if (!e.errCode) { console.log(e);
console.log('初始化完成') if (!e.errCode) {
} else if (e.errCode == 10001) { resolve()
showToast('请打开手机蓝牙') console.log('初始化完成')
} else { } else if (e.errCode == 10001) {
console.log('openBluetoothAdapter:error=========', e.errMsg) reject(e)
showToast(e.errMsg) showToast('请打开手机蓝牙')
} else {
reject(e)
console.log('openBluetoothAdapter:error=========', e.errMsg)
showToast(e.errMsg)
}
} }
} })
}) })
}, },
// 开始搜寻附近的蓝牙外围设备 // 开始搜寻附近的蓝牙外围设备
@ -183,21 +207,26 @@ export default {
_this.devices = [] _this.devices = []
_this.searchLoading = true _this.searchLoading = true
_this.curTab = 0 _this.curTab = 0
uni?.startBluetoothDevicesDiscovery?.({ return new Promise((resolve, reject)=> {
complete (e) { uni?.startBluetoothDevicesDiscovery?.({
console.log(e) complete (e) {
if (e.errMsg == "startBluetoothDevicesDiscovery:ok") { console.log(e)
// ArrayBuffer转16进度字符串示例 if (e.errMsg == "startBluetoothDevicesDiscovery:ok") {
uni.onBluetoothDeviceFound(devices => { resolve()
console.log("========devices=====", devices) // ArrayBuffer转16进度字符串示例
let curDvs = devices?.devices?.[0] || {} uni.onBluetoothDeviceFound(devices => {
// 过滤重复的 console.log("========devices=====", devices)
if(curDvs.deviceId && _this.devices.map(t=> t.deviceId).indexOf(curDvs.deviceId) == -1) { let curDvs = devices?.devices?.[0] || {}
_this.devices.push(curDvs) // 过滤重复的
} if(curDvs.deviceId && _this.devices.map(t=> t.deviceId).indexOf(curDvs.deviceId) == -1) {
}) _this.devices.push(curDvs)
}
})
} else {
reject()
}
} }
} })
}) })
}, },
// 停止搜寻附近的蓝牙外围设备 // 停止搜寻附近的蓝牙外围设备
@ -294,7 +323,7 @@ export default {
}, },
getBLEDeviceCharacteristics () { getBLEDeviceCharacteristics () {
var _this = this var _this = this
return new Promise((resolve)=> { return new Promise((resolve, reject)=> {
showLoading('正在搜索特征值') showLoading('正在搜索特征值')
uni.getBLEDeviceCharacteristics({ uni.getBLEDeviceCharacteristics({
// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接 // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
@ -310,12 +339,15 @@ export default {
}, },
fail:(res)=>{ fail:(res)=>{
hideLoading() hideLoading()
resolve() reject()
console.log(res) console.log(res)
} }
}) })
}) })
}, },
getInstance() {
return this
}
} }
} }
</script> </script>

View File

@ -96,7 +96,7 @@
<tpx-checkitems :list="resData.list" v-model:value="submitParam.billCode" v-slot="dlCurrent" type="multiple" <tpx-checkitems :list="resData.list" v-model:value="submitParam.billCode" v-slot="dlCurrent" type="multiple"
> >
<view slot class="yditem mt-20"> <view slot class="yditem">
<u-row> <u-row>
<u-col span="8"> <u-col span="8">
<u-row> <u-row>

View File

@ -1,4 +1,4 @@
import { XTOKEN_KEY, USERINFO_KEY } from "./key" import { XTOKEN_KEY, USERINFO_KEY, BLUETOOTH_KEY } from "./key"
export const setSessionXToken = (val = '')=> { export const setSessionXToken = (val = '')=> {
uni.setStorageSync(XTOKEN_KEY, val) uni.setStorageSync(XTOKEN_KEY, val)
@ -14,4 +14,13 @@ export const setSessionUserInfo = (val = {})=> {
export const getSessionUserInfo = ()=> { export const getSessionUserInfo = ()=> {
return uni.getStorageSync(USERINFO_KEY) || {} return uni.getStorageSync(USERINFO_KEY) || {}
}
export const setSessionBluetoothInfo = (val = {})=> {
uni.setStorageSync(BLUETOOTH_KEY, val)
}
export const getSessionBluetoothInfo = ()=> {
return uni.getStorageSync(BLUETOOTH_KEY) || {}
} }

View File

@ -1,2 +1,3 @@
export const XTOKEN_KEY = 'XTOKEN_KEY' export const XTOKEN_KEY = 'XTOKEN_KEY'
export const USERINFO_KEY = 'USERINFO_KEY' export const USERINFO_KEY = 'USERINFO_KEY'
export const BLUETOOTH_KEY = 'BLUETOOTH_KEY'