From 921f9607a3d2c4ae8bdf0267dbdacd68d40fd678 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8C=83=E9=B9=8F?= Date: Thu, 22 Aug 2024 16:37:15 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E4=BF=AE=E6=94=B9=E8=93=9D=E7=89=99?= =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E4=BD=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App.vue | 33 ++++++------ pages.json | 18 +++++++ pages/home/home.vue | 51 +++++++++++++++--- pages/lockDetail/lockDetail.vue | 17 ++++++ pages/searchDevice/searchDevice.vue | 17 ++++++ stores/basic.js | 10 ++++ stores/bluetooth.js | 84 ++++++++++++++++++++--------- stores/user.js | 4 +- 8 files changed, 184 insertions(+), 50 deletions(-) create mode 100644 pages/lockDetail/lockDetail.vue create mode 100644 pages/searchDevice/searchDevice.vue diff --git a/App.vue b/App.vue index f8e8dce..15ddf32 100644 --- a/App.vue +++ b/App.vue @@ -3,8 +3,7 @@ import { useUserStore } from '@/stores/user' import { useBasicStore } from '@/stores/basic' import { mapState, mapActions } from 'pinia' - import { getUserInfoRequest } from '@/api/user' - let vm + let firstCheckSetting = true export default { globalData: { @@ -16,28 +15,26 @@ computed: { ...mapState(useBluetoothStore, ['bluetoothStatus']), }, - onLaunch: function() { + onLaunch: async function() { // 检查强制升级 this.updateMiniProgram() - // // 初始化蓝牙 - // this.initBluetooth() - // // 监听蓝牙开关状态 - // this.onBluetoothState() - // // 监听蓝牙连接状态 - // this.onBluetoothConnectStatus() - // // 监听设备特征值变化 - // this.onBluetoothCharacteristicValueChange() - - this.getDeviceInfo() - this.getButtonInfo() + // 检查蓝牙权限 + const checkResult = await this.checkSetting() + console.log(checkResult) + if(checkResult === true) { + this.initAndListenBluetooth() + } }, - onShow: function() { - this.checkSetting() + onShow() { + if(firstCheckSetting) { + firstCheckSetting = false + } else { + this.checkSetting() + } }, methods: { ...mapActions(useBluetoothStore, ['initBluetooth', 'onBluetoothState', 'updateBluetoothStatus', 'checkSetting', - 'onBluetoothConnectStatus', 'onBluetoothCharacteristicValueChange']), - ...mapActions(useBasicStore, ['getDeviceInfo', 'getButtonInfo']), + 'onBluetoothConnectStatus', 'onBluetoothCharacteristicValueChange', 'updateInitBluetooth', 'initAndListenBluetooth']), ...mapActions(useUserStore, ['updateLoginStatus']), // 强制升级 updateMiniProgram() { diff --git a/pages.json b/pages.json index 348bf49..9c0ec72 100644 --- a/pages.json +++ b/pages.json @@ -100,6 +100,24 @@ "navigationBarBackgroundColor": "#63b8af", "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": { diff --git a/pages/home/home.vue b/pages/home/home.vue index f6eeefa..d96a461 100644 --- a/pages/home/home.vue +++ b/pages/home/home.vue @@ -12,7 +12,7 @@ - + 填加锁时,手机必须在锁旁边 @@ -21,7 +21,7 @@ {{group.groupName}} - + @@ -46,7 +46,7 @@ + class="button-add" @click="toSearchDevice"> @@ -60,6 +60,8 @@ import { getUserInfoRequest } from '@/api/user' import { useUserStore } from '@/stores/user' import { useLockStore } from '@/stores/lock' + import { useBluetoothStore } from '@/stores/bluetooth' + import { useBasicStore } from '@/stores/basic' import { mapState, mapActions } from 'pinia' export default { @@ -72,18 +74,18 @@ }, refresherTriggered: false, focus: false, - penging: false + penging: true } }, computed: { ...mapState(useUserStore, ['userInfo', 'isLogin']), - ...mapState(useLockStore, ['lockList', 'lockTotal']) + ...mapState(useLockStore, ['lockList', 'lockTotal']), + ...mapState(useBluetoothStore, ['bluetoothStatus', 'isInitBluetooth']), }, async onLoad() { uni.showLoading({ title: '加载中' }) - this.penging = true const token = uni.getStorageSync('token') if(token) { this.updateLoginStatus(true) @@ -99,6 +101,8 @@ timeFormat, ...mapActions(useUserStore, ['updateUserInfo', 'updateLoginStatus', 'login']), ...mapActions(useLockStore, ['getLockList', 'getRole', 'getTimeLimit']), + ...mapActions(useBluetoothStore, ['getBluetoothStatus', 'initAndListenBluetooth', 'updateCurrentLockInfo', 'checkSetting']), + ...mapActions(useBasicStore, ['routeJump']), async nextPage() { if(this.lockList.length < this.lockTotal) { this.search.pageNo++ @@ -131,6 +135,41 @@ }, getBlur() { 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() + } } } } diff --git a/pages/lockDetail/lockDetail.vue b/pages/lockDetail/lockDetail.vue new file mode 100644 index 0000000..608c2be --- /dev/null +++ b/pages/lockDetail/lockDetail.vue @@ -0,0 +1,17 @@ + + + + + diff --git a/pages/searchDevice/searchDevice.vue b/pages/searchDevice/searchDevice.vue new file mode 100644 index 0000000..608c2be --- /dev/null +++ b/pages/searchDevice/searchDevice.vue @@ -0,0 +1,17 @@ + + + + + diff --git a/stores/basic.js b/stores/basic.js index 351e5fd..43e57e0 100644 --- a/stores/basic.js +++ b/stores/basic.js @@ -51,6 +51,16 @@ const pages = [ name: 'webview', path: '/pages/webview/webview', tabBar: false + }, + { + name: 'lockDetail', + path: '/pages/lockDetail/lockDetail', + tabBar: false + }, + { + name: 'searchDevice', + path: '/pages/searchDevice/searchDevice', + tabBar: false } ] diff --git a/stores/bluetooth.js b/stores/bluetooth.js index 52870ef..541e2a7 100644 --- a/stores/bluetooth.js +++ b/stores/bluetooth.js @@ -56,11 +56,36 @@ export const useBluetoothStore = defineStore('ble', { // 当前锁信息 currentLockInfo: {}, // 消息序号 - messageCount: 1 + messageCount: 1, + // 是否初始化蓝牙 + isInitBluetooth: false } }, 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() { const that = this // 初始化蓝牙模块 @@ -69,7 +94,7 @@ export const useBluetoothStore = defineStore('ble', { success() { that.bluetoothStatus = 0 console.log('蓝牙初始化成功') - resolve() + resolve(true) }, fail(err) { console.log('蓝牙初始化失败', err) @@ -89,7 +114,12 @@ export const useBluetoothStore = defineStore('ble', { 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() { const that = this - uni.getSetting({ - async success(res) { - const bluetooth = res.authSetting['scope.bluetooth'] - if(that.bluetoothStatus === -1) { - if(bluetooth) { - that.bluetoothStatus = 0 - } else { + return new Promise((resolve) => { + uni.getSetting({ + async success(res) { + const bluetooth = res.authSetting['scope.bluetooth'] + if(that.bluetoothStatus === -1) { + if(bluetooth !== false) { + that.bluetoothStatus = 0 + } else { + that.bluetoothStatus = 2 + } + } else if(that.bluetoothStatus === 0 && bluetooth === false) { 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) { - that.bluetoothStatus = 2 - } else if(that.bluetoothStatus === 1 && !bluetooth) { - that.bluetoothStatus = 3 - } else if(that.bluetoothStatus === 2 && bluetooth) { - 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) + resolve(bluetooth) + }, + fail() { + resolve(false) } - console.log('蓝牙权限', bluetooth, that.bluetoothStatus) - } + }) }) }, // 连接蓝牙设备+获取设备服务+获取设备特征值 diff --git a/stores/user.js b/stores/user.js index f9b4e10..5aa4988 100644 --- a/stores/user.js +++ b/stores/user.js @@ -23,15 +23,15 @@ export const useUserStore = defineStore('user', { }, async login() { uni.setStorageSync('token', '3021|MZv7iEf0NwjCPSGx4QWs37zOjeVN3GrSJ2v7D56L7db1fcc5') - this.isLogin = true const { code, data } = await getUserInfoRequest() - useLockStore().getLockList({ + await useLockStore().getLockList({ pageNo: 1, pageSize: 50 }) if(code === 0) { this.updateUserInfo(data) } + this.isLogin = true return this.isLogin } }