1. 修改蓝牙初始化位置

This commit is contained in:
范鹏 2024-08-22 16:37:15 +08:00
parent 7663c1cecd
commit 921f9607a3
8 changed files with 184 additions and 50 deletions

33
App.vue
View File

@ -3,8 +3,7 @@
import { useUserStore } from '@/stores/user' import { useUserStore } from '@/stores/user'
import { useBasicStore } from '@/stores/basic' import { useBasicStore } from '@/stores/basic'
import { mapState, mapActions } from 'pinia' import { mapState, mapActions } from 'pinia'
import { getUserInfoRequest } from '@/api/user' let firstCheckSetting = true
let vm
export default { export default {
globalData: { globalData: {
@ -16,28 +15,26 @@
computed: { computed: {
...mapState(useBluetoothStore, ['bluetoothStatus']), ...mapState(useBluetoothStore, ['bluetoothStatus']),
}, },
onLaunch: function() { onLaunch: async function() {
// //
this.updateMiniProgram() this.updateMiniProgram()
// // //
// this.initBluetooth() const checkResult = await this.checkSetting()
// // console.log(checkResult)
// this.onBluetoothState() if(checkResult === true) {
// // this.initAndListenBluetooth()
// this.onBluetoothConnectStatus() }
// //
// this.onBluetoothCharacteristicValueChange()
this.getDeviceInfo()
this.getButtonInfo()
}, },
onShow: function() { onShow() {
this.checkSetting() if(firstCheckSetting) {
firstCheckSetting = false
} else {
this.checkSetting()
}
}, },
methods: { methods: {
...mapActions(useBluetoothStore, ['initBluetooth', 'onBluetoothState', 'updateBluetoothStatus', 'checkSetting', ...mapActions(useBluetoothStore, ['initBluetooth', 'onBluetoothState', 'updateBluetoothStatus', 'checkSetting',
'onBluetoothConnectStatus', 'onBluetoothCharacteristicValueChange']), 'onBluetoothConnectStatus', 'onBluetoothCharacteristicValueChange', 'updateInitBluetooth', 'initAndListenBluetooth']),
...mapActions(useBasicStore, ['getDeviceInfo', 'getButtonInfo']),
...mapActions(useUserStore, ['updateLoginStatus']), ...mapActions(useUserStore, ['updateLoginStatus']),
// //
updateMiniProgram() { updateMiniProgram() {

View File

@ -100,6 +100,24 @@
"navigationBarBackgroundColor": "#63b8af", "navigationBarBackgroundColor": "#63b8af",
"navigationStyle": "default" "navigationStyle": "default"
} }
},
{
"path": "pages/lockDetail/lockDetail",
"style": {
"navigationBarTitleText": "星星锁",
"navigationBarTextStyle": "white",
"navigationBarBackgroundColor": "#63b8af",
"navigationStyle": "default"
}
},
{
"path": "pages/searchDevice/searchDevice",
"style": {
"navigationBarTitleText": "搜索",
"navigationBarTextStyle": "white",
"navigationBarBackgroundColor": "#63b8af",
"navigationStyle": "default"
}
} }
], ],
"globalStyle": { "globalStyle": {

View File

@ -12,7 +12,7 @@
</view> </view>
<view class="lock-list" v-if="!penging"> <view class="lock-list" v-if="!penging">
<view v-if="lockList.length === 0 && search.searchStr === '' && !focus"> <view v-if="lockList.length === 0 && search.searchStr === '' && !focus">
<image src="/static/images/icon_add.png" mode="aspectFill" class="button-add-big"></image> <image src="/static/images/icon_add.png" mode="aspectFill" class="button-add-big" @click="toSearchDevice"></image>
<view class="text">填加锁时手机必须在锁旁边</view> <view class="text">填加锁时手机必须在锁旁边</view>
</view> </view>
<view v-else> <view v-else>
@ -21,7 +21,7 @@
<view class="group-name-text">{{group.groupName}}</view> <view class="group-name-text">{{group.groupName}}</view>
<view class="group-name-line"></view> <view class="group-name-line"></view>
</view> </view>
<view class="lock" v-for="lock in group.lockList" :key="lock.lockId"> <view class="lock" v-for="lock in group.lockList" :key="lock.lockId" @click="toLockDeatil(lock)">
<view class="lock-top"> <view class="lock-top">
<image class="lock-image-lock" src="/static/images/icon_lock.png"></image> <image class="lock-image-lock" src="/static/images/icon_lock.png"></image>
<view class="lock-top-right"> <view class="lock-top-right">
@ -46,7 +46,7 @@
</view> </view>
</scroll-view> </scroll-view>
<image v-if="lockList.length !== 0" src="/static/images/icon_add.png" mode="aspectFill" <image v-if="lockList.length !== 0" src="/static/images/icon_add.png" mode="aspectFill"
class="button-add"></image> class="button-add" @click="toSearchDevice"></image>
</view> </view>
<view v-else> <view v-else>
<view class="button-login" @click="login">登录</view> <view class="button-login" @click="login">登录</view>
@ -60,6 +60,8 @@
import { getUserInfoRequest } from '@/api/user' import { getUserInfoRequest } from '@/api/user'
import { useUserStore } from '@/stores/user' import { useUserStore } from '@/stores/user'
import { useLockStore } from '@/stores/lock' import { useLockStore } from '@/stores/lock'
import { useBluetoothStore } from '@/stores/bluetooth'
import { useBasicStore } from '@/stores/basic'
import { mapState, mapActions } from 'pinia' import { mapState, mapActions } from 'pinia'
export default { export default {
@ -72,18 +74,18 @@
}, },
refresherTriggered: false, refresherTriggered: false,
focus: false, focus: false,
penging: false penging: true
} }
}, },
computed: { computed: {
...mapState(useUserStore, ['userInfo', 'isLogin']), ...mapState(useUserStore, ['userInfo', 'isLogin']),
...mapState(useLockStore, ['lockList', 'lockTotal']) ...mapState(useLockStore, ['lockList', 'lockTotal']),
...mapState(useBluetoothStore, ['bluetoothStatus', 'isInitBluetooth']),
}, },
async onLoad() { async onLoad() {
uni.showLoading({ uni.showLoading({
title: '加载中' title: '加载中'
}) })
this.penging = true
const token = uni.getStorageSync('token') const token = uni.getStorageSync('token')
if(token) { if(token) {
this.updateLoginStatus(true) this.updateLoginStatus(true)
@ -99,6 +101,8 @@
timeFormat, timeFormat,
...mapActions(useUserStore, ['updateUserInfo', 'updateLoginStatus', 'login']), ...mapActions(useUserStore, ['updateUserInfo', 'updateLoginStatus', 'login']),
...mapActions(useLockStore, ['getLockList', 'getRole', 'getTimeLimit']), ...mapActions(useLockStore, ['getLockList', 'getRole', 'getTimeLimit']),
...mapActions(useBluetoothStore, ['getBluetoothStatus', 'initAndListenBluetooth', 'updateCurrentLockInfo', 'checkSetting']),
...mapActions(useBasicStore, ['routeJump']),
async nextPage() { async nextPage() {
if(this.lockList.length < this.lockTotal) { if(this.lockList.length < this.lockTotal) {
this.search.pageNo++ this.search.pageNo++
@ -131,6 +135,41 @@
}, },
getBlur() { getBlur() {
this.focus = false this.focus = false
},
async toSearchDevice() {
if(this.bluetoothStatus !== 0) {
this.getBluetoothStatus()
return
}
let result = true
if(!this.isInitBluetooth) {
result = await this.initAndListenBluetooth()
}
if(result) {
this.routeJump({
name: 'searchDevice'
})
} else {
this.checkSetting()
}
},
async toLockDeatil(lock) {
if(this.bluetoothStatus !== 0) {
this.getBluetoothStatus()
return
}
let result = true
if(!this.isInitBluetooth) {
result = await this.initAndListenBluetooth()
}
if(result) {
this.updateCurrentLockInfo(lock)
this.routeJump({
name: 'lockDetail'
})
} else {
this.checkSetting()
}
} }
} }
} }

View File

@ -0,0 +1,17 @@
<template>
<view>
</view>
</template>
<script>
export default {
data () {
return {}
}
}
</script>
<style lang="scss">
</style>

View File

@ -0,0 +1,17 @@
<template>
<view>
</view>
</template>
<script>
export default {
data () {
return {}
}
}
</script>
<style lang="scss">
</style>

View File

@ -51,6 +51,16 @@ const pages = [
name: 'webview', name: 'webview',
path: '/pages/webview/webview', path: '/pages/webview/webview',
tabBar: false tabBar: false
},
{
name: 'lockDetail',
path: '/pages/lockDetail/lockDetail',
tabBar: false
},
{
name: 'searchDevice',
path: '/pages/searchDevice/searchDevice',
tabBar: false
} }
] ]

