From 455687486a36ac1bc51499324a74efca9dab609d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8C=83=E9=B9=8F?= Date: Tue, 26 Nov 2024 10:16:28 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=8C=E6=88=90=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E5=91=98=E5=AF=86=E7=A0=81=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api.js | 9 ++++ starCloud.js | 121 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+) diff --git a/api.js b/api.js index c9a1454..0c262d3 100644 --- a/api.js +++ b/api.js @@ -134,3 +134,12 @@ export function updateElectricQuantityRequest(data) { data }) } + +// 修改管理员密码 +export function changeAdminKeyboardPwdRequest(data) { + return request({ + url: '/v1/lock/changeAdminKeyboardPwd', + method: 'POST', + data + }) +} diff --git a/starCloud.js b/starCloud.js index 21fd1fb..4eed516 100644 --- a/starCloud.js +++ b/starCloud.js @@ -4,6 +4,7 @@ import { buildNumber, configs, version } from '@/starCloud/env' import { addCustomPasswordRequest, bindLockRequest, + changeAdminKeyboardPwdRequest, deleteLockRequest, deletePasswordRequest, getLockDetailRequest, @@ -158,6 +159,8 @@ const cmdIds = { // 子命令ID const subCmdIds = { + // 修改管理员密码 + updateAdminPassword: 2, // 设置开锁密码 setLockPassword: 3, // 重置开锁密码 @@ -787,6 +790,110 @@ export const useStarCloudStore = defineStore('starCloud', { return this.getWriteResult(this.deleteLock, params) }, + /** + * 修改管理员密码 + * @param params + * @param {AccountInfo} params.accountInfo 账号信息 + * @param {String} params.adminPwd 管理员密码 + * @param {Boolean} params.disconnect 操作后是否断开连接 + * @returns {Promise} + */ + async updateAdminPassword(params) { + const { adminPwd, accountInfo } = params + + // 设置执行账号 + const result = await this.login(accountInfo) + if (result.code !== Result.Success.code) { + return result + } + + // 确认设备连接正常 + const searchResult = await searchAndConnectDevice(this.lockInfo.bluetooth.bluetoothDeviceName) + if (searchResult.code !== Result.Success.code) { + return searchResult + } + this.updateLockInfo(searchResult.data) + + // 检查是否已添加为用户 + const checkResult = await this.checkLockUser() + if (checkResult.code !== Result.Success.code) { + return checkResult + } + + requestParams = params + + const uid = this.lockInfo.uid.toString() + const keyId = this.lockInfo.keyId.toString() + const pwdNo = 1 + const userCountLimit = 0xff + const startDate = Math.floor(this.lockInfo.startDate / 1000) + const endDate = Math.floor(this.lockInfo.endDate / 1000) + + const length = 2 + 1 + 1 + 40 + 20 + 2 + 20 + 2 + 4 + 4 + 4 + 1 + 16 + const headArray = this.createPackageHeader(3, length) + + const contentArray = new Uint8Array(length) + contentArray[0] = cmdIds.expandCmd / 256 + contentArray[1] = cmdIds.expandCmd % 256 + + // 子命令 + contentArray[2] = subCmdIds.updateAdminPassword + + contentArray[3] = length - 3 + + for (let i = 0; i < keyId.length; i++) { + contentArray[i + 4] = keyId.charCodeAt(i) + } + + for (let i = 0; i < uid.length; i++) { + contentArray[i + 44] = uid.charCodeAt(i) + } + + contentArray[64] = pwdNo / 256 + contentArray[65] = pwdNo % 256 + + for (let i = 0; i < adminPwd.length; i++) { + contentArray[i + 66] = adminPwd.charCodeAt(i) + } + + contentArray[86] = userCountLimit / 256 + contentArray[87] = userCountLimit % 256 + + contentArray.set(this.lockInfo.token || new Uint8Array([0, 0, 0, 0]), 88) + + contentArray.set(timestampToArray(startDate), 92) + contentArray.set(timestampToArray(endDate), 96) + + contentArray[100] = 16 + + const md5Array = md5Encrypt( + keyId + uid, + this.lockInfo.token || new Uint8Array([0, 0, 0, 0]), + this.lockInfo.bluetooth.signKey + ) + + contentArray.set(md5Array, 101) + + const cebArray = sm4.encrypt(contentArray, this.lockInfo.bluetooth.privateKey, { + mode: 'ecb', + output: 'array' + }) + + const packageArray = createPackageEnd(headArray, cebArray) + + const writeResult = await writeBLECharacteristicValue( + this.lockInfo.deviceId, + this.lockInfo.serviceId, + this.lockInfo.writeCharacteristicId, + packageArray + ) + + if (writeResult.code !== Result.Success.code) { + return writeResult + } + + return this.getWriteResult(this.updateAdminPassword, params) + }, /** * 清理用户 @@ -1428,6 +1535,20 @@ export const useStarCloudStore = defineStore('starCloud', { case cmdIds.expandCmd: const subCmdId = decrypted[3] switch (subCmdId) { + case subCmdIds.updateAdminPassword: + this.updateLockInfo({ + token: decrypted.slice(5, 9) + }) + if (decrypted[2] === Result.Success.code) { + const result = await changeAdminKeyboardPwdRequest({ + password: requestParams.adminPwd, + lockId: this.lockInfo.lockId + }) + return characteristicValueCallback(new Result(result.code)) + } else { + characteristicValueCallback(new Result(decrypted[2])) + } + break case subCmdIds.resetLockPassword: this.updateLockInfo({ token: decrypted.slice(5, 9)