From e9efe4dfab517dae1521f0ae997a1b0e7f004e3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8C=83=E9=B9=8F?= Date: Tue, 8 Apr 2025 18:12:52 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E7=BB=91=E5=AE=9A?= =?UTF-8?q?=E9=94=81=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/addDevice/bindLock.vue | 8 +-- stores/bluetooth.js | 116 +++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+), 4 deletions(-) diff --git a/pages/addDevice/bindLock.vue b/pages/addDevice/bindLock.vue index 089cd87..6a5a722 100644 --- a/pages/addDevice/bindLock.vue +++ b/pages/addDevice/bindLock.vue @@ -9,7 +9,7 @@ placeholder-class="input-placeholder" @input="updateName" /> - 确定 + 确定 @@ -39,7 +39,7 @@ }, methods: { ...mapActions(useBluetoothStore, [ - 'addLockUser', + 'bindLock', 'closeBluetoothConnection', 'updateBindedDeviceName', 'closeAllBluetooth', @@ -50,7 +50,7 @@ updateName(data) { this.name = data.detail.value }, - async bindLock() { + async bindLockOperation() { if (this.name === '') { uni.showToast({ title: '请输入名称', @@ -69,7 +69,7 @@ this.updateBindedDeviceName(this.currentLockInfo.name) const timestamp = parseInt(new Date().getTime() / 1000, 10) const password = (Math.floor(Math.random() * 900000) + 100000).toString() - const { code: addUserCode } = await this.addLockUser({ + const { code: addUserCode } = await this.bindLock({ name: this.currentLockInfo.name, keyId: this.keyId, authUid: this.userInfo.uid.toString(), diff --git a/stores/bluetooth.js b/stores/bluetooth.js index 1302e11..71a91a2 100644 --- a/stores/bluetooth.js +++ b/stores/bluetooth.js @@ -1841,6 +1841,122 @@ export const useBluetoothStore = defineStore('ble', { await this.writeBLECharacteristicValue(packageArray, false) return this.getWriteResult(this.addLockUser, data) }, + // 添加用户 + async bindLock(data) { + // 确认蓝牙状态正常 + if (this.bluetoothStatus !== 0) { + console.log('写入未执行', this.bluetoothStatus) + this.getBluetoothStatus() + return { + code: -1 + } + } + + // 确认设备连接正常 + if (!this.currentLockInfo.connected) { + const searchResult = await this.searchAndConnectDevice() + if (searchResult.code !== 0) { + return searchResult + } + this.updateCurrentLockInfo({ + ...this.currentLockInfo, + deviceId: searchResult.data.deviceId + }) + console.log('设备ID:', this.currentLockInfo.deviceId) + const result = await this.connectBluetoothDevice() + console.log('连接结果', result) + if (!result) { + return { + code: -1 + } + } + } + + const { + name, + authUid, + uid, + keyId, + openMode, + keyType, + startDate, + expireDate, + useCountLimit, + isRound, + weekRound, + startHour, + startMin, + endHour, + endMin, + role, + password + } = data + const length = + 2 + 40 + 20 + 40 + 20 + 1 + 1 + 4 + 4 + 2 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 20 + 4 + 1 + 16 + const headArray = this.createPackageHeader(3, length) + const contentArray = new Uint8Array(length) + + contentArray[0] = cmdIds.addUser / 256 + contentArray[1] = cmdIds.addUser % 256 + + for (let i = 0; i < name.length; i++) { + contentArray[i + 2] = name.charCodeAt(i) + } + + for (let i = 0; i < authUid.length; i++) { + contentArray[i + 42] = authUid.charCodeAt(i) + } + + for (let i = 0; i < keyId.length; i++) { + contentArray[i + 62] = keyId.charCodeAt(i) + } + + for (let i = 0; i < uid.length; i++) { + contentArray[i + 102] = uid.charCodeAt(i) + } + + contentArray[122] = openMode + contentArray[123] = keyType + + contentArray.set(this.timestampToArray(startDate), 124) + contentArray.set(this.timestampToArray(expireDate), 128) + + contentArray[132] = useCountLimit / 256 + contentArray[133] = useCountLimit % 256 + + contentArray[134] = isRound + contentArray[135] = weekRound + contentArray[136] = startHour + contentArray[137] = startMin + contentArray[138] = endHour + contentArray[139] = endMin + contentArray[140] = role + + for (let i = 0; i < password.length; i++) { + contentArray[i + 141] = password.charCodeAt(i) + } + + contentArray.set(this.currentLockInfo.token || this.timestampToArray(startDate), 161) + + contentArray[165] = 16 + + const md5Array = this.md5Encrypte( + authUid + keyId, + this.currentLockInfo.token || this.timestampToArray(startDate), + this.currentLockInfo.publicKey + ) + + contentArray.set(md5Array, 166) + + const cebArray = sm4.encrypt(contentArray, this.currentLockInfo.commKey, { + mode: 'ecb', + output: 'array' + }) + + const packageArray = this.createPackageEnd(headArray, cebArray) + await this.writeBLECharacteristicValue(packageArray, false) + return this.getWriteResult(this.bindLock, data) + }, // 获取写入结果 getWriteResult(request, params) { const $user = useUserStore()