feat: 1. 完成设置相关功能

This commit is contained in:
范鹏 2024-11-25 15:31:38 +08:00
parent 6cd5311190
commit 778172cf7f
2 changed files with 150 additions and 1 deletions

18
api.js
View File

@ -107,3 +107,21 @@ export function bindLockRequest(data) {
data
})
}
// 移除坏锁
export function removeBadLockRequest(data) {
return request({
url: '/v1/lock/removeBadLock',
method: 'POST',
data
})
}
// 删除锁
export function deleteLockRequest(data) {
return request({
url: '/v1/lock/delete',
method: 'POST',
data
})
}

View File

@ -4,6 +4,7 @@ import { buildNumber, configs, version } from '@/starCloud/env'
import {
addCustomPasswordRequest,
bindLockRequest,
deleteLockRequest,
deletePasswordRequest,
getLockDetailRequest,
getLockNetTokenRequest,
@ -11,6 +12,7 @@ import {
getServerDatetimeRequest,
getStarCloudToken,
getUserNoListRequest,
removeBadLockRequest,
starCloudCreateUser,
updateLockUserNoRequest,
updatePasswordRequest
@ -527,7 +529,6 @@ export const useStarCloudStore = defineStore('starCloud', {
return this.getWriteResult(this.customPassword, params)
},
// 搜索设备
async searchDevice(callback) {
const result = await startBluetoothDevicesDiscovery()
@ -680,6 +681,111 @@ export const useStarCloudStore = defineStore('starCloud', {
}
return connectResult
},
/**
* 移除坏锁
* @param params
* @param {AccountInfo} params.accountInfo 账号信息
* @param {List[int]} params.lockIds 锁Id列表
* @returns {Promise<Result>}
*/
async removeBadLock(params) {
const { accountInfo, lockIds } = params
// 设置执行账号
const result = await this.login(accountInfo)
if (result.code !== Result.Success.code) {
return result
}
const { code, message } = await removeBadLockRequest({
lockIds: params.lockIds
})
if (code === Result.Success.code) {
const lockList = getStorage('lockList')
if (lockList[accountInfo.uid]) {
lockIds.forEach(lockId => {
const index = lockList[accountInfo.uid].findIndex(item => item.lockId === lockId)
if (index !== -1) {
lockList[accountInfo.uid].splice(index, 1)
}
})
setStorage('lockList', lockList)
}
}
return new Result(code, {}, message)
},
/**
* 删除锁
* @param params
* @param {AccountInfo} params.accountInfo 账号信息
*/
async deleteLock(params) {
const { 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
}
const {
token,
bluetooth: { publicKey, privateKey }
} = this.lockInfo
const authUid = this.lockInfo.uid.toString()
const name = this.lockInfo.bluetooth.bluetoothDeviceName
const length = 2 + 40 + 20 + 4 + 1 + 16
const headArray = this.createPackageHeader(3, length)
const contentArray = new Uint8Array(length)
contentArray[0] = cmdIds.resetDevice / 256
contentArray[1] = cmdIds.resetDevice % 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)
}
contentArray.set(token || new Uint8Array([0, 0, 0, 0]), 62)
contentArray[66] = 16
const md5Array = md5Encrypt(name, token || new Uint8Array([0, 0, 0, 0]), publicKey)
contentArray.set(md5Array, 67)
const cebArray = sm4.encrypt(contentArray, 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.deleteLock, params)
},
/**
* 清理用户
@ -1392,6 +1498,31 @@ export const useStarCloudStore = defineStore('starCloud', {
console.log('开门', decrypted[6], this.lockInfo.token)
characteristicValueCallback(new Result(decrypted[6]))
break
case cmdIds.resetDevice:
this.updateLockInfo({
token: decrypted.slice(2, 6)
})
if (decrypted[6] === Result.Success.code) {
const { code, message } = await deleteLockRequest({
lockId: this.lockInfo.lockId
})
if (code === Result.Success.code) {
const lockList = getStorage('lockList')
if (lockList[this.accountInfo.uid]) {
const index = lockList[this.accountInfo.uid].findIndex(
item => item.lockId === this.lockInfo.lockId
)
if (index !== -1) {
lockList[this.accountInfo.uid].splice(index, 1)
}
setStorage('lockList', lockList)
}
}
characteristicValueCallback(new Result(code, {}, message))
} else {
characteristicValueCallback(new Result(decrypted[6]))
}
break
default:
this.updateLockInfo({
token: decrypted.slice(2, 6)