From a888ce74158aceca1ff08615b09857ab578e319c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8C=83=E9=B9=8F?= Date: Thu, 28 Nov 2024 13:45:45 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BC=98=E5=8C=96=E5=BC=80=E9=94=81?= =?UTF-8?q?=E9=80=9F=E5=BA=A6=EF=BC=8C=E6=B7=BB=E5=8A=A0=E5=BC=80=E9=94=81?= =?UTF-8?q?=E5=AE=9E=E6=97=B6=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + log.js | 40 +++++++++ starCloud.js | 235 ++++++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 235 insertions(+), 41 deletions(-) create mode 100644 log.js diff --git a/README.md b/README.md index 0ffa075..82a110e 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,7 @@ const $starCloud = useStarCloudStore() * @param {String} params.clientId 客户端Id * @param {String} params.clientSecret 客户端密码 * @param {String} params.env 环境 + * @param {Boolean} params.isReportLog 是否上报日志 */ $starCloud.initStarCloud(params) diff --git a/log.js b/log.js new file mode 100644 index 0000000..ff8ab75 --- /dev/null +++ b/log.js @@ -0,0 +1,40 @@ +import { isReportLog } from '@/starCloud/starCloud' + +const log = wx.getRealtimeLogManager ? wx.getRealtimeLogManager() : null + +export default { + debug() { + if (!log) return + if (!isReportLog) return + log.debug.apply(log, arguments) + }, + info() { + if (!log) return + if (!isReportLog) return + log.info.apply(log, arguments) + }, + warn() { + if (!log) return + if (!isReportLog) return + log.warn.apply(log, arguments) + }, + error() { + if (!log) return + if (!isReportLog) return + log.error.apply(log, arguments) + }, + setFilterMsg(msg) { + // 从基础库2.7.3开始支持 + if (!log || !log.setFilterMsg) return + if (typeof msg !== 'string') return + if (!isReportLog) return + log.setFilterMsg(msg) + }, + addFilterMsg(msg) { + // 从基础库2.8.1开始支持 + if (!log || !log.addFilterMsg) return + if (typeof msg !== 'string') return + if (!isReportLog) return + log.addFilterMsg(msg) + } +} diff --git a/starCloud.js b/starCloud.js index 9e39b3b..ff3e707 100644 --- a/starCloud.js +++ b/starCloud.js @@ -43,6 +43,7 @@ import { timestampToArray, uint8ArrayToString } from '@/starCloud/format' +import log from '@/starCloud/log' /** * 离线密码 @@ -139,6 +140,9 @@ import { * @property {String} userNos - 设备ID */ +// 是否上报日志 +export const isReportLog = false + // 命令ID const cmdIds = { // 获取公钥 @@ -216,12 +220,15 @@ export const useStarCloudStore = defineStore('starCloud', { * @param {String} params.clientId 客户端Id * @param {String} params.clientSecret 客户端密码 * @param {String} params.env 环境 + * @param {Boolean} params.isReportLog 是否上报日志 */ initStarCloud(params) { const { clientId, clientSecret, env } = params const appInfo = uni.getAccountInfoSync() this.envVersion = appInfo.miniProgram.envVersion + isReportLog = params.isReportLog + this.env = env || 'XHJ' this.clientId = clientId this.clientSecret = clientSecret @@ -271,25 +278,26 @@ export const useStarCloudStore = defineStore('starCloud', { lockId }) if (code === Result.Success.code) { - let lockList = getStorage('lockList') + this.lockInfo = data + let lockList = getStorage('starLockList') if (!lockList) { lockList = {} } if (lockList[accountInfo.uid]) { const index = lockList[accountInfo.uid].findIndex(item => item.lockId === lockId) if (index === -1) { - lockList[accountInfo.uid].push(data) + lockList[accountInfo.uid].push(this.lockInfo) } else { - lockList[accountInfo.uid][index] = data + this.lockInfo.token = lockList[accountInfo.uid][index].token + lockList[accountInfo.uid][index] = this.lockInfo } - setStorage('lockList', lockList) + setStorage('starLockList', lockList) } else { - lockList[accountInfo.uid] = [data] - setStorage('lockList', lockList) + lockList[accountInfo.uid] = [this.lockInfo] + setStorage('starLockList', lockList) } - this.lockInfo = data } else { - const lockList = getStorage('lockList') + const lockList = getStorage('starLockList') if (lockList[accountInfo.uid]) { const index = lockList[accountInfo.uid].findIndex(item => item.lockId === lockId) if (index !== -1) { @@ -310,18 +318,61 @@ export const useStarCloudStore = defineStore('starCloud', { */ async openDoor(params) { const { accountInfo, type } = params + + log.info( + new Result( + Result.Success.code, + { + lockName: this.lockInfo.bluetooth.bluetoothDeviceName, + lockId: this.lockInfo.lockId, + uid: accountInfo.uid, + time: new Date().getTime() + }, + `开始开门` + ) + ) + // 设置执行账号 const result = await this.login(accountInfo) if (result.code !== Result.Success.code) { return result } + log.info( + new Result( + result.code, + { + lockName: this.lockInfo.bluetooth.bluetoothDeviceName, + lockId: this.lockInfo.lockId, + uid: accountInfo.uid, + time: new Date().getTime() + }, + `登录星云账号: ${result.message}` + ) + ) + // 确认设备连接正常 - const searchResult = await searchAndConnectDevice(this.lockInfo.bluetooth.bluetoothDeviceName) - if (searchResult.code !== Result.Success.code) { - return searchResult + if (!params.connected) { + const searchResult = await searchAndConnectDevice( + this.lockInfo.bluetooth.bluetoothDeviceName + ) + log.info( + new Result( + searchResult.code, + { + lockName: this.lockInfo.bluetooth.bluetoothDeviceName, + lockId: this.lockInfo.lockId, + uid: accountInfo.uid, + time: new Date().getTime() + }, + `连接设备: ${searchResult.message}` + ) + ) + if (searchResult.code !== Result.Success.code) { + return searchResult + } + this.updateLockInfo(searchResult.data) } - this.updateLockInfo(searchResult.data) // 检查是否已添加为用户 const checkResult = await this.checkLockUser() @@ -329,6 +380,19 @@ export const useStarCloudStore = defineStore('starCloud', { return checkResult } + log.info( + new Result( + checkResult.code, + { + lockName: this.lockInfo.bluetooth.bluetoothDeviceName, + lockId: this.lockInfo.lockId, + uid: accountInfo.uid, + time: new Date().getTime() + }, + `确认是否为锁用户: ${checkResult.message}` + ) + ) + // 是否需要联网 let onlineToken = '' if (this.lockInfo.lockSetting.appUnlockOnline) { @@ -340,6 +404,19 @@ export const useStarCloudStore = defineStore('starCloud', { } } + log.info( + new Result( + checkResult.code, + { + lockName: this.lockInfo.bluetooth.bluetoothDeviceName, + lockId: this.lockInfo.lockId, + uid: accountInfo.uid, + time: new Date().getTime() + }, + `判断是否需要联网token: ${this.lockInfo.lockSetting.appUnlockOnline}` + ) + ) + // 开门方式 let openMode if (type === 'close') { @@ -396,6 +473,19 @@ export const useStarCloudStore = defineStore('starCloud', { const packageArray = createPackageEnd(headArray, cebArray) + log.info( + new Result( + Result.Success.code, + { + lockName: this.lockInfo.bluetooth.bluetoothDeviceName, + lockId: this.lockInfo.lockId, + uid: accountInfo.uid, + time: new Date().getTime() + }, + `开始写入` + ) + ) + const writeResult = await writeBLECharacteristicValue( this.lockInfo.deviceId, this.lockInfo.serviceId, @@ -407,6 +497,19 @@ export const useStarCloudStore = defineStore('starCloud', { return writeResult } + log.info( + new Result( + writeResult.code, + { + lockName: this.lockInfo.bluetooth.bluetoothDeviceName, + lockId: this.lockInfo.lockId, + uid: accountInfo.uid, + time: new Date().getTime() + }, + `写入完成:${writeResult.message}` + ) + ) + return this.getWriteResult(this.openDoor, params) }, /** @@ -442,11 +545,15 @@ export const useStarCloudStore = defineStore('starCloud', { return result } // 确认设备连接正常 - const searchResult = await searchAndConnectDevice(this.lockInfo.bluetooth.bluetoothDeviceName) - if (searchResult.code !== Result.Success.code) { - return searchResult + if (!params.connected) { + const searchResult = await searchAndConnectDevice( + this.lockInfo.bluetooth.bluetoothDeviceName + ) + if (searchResult.code !== Result.Success.code) { + return searchResult + } + this.updateLockInfo(searchResult.data) } - this.updateLockInfo(searchResult.data) // 检查是否已添加为用户 const checkResult = await this.checkLockUser() @@ -707,7 +814,7 @@ export const useStarCloudStore = defineStore('starCloud', { lockIds: params.lockIds }) if (code === Result.Success.code) { - const lockList = getStorage('lockList') + const lockList = getStorage('starLockList') if (lockList[accountInfo.uid]) { lockIds.forEach(lockId => { const index = lockList[accountInfo.uid].findIndex(item => item.lockId === lockId) @@ -715,7 +822,7 @@ export const useStarCloudStore = defineStore('starCloud', { lockList[accountInfo.uid].splice(index, 1) } }) - setStorage('lockList', lockList) + setStorage('starLockList', lockList) } } @@ -734,11 +841,15 @@ export const useStarCloudStore = defineStore('starCloud', { return result } // 确认设备连接正常 - const searchResult = await searchAndConnectDevice(this.lockInfo.bluetooth.bluetoothDeviceName) - if (searchResult.code !== Result.Success.code) { - return searchResult + if (!params.connected) { + const searchResult = await searchAndConnectDevice( + this.lockInfo.bluetooth.bluetoothDeviceName + ) + if (searchResult.code !== Result.Success.code) { + return searchResult + } + this.updateLockInfo(searchResult.data) } - this.updateLockInfo(searchResult.data) // 检查是否已添加为用户 const checkResult = await this.checkLockUser() @@ -812,11 +923,15 @@ export const useStarCloudStore = defineStore('starCloud', { } // 确认设备连接正常 - const searchResult = await searchAndConnectDevice(this.lockInfo.bluetooth.bluetoothDeviceName) - if (searchResult.code !== Result.Success.code) { - return searchResult + if (!params.connected) { + const searchResult = await searchAndConnectDevice( + this.lockInfo.bluetooth.bluetoothDeviceName + ) + if (searchResult.code !== Result.Success.code) { + return searchResult + } + this.updateLockInfo(searchResult.data) } - this.updateLockInfo(searchResult.data) // 检查是否已添加为用户 const checkResult = await this.checkLockUser() @@ -948,11 +1063,15 @@ export const useStarCloudStore = defineStore('starCloud', { } // 确认设备连接正常 - const searchResult = await searchAndConnectDevice(this.lockInfo.bluetooth.bluetoothDeviceName) - if (searchResult.code !== Result.Success.code) { - return searchResult + if (!params.connected) { + const searchResult = await searchAndConnectDevice( + this.lockInfo.bluetooth.bluetoothDeviceName + ) + if (searchResult.code !== Result.Success.code) { + return searchResult + } + this.updateLockInfo(searchResult.data) } - this.updateLockInfo(searchResult.data) // 检查是否已添加为用户 const checkResult = await this.checkLockUser() @@ -1337,15 +1456,18 @@ export const useStarCloudStore = defineStore('starCloud', { // 添加用户 async addLockUser(params) { const { params: data } = params + // 确认设备连接正常 - const searchResult = await searchAndConnectDevice( - this.lockInfo.bluetooth.bluetoothDeviceName, - data.role !== 0xff - ) - if (searchResult.code !== Result.Success.code) { - return searchResult + if (!params.connected) { + const searchResult = await searchAndConnectDevice( + this.lockInfo.bluetooth.bluetoothDeviceName, + data.role !== 0xff + ) + if (searchResult.code !== Result.Success.code) { + return searchResult + } + this.updateLockInfo(searchResult.data) } - this.updateLockInfo(searchResult.data) const { name, @@ -1451,11 +1573,22 @@ export const useStarCloudStore = defineStore('starCloud', { characteristicValueCallback = async data => { // code 6 token过期,重新获取 if (data.code === Result.NotTokenLock.code) { - resolve(await request(params)) + log.info( + new Result( + data.code, + { + lockName: this.lockInfo.bluetooth.bluetoothDeviceName, + lockId: this.lockInfo.lockId, + time: new Date().getTime() + }, + `token过期:${data.message}` + ) + ) + resolve(await request({ ...params, connected: true })) } else if (data.code === Result.NotRegisteredLock.code) { const checkResult = await this.checkLockUser(true) if (checkResult.code === Result.Success.code) { - resolve(await request(params)) + resolve(await request({ ...params, connected: true })) } else { clearTimeout(getWriteResultTimer) resolve(checkResult) @@ -1466,6 +1599,17 @@ export const useStarCloudStore = defineStore('starCloud', { await this.disconnectDevice() } console.log('写入结果', data, request, params) + log.info( + new Result( + data.code, + { + lockName: this.lockInfo.bluetooth.bluetoothDeviceName, + lockId: this.lockInfo.lockId, + time: new Date().getTime() + }, + `开门结果:${data.message}` + ) + ) resolve(data) } } @@ -1535,6 +1679,15 @@ export const useStarCloudStore = defineStore('starCloud', { ...this.lockInfo, ...lockInfo } + + const lockList = getStorage('starLockList') + const index = lockList[this.accountInfo.uid].findIndex( + item => item.lockId === this.lockInfo.lockId + ) + if (index !== -1) { + lockList[this.accountInfo.uid][index] = this.lockInfo + } + setStorage('starLockList', lockList) }, // 特征值变化回调 listenCharacteristicValue(res) { @@ -1811,7 +1964,7 @@ export const useStarCloudStore = defineStore('starCloud', { lockId: this.lockInfo.lockId }) if (code === Result.Success.code) { - const lockList = getStorage('lockList') + const lockList = getStorage('starLockList') if (lockList[this.accountInfo.uid]) { const index = lockList[this.accountInfo.uid].findIndex( item => item.lockId === this.lockInfo.lockId @@ -1819,7 +1972,7 @@ export const useStarCloudStore = defineStore('starCloud', { if (index !== -1) { lockList[this.accountInfo.uid].splice(index, 1) } - setStorage('lockList', lockList) + setStorage('starLockList', lockList) } } characteristicValueCallback(new Result(code, {}, message))