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

View File

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

View File

@ -96,7 +96,7 @@
<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-col span="8">
<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 = '')=> {
uni.setStorageSync(XTOKEN_KEY, val)
@ -15,3 +15,12 @@ export const setSessionUserInfo = (val = {})=> {
export const getSessionUserInfo = ()=> {
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 USERINFO_KEY = 'USERINFO_KEY'
export const BLUETOOTH_KEY = 'BLUETOOTH_KEY'