feat: 添加绑定锁方法

This commit is contained in:
范鹏 2025-04-08 18:12:52 +08:00
parent a7a1148f2c
commit e9efe4dfab
2 changed files with 120 additions and 4 deletions

View File

@ -9,7 +9,7 @@
placeholder-class="input-placeholder"
@input="updateName"
/>
<view class="button" @click="bindLock">确定</view>
<view class="button" @click="bindLockOperation">确定</view>
</view>
</template>
@ -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(),

View File

@ -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()