View File

@ -56,11 +56,36 @@ export const useBluetoothStore = defineStore('ble', {
// 当前锁信息 // 当前锁信息
currentLockInfo: {}, currentLockInfo: {},
// 消息序号 // 消息序号
messageCount: 1 messageCount: 1,
// 是否初始化蓝牙
isInitBluetooth: false
} }
}, },
actions: { actions: {
// 初始化蓝牙模块并监听蓝牙状态变化 // 初始化并监听
async initAndListenBluetooth() {
// 初始化蓝牙
const initResult = await this.initBluetooth()
if (initResult) {
// 更新蓝牙初始化状态
this.updateInitBluetooth(true)
// 监听蓝牙开关状态
this.onBluetoothState()
// 监听蓝牙连接状态
this.onBluetoothConnectStatus()
// 监听设备特征值变化
this.onBluetoothCharacteristicValueChange()
return true
} else {
return false
}
},
// 更新是否初始化蓝牙字段
updateInitBluetooth(value) {
this.isInitBluetooth = value
},
// 初始化蓝牙模块
initBluetooth() { initBluetooth() {
const that = this const that = this
// 初始化蓝牙模块 // 初始化蓝牙模块
@ -69,7 +94,7 @@ export const useBluetoothStore = defineStore('ble', {
success() { success() {
that.bluetoothStatus = 0 that.bluetoothStatus = 0
console.log('蓝牙初始化成功') console.log('蓝牙初始化成功')
resolve() resolve(true)
}, },
fail(err) { fail(err) {
console.log('蓝牙初始化失败', err) console.log('蓝牙初始化失败', err)
@ -89,7 +114,12 @@ export const useBluetoothStore = defineStore('ble', {
that.bluetoothStatus = 2 that.bluetoothStatus = 2
} }
} }
resolve() // 蓝牙已经初始化
if(err.errMsg === 'openBluetoothAdapter:fail already opened') {
resolve(true)
return
}
resolve(false)
} }
}) })
}) })
@ -385,29 +415,35 @@ export const useBluetoothStore = defineStore('ble', {
// 检查小程序设置 // 检查小程序设置
checkSetting() { checkSetting() {
const that = this const that = this
uni.getSetting({ return new Promise((resolve) => {
async success(res) { uni.getSetting({
const bluetooth = res.authSetting['scope.bluetooth'] async success(res) {
if(that.bluetoothStatus === -1) { const bluetooth = res.authSetting['scope.bluetooth']
if(bluetooth) { if(that.bluetoothStatus === -1) {
that.bluetoothStatus = 0 if(bluetooth !== false) {
} else { that.bluetoothStatus = 0
} else {
that.bluetoothStatus = 2
}
} else if(that.bluetoothStatus === 0 && bluetooth === false) {
that.bluetoothStatus = 2 that.bluetoothStatus = 2
} else if(that.bluetoothStatus === 1 && bluetooth === false) {
that.bluetoothStatus = 3
} else if(that.bluetoothStatus === 2 && bluetooth !== false) {
that.bluetoothStatus = 0
await that.initBluetooth()
} else if(that.bluetoothStatus === 3 && bluetooth !== false) {
that.bluetoothStatus = 1
await that.initBluetooth()
that.onBluetoothState()
} }
} else if(that.bluetoothStatus === 0 && !bluetooth) { console.log('蓝牙权限', bluetooth, that.bluetoothStatus)
that.bluetoothStatus = 2 resolve(bluetooth)
} else if(that.bluetoothStatus === 1 && !bluetooth) { },
that.bluetoothStatus = 3 fail() {
} else if(that.bluetoothStatus === 2 && bluetooth) { resolve(false)
that.bluetoothStatus = 0
await that.initBluetooth()
} else if(that.bluetoothStatus === 3 && bluetooth) {
that.bluetoothStatus = 1
await that.initBluetooth()
that.onBluetoothState()
} }
console.log('蓝牙权限', bluetooth, that.bluetoothStatus) })
}
}) })
}, },
// 连接蓝牙设备+获取设备服务+获取设备特征值 // 连接蓝牙设备+获取设备服务+获取设备特征值

View File

@ -23,15 +23,15 @@ export const useUserStore = defineStore('user', {
}, },
async login() { async login() {
uni.setStorageSync('token', '3021|MZv7iEf0NwjCPSGx4QWs37zOjeVN3GrSJ2v7D56L7db1fcc5') uni.setStorageSync('token', '3021|MZv7iEf0NwjCPSGx4QWs37zOjeVN3GrSJ2v7D56L7db1fcc5')
this.isLogin = true
const { code, data } = await getUserInfoRequest() const { code, data } = await getUserInfoRequest()
useLockStore().getLockList({ await useLockStore().getLockList({
pageNo: 1, pageNo: 1,
pageSize: 50 pageSize: 50
}) })
if(code === 0) { if(code === 0) {
this.updateUserInfo(data) this.updateUserInfo(data)
} }
this.isLogin = true
return this.isLogin return this.isLogin
} }
} }