feat:增加卡片蓝牙回调后的增删改查内容

This commit is contained in:
liyi 2025-03-07 11:13:13 +08:00
parent b5605e8025
commit de6b6e127c

161
common.js
View File

@ -1,4 +1,4 @@
import { sm4 } from 'sm-crypto'
import {sm4} from 'sm-crypto'
import {
arrayToTimestamp,
convertWeekdaysToNumber,
@ -8,28 +8,35 @@ import {
timestampToArray,
uint8ArrayToString
} from './format'
import { configs } from './env'
import { cmdIds, Result, subCmdIds } from './constant'
import {configs} from './env'
import {cmdIds, Result, subCmdIds} from './constant'
import {
closeBLEConnection,
searchAndConnectDevice,
writeBLECharacteristicValue
} from './uni/basic'
import {
addCustomPasswordRequest,
changeAdminKeyboardPwdRequest,
addCustomPasswordRequest, addFingerprintRequest, addIcCardRequest,
changeAdminKeyboardPwdRequest, clearAllIcCard, deleteIcCardRequest,
deleteLockRequest,
deletePasswordRequest,
getLastRecordTimeRequest,
getLockNetTokenRequest,
getStarCloudToken,
getUserNoListRequest,
updateElectricQuantityRequest,
updateElectricQuantityRequest, updateIcCardRequest,
updateLockUserNoRequest,
updatePasswordRequest,
uploadRecordRequest
} from './api'
import { buildNumber, getStorage, setStorage, version } from './export'
import {
buildNumber,
emitRegisterCardConfirmEvent,
emitRegisterFingerprintConfirmEvent, emitRegisterFingerprintProcessEvent,
getStorage,
setStorage,
version
} from './export'
import log from './log'
/**
@ -146,15 +153,15 @@ export async function cleanLockUser() {
this.updateLockInfo(searchResult.data)
// 获取并处理锁信息
let { uid: authUid, keyId, token, bluetooth } = this.lockInfo
let { uid } = this.userInfo
let {uid: authUid, keyId, token, bluetooth} = this.lockInfo
let {uid} = this.userInfo
authUid = authUid.toString()
uid = uid.toString()
keyId = keyId.toString()
const name = bluetooth.bluetoothDeviceName
// 获取用户列表
const { code: requestCode, data: requestData } = await getUserNoListRequest({
const {code: requestCode, data: requestData} = await getUserNoListRequest({
lockId: this.lockInfo.lockId
})
console.log('获取用户列表请求结果', requestCode, requestData)
@ -222,7 +229,7 @@ export async function cleanLockUser() {
return writeResult
}
return this.getWriteResult(this.cleanLockUser, { disconnect: false })
return this.getWriteResult(this.cleanLockUser, {disconnect: false})
}
/**
@ -316,7 +323,7 @@ export async function getPublicKey() {
return writeResult
}
return this.getWriteResult(this.getPublicKey, { disconnect: false })
return this.getWriteResult(this.getPublicKey, {disconnect: false})
}
// 获取私钥
@ -376,7 +383,7 @@ export async function getCommKey() {
return writeResult
}
return this.getWriteResult(this.getCommKey, { disconnect: false })
return this.getWriteResult(this.getCommKey, {disconnect: false})
}
// 获取锁状态
@ -422,12 +429,12 @@ export async function getLockStatus() {
return writeResult
}
return this.getWriteResult(this.getLockStatus, { disconnect: false })
return this.getWriteResult(this.getLockStatus, {disconnect: false})
}
// 获取联网token
export async function getNetToken() {
const { code, data, message } = await getLockNetTokenRequest({
const {code, data, message} = await getLockNetTokenRequest({
lockId: this.lockInfo.lockId
})
return new Result(code, data, message)
@ -435,7 +442,7 @@ export async function getNetToken() {
// 添加用户
export async function addLockUser(params) {
const { params: data } = params
const {params: data} = params
// 确认设备连接正常
if (!params.connected) {
@ -566,11 +573,11 @@ export function getWriteResult(request, params) {
),
name: 'openDoor'
})
resolve(await request.call(this, { ...params, connected: true }))
resolve(await request.call(this, {...params, connected: true}))
} else if (data.code === Result.NotRegisteredLock.code) {
const checkResult = await this.checkLockUser(true)
if (checkResult.code === Result.Success.code) {
resolve(await request.call(this, { ...params, connected: true }))
resolve(await request.call(this, {...params, connected: true}))
} else {
clearTimeout(getWriteResultTimer)
resolve(checkResult)
@ -632,7 +639,7 @@ export async function checkLockUser(forceAdd = false) {
})
console.log('添加用户蓝牙结果', addUserResult)
if (addUserResult.code === Result.Success.code) {
const { code } = await updateLockUserNoRequest({
const {code} = await updateLockUserNoRequest({
keyId: this.lockInfo.keyId,
lockUserNo: this.lockInfo.lockUserNo
})
@ -641,7 +648,7 @@ export async function checkLockUser(forceAdd = false) {
}
if (addUserResult.code === Result.NotMoreKeyLock.code) {
console.log('用户达上限,开始清理用户')
const { code: cleanCode } = await this.cleanLockUser()
const {code: cleanCode} = await this.cleanLockUser()
console.log('清理用户蓝牙结果', cleanCode)
if (cleanCode === Result.Success.code) {
return await this.checkLockUser()
@ -679,6 +686,7 @@ export function updateLockInfo(lockInfo) {
export function listenCharacteristicValue(res) {
if (res.deviceId === this.lockInfo.deviceId) {
let binaryData = new Uint8Array(res.value)
if (
binaryData[0] === 0xef &&
binaryData[1] === 0x01 &&
@ -689,7 +697,9 @@ export function listenCharacteristicValue(res) {
if (this.length + 14 > binaryData.length) {
this.completeArray = binaryData
} else {
this.parsingCharacteristicValue(binaryData).then(() => {})
this.parsingCharacteristicValue(binaryData).then(() => {
})
console.log('listenCharacteristicValue binaryData', binaryData)
}
} else if (this.completeArray) {
const combinedArray = new Uint8Array(this.completeArray.length + binaryData.length)
@ -697,7 +707,8 @@ export function listenCharacteristicValue(res) {
combinedArray.set(binaryData, this.completeArray.length)
this.completeArray = combinedArray
if (this.length + 14 === this.completeArray.length) {
this.parsingCharacteristicValue(this.completeArray).then(() => {})
this.parsingCharacteristicValue(this.completeArray).then(() => {
})
this.completeArray = null
}
}
@ -750,7 +761,6 @@ export async function parsingCharacteristicValue(binaryData) {
}
} else {
const cebBinaryData = binaryData.slice(12, binaryData.length - 2)
const decrypted = sm4.decrypt(cebBinaryData, this.lockInfo.bluetooth.privateKey, {
mode: 'ecb',
output: 'array'
@ -906,7 +916,7 @@ export async function parsingCharacteristicValue(binaryData) {
}
records.push(record)
}
const { code, message } = await uploadRecordRequest({
const {code, message} = await uploadRecordRequest({
records,
lockId: this.lockInfo.lockId
})
@ -923,6 +933,101 @@ export async function parsingCharacteristicValue(binaryData) {
this.characteristicValueCallback(new Result(decrypted[2]))
}
break
case subCmdIds.registerCard:
if (decrypted[2] === Result.Success.code) {
// 锁版应答成功
switch (this.requestParams.operate) {
case 1:
// 修改
const updateResult = await updateIcCardRequest({
...this.requestParams,
cardId: this.requestParams.cardId,
cardRight: this.requestParams.isAdmin,
lockId: this.lockInfo.lockId,
cardNumber: this.requestParams.cardNo,
cardType: this.requestParams.cardType,
cardUserNo: this.requestParams.cardUserNo
})
this.characteristicValueCallback(updateResult)
await this.disconnectDevice()
break;
case 2:
// 删除
const deleteResult = await deleteIcCardRequest({
cardId: this.requestParams.cardId,
lockId: this.lockInfo.lockId,
})
this.characteristicValueCallback(deleteResult)
await this.disconnectDevice()
break;
case 3:
// 删除全部
const deleteAllResult = await clearAllIcCard({
lockId: this.lockInfo.lockId,
})
this.characteristicValueCallback(deleteAllResult)
await this.disconnectDevice()
break;
default:
this.characteristicValueCallback(new Result(decrypted[2]))
break;
}
}
break;
case subCmdIds.registerCardConfirm:
// 收到锁版回复判断操作类型进行对应api操作
if (decrypted[2] === Result.Success.code) {
switch (this.requestParams.operate) {
case 0:
// 注册
const addResult = await addIcCardRequest({
...this.requestParams,
isCoerced: this.requestParams.isForce,
cardRight: this.requestParams.isAdmin,
lockId: this.lockInfo.lockId,
cardNumber: this.requestParams.cardNo,
cardUserNo: this.requestParams.cardUserNo
})
if (addResult.code === Result.Success.code) {
// 增加返回一个卡序号
addResult.data.cardNo = decrypted[6] * 256 + decrypted[7];
}
// 触发卡片确认事件
emitRegisterCardConfirmEvent(addResult)
// 断开蓝牙连接
await this.disconnectDevice()
break;
}
}
break;
case subCmdIds.registerFingerprint:
console.log('注册指纹开始回调')
// 返回锁版应答的Status状态
this.characteristicValueCallback(new Result(decrypted[2]))
break;
case subCmdIds.registerFingerprintProcess:
console.log('注册指纹过程回调', decrypted)
emitRegisterFingerprintProcessEvent(decrypted)
break;
case subCmdIds.registerFingerprintConfirm:
if (this.requestParams.operate === 0) {
const addResult = await addFingerprintRequest({
...this.requestParams,
isCoerced: this.requestParams.isForce,
fingerRight: this.requestParams.isAdmin,
lockId: this.lockInfo.lockId,
fingerprintNumber: this.requestParams.no || this.requestParams.fingerprintNumber,
fingerprintUserNo: this.requestParams.fingerprintUserNo
})
if (addResult.code === Result.Success.code) {
console.log('注册指纹确认回调')
// 触发指纹确认事件
emitRegisterFingerprintConfirmEvent(addResult)
// 断开蓝牙连接
await this.disconnectDevice()
}
}
break;
default:
break
}
@ -938,16 +1043,17 @@ export async function parsingCharacteristicValue(binaryData) {
lockId: this.lockInfo.lockId,
electricQuantity: decrypted[7],
electricQuantityStandby: decrypted[9]
}).then(() => {})
}).then(() => {
})
}
this.characteristicValueCallback(new Result(decrypted[6], { lock: this.lockInfo }))
this.characteristicValueCallback(new Result(decrypted[6], {lock: this.lockInfo}))
break
case cmdIds.resetDevice:
this.updateLockInfo({
token: decrypted.slice(2, 6)
})
if (decrypted[6] === Result.Success.code) {
const { code, message } = await deleteLockRequest({
const {code, message} = await deleteLockRequest({
lockId: this.lockInfo.lockId
})
if (code === Result.Success.code) {
@ -1040,6 +1146,7 @@ export function createPackageHeader(encryptionType, originalLength) {
return headArray
}
// 断开与设备的连接
export async function disconnectDevice() {
return await closeBLEConnection(this.lockInfo.deviceId)