1. 完成普通用户的展示适配

2. 解决苹果安卓获取的设备id不同的问题
This commit is contained in:
范鹏 2024-08-29 11:30:00 +08:00
parent f9669c8d2c
commit 13f45e1c26
6 changed files with 247 additions and 22 deletions

View File

@ -37,3 +37,21 @@ export function deleteKeyRequest(data) {
data
})
}
// 更新锁用户
export function updateLockUserNoRequest(data) {
return request({
url: '/key/updateLockUserNo',
method: 'POST',
data
})
}
// 获取所有锁用户
export function getUserNoListRequest(data) {
return request({
url: '/key/getUserNoList',
method: 'POST',
data
})
}

View File

@ -7,7 +7,7 @@
"transformPx" : false,
/* */
"mp-weixin" : {
"appid" : "wx10c16cfd90808097",
"appid" : "wx9829a39e65550757",
"setting" : {
"urlCheck" : true,
"minified" : true

View File

@ -55,7 +55,7 @@ export default {
authUid: this.userInfo.uid.toString(),
uid: this.userInfo.uid.toString(),
openMode: 1,
keyType: 1,
keyType: 0,
startDate: timestamp,
expireDate: 0xffffffff,
useCountLimit: 0xffff,

View File

@ -26,7 +26,7 @@
</view>
</view>
</view>
<view class="menu">
<view class="menu" v-if="currentLockInfo.userType === 110301">
<view class="menu-title">
<image class="menu-image" src="/static/images/icon_menu.png"></image>
<view>功能</view>

View File

@ -28,7 +28,7 @@
</view>
</view>
</view>
<view class="view">
<view class="view" v-if="currentLockInfo.userType === 110301">
<view class="view-button">
<view>开锁时是否需联网</view>
<view class="view-button" style="padding: 0">
@ -48,6 +48,7 @@ import { mapActions, mapState } from 'pinia'
import { deleteLockRequest } from '@/api/lock'
import { useLockStore } from '@/stores/lock'
import { updateLockSettingRequest } from '@/api/lockSetting'
import { deleteKeyRequest } from '@/api/key'
export default {
data () {
@ -101,15 +102,49 @@ export default {
uni.showLoading({
title: '删除中'
})
console.log(that.currentLockInfo.name, that.userInfo.uid.toString(), that.keyId.toString())
const { code: resetDeviceCode } = await that.resetDevice({
name: that.currentLockInfo.name,
authUid: that.userInfo.uid.toString(),
keyId: that.keyId.toString()
})
if(resetDeviceCode === 0) {
const { code, message } = await deleteLockRequest({
lockId: that.currentLockInfo.lockId
if(that.currentLockInfo.userType === 110301) {
const { code: resetDeviceCode } = await that.resetDevice({
name: that.currentLockInfo.name,
authUid: that.userInfo.uid.toString(),
keyId: that.keyId.toString()
})
if(resetDeviceCode === 0) {
const { code, message } = await deleteLockRequest({
lockId: that.currentLockInfo.lockId
})
if(code === 0) {
uni.hideLoading()
that.getLockList({
pageNo: 1,
pageSize: 50
})
uni.navigateBack({
delta: 2,
complete: () => {
uni.showToast({
title: '删除成功',
icon: 'none'
})
}
})
uni.navigateBack()
} else {
uni.hideLoading()
uni.showToast({
title: 'message',
icon: 'none'
})
}
} else {
uni.hideLoading()
uni.showToast({
title: '删除失败',
icon: 'none'
})
}
} else {
const { code } = await deleteKeyRequest({
keyId: that.keyId
})
if(code === 0) {
uni.hideLoading()
@ -134,12 +169,6 @@ export default {
icon: 'none'
})
}
} else {
uni.hideLoading()
uni.showToast({
title: '删除失败',
icon: 'none'
})
}
}
}

View File

@ -6,6 +6,7 @@ import crc from 'crc'
import { sm4 } from 'sm-crypto'
import { md5 } from 'js-md5'
import { getServerDatetime } from '@/api/check'
import { getUserNoListRequest, updateLockUserNoRequest } from '@/api/key'
// 定时器
let timer
@ -495,7 +496,7 @@ export const useBluetoothStore = defineStore('ble', {
success (res) {
let serviceId
for(let i = 0; i < res.services.length; i++) {
if(res.services[i].isPrimary) {
if(res.services[i].uuid.indexOf('FFF0') !== -1) {
serviceId = res.services[i].uuid
}
}
@ -593,8 +594,122 @@ export const useBluetoothStore = defineStore('ble', {
}
})
},
// 查找设备并连接
async searchAndConnectDevice() {
const that = this
let timer1
let timer2
return new Promise((resolve) => {
uni.startBluetoothDevicesDiscovery({
success: function (res) {
timer2 = setTimeout(() => {
setTimeout(() => {
uni.stopBluetoothDevicesDiscovery()
clearInterval(timer)
}, 7000)
timer = setInterval(() => {
uni.getBluetoothDevices({
success(res) {
const deviceList = res.devices
for(let i = 0; i < deviceList.length; i++) {
if(deviceList[i]?.name === that.currentLockInfo.name) {
const uuid = deviceList[i]?.advertisServiceUUIDs[0]
if(uuid && uuid.slice(2,8)==='758824' && uuid.slice(30,32)==='01') {
clearInterval(timer1)
clearInterval(timer2)
uni.stopBluetoothDevicesDiscovery()
resolve(deviceList[i].deviceId)
break
}
}
}
},
fail: async function (res) {
console.log('获取设备列表失败', res)
if(res.errCode === 10000) {
// 重新初始化蓝牙适配器
await that.initBluetooth()
}
}
})
}, 1000)
}, 300)
},
fail: async function (res) {
console.log('开始搜索失败', res)
if(res.errCode === 10000) {
// 重新初始化蓝牙适配器
await that.initBluetooth()
that.searchAndConnectDevice()
} else {
resolve(false)
}
}
})
})
},
// 检查是否已添加为用户
async checkLockUser() {
console.log('检查是否已添加为用户', this.currentLockInfo.lockUserNo === 0)
if(this.currentLockInfo.lockUserNo === 0) {
const timestamp = parseInt(new Date().getTime() / 1000)
const password = (Math.floor(Math.random() * 900000) + 100000).toString()
console.log('用户未添加,开始添加用户')
const { code: addUserCode } = await this.addLockUser({
name: this.currentLockInfo.name,
keyId: this.keyId,
authUid: this.currentLockInfo.senderUserId.toString(),
uid: this.currentLockInfo.uid.toString(),
openMode: 1,
keyType: 0,
startDate: this.currentLockInfo.startDate === 0 ? timestamp : this.currentLockInfo.startDate,
expireDate: this.currentLockInfo.endDate === 0 ? 0xffffffff : this.currentLockInfo.endDate,
useCountLimit: this.currentLockInfo.keyType === 3 ? 1 : 0xffff,
isRound: 0,
weekRound: 0,
startHour: 0,
startMin: 0,
endHour: 0,
endMin: 0,
role: 0,
password
})
console.log('添加结果', addUserCode)
if(addUserCode === 0) {
const { code } = await updateLockUserNoRequest({
keyId: this.keyId,
lockUserNo: this.currentLockInfo.lockUserNo
})
return true
} else if(addUserCode === 0x0c) {
const { code: requestCode, data: requestData } = await getUserNoListRequest({
lockId: this.currentLockInfo.lockId
})
if(requestCode !== 0) return false
const userNoList = requestData.userNos
const { code: cleanCode } = await this.cleanLockUser({
name: this.currentLockInfo.name,
keyId: this.keyId,
authUid: this.currentLockInfo.senderUserId.toString(),
uid: this.currentLockInfo.uid.toString(),
userNoList: userNoList
})
if(cleanCode === 0) {
return await this.checkLockUser()
} else {
return false
}
} else if(addUserCode === 0x0f) {
return true
} else {
return false
}
} else {
return true
}
},
// 写入特征值
async writeBLECharacteristicValue(binaryData) {
async writeBLECharacteristicValue(binaryData, isCheckUser = true) {
const that = this
// 确认蓝牙状态正常
@ -606,10 +721,23 @@ export const useBluetoothStore = defineStore('ble', {
// 确认设备连接正常
if(!that.currentLockInfo.connected) {
const srerchResult = await that.searchAndConnectDevice()
that.updateCurrentLockInfo({
...that.currentLockInfo,
deviceId: srerchResult
})
console.log('设备ID', that.currentLockInfo.deviceId)
if(!srerchResult) return
const result = await that.connectBluetoothDevice()
if(!result) return
}
// 检查是否已添加为用户
if(isCheckUser) {
const checkResult = await that.checkLockUser()
if (!checkResult) return
}
console.log('设备ID', that.currentLockInfo.deviceId)
console.log('设备名称:', that.currentLockInfo.name)
console.log('设备主服务:', that.currentLockInfo.serviceId)
@ -862,7 +990,7 @@ export const useBluetoothStore = defineStore('ble', {
const cebArray = sm4.encrypt(conentArray, this.currentLockInfo.commKey, { mode: 'ecb', output: 'array' })
const packageArray = this.createPackageEnd(headArray, cebArray)
await this.writeBLECharacteristicValue(packageArray)
await this.writeBLECharacteristicValue(packageArray, false)
return this.getWriteResult(this.addLockUser, data)
},
// 获取写入结果
@ -924,6 +1052,56 @@ export const useBluetoothStore = defineStore('ble', {
return this.getWriteResult(this.openDoor, data)
},
// 清理用户
async cleanLockUser(data) {
const { name, authUid, keyId, uid, userNoList } = data
const length = 2 + 40 + 20 + 40 + 20 + 2 + userNoList.length + 4 + 1 + 16
const headArray = this.createPackageHeader(3, length)
const conentArray = new Uint8Array(length)
conentArray[0] = cmdIds.cleanUser / 256
conentArray[1] = cmdIds.cleanUser % 256
for (let i = 0; i < name.length; i++) {
conentArray[i + 2] = name.charCodeAt(i)
}
for (let i = 0; i < authUid.length; i++) {
conentArray[i + 42] = authUid.charCodeAt(i)
}
for (let i = 0; i < keyId.length; i++) {
conentArray[i + 62] = keyId.charCodeAt(i)
}
for (let i = 0; i < uid.length; i++) {
conentArray[i + 102] = uid.charCodeAt(i)
}
conentArray[122] = userNoList.length / 256
conentArray[123] = userNoList.length % 256
for (let i = 0; i < userNoList.length; i++) {
conentArray[i + 124] = userNoList[i]
}
conentArray.set(this.currentLockInfo.token || new Uint8Array([0, 0, 0, 0]), 124 + userNoList.length)
conentArray[128 + userNoList.length] = 16
const md5Array = this.md5Encrypte(authUid + keyId, this.currentLockInfo.token || new Uint8Array([0, 0, 0, 0]), this.currentLockInfo.publicKey)
conentArray.set(md5Array, 129 + userNoList.length)
const cebArray = sm4.encrypt(conentArray, this.currentLockInfo.commKey, { mode: 'ecb', output: 'array' })
const packageArray = this.createPackageEnd(headArray, cebArray)
await this.writeBLECharacteristicValue(packageArray, false)
return this.getWriteResult(this.cleanLockUser, data)
},
// 恢复出厂设置
async resetDevice(data) {
const { name, authUid, keyId } = data