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 { import {
arrayToTimestamp, arrayToTimestamp,
convertWeekdaysToNumber, convertWeekdaysToNumber,
@ -8,28 +8,35 @@ import {
timestampToArray, timestampToArray,
uint8ArrayToString uint8ArrayToString
} from './format' } from './format'
import { configs } from './env' import {configs} from './env'
import { cmdIds, Result, subCmdIds } from './constant' import {cmdIds, Result, subCmdIds} from './constant'
import { import {
closeBLEConnection, closeBLEConnection,
searchAndConnectDevice, searchAndConnectDevice,
writeBLECharacteristicValue writeBLECharacteristicValue
} from './uni/basic' } from './uni/basic'
import { import {
addCustomPasswordRequest, addCustomPasswordRequest, addFingerprintRequest, addIcCardRequest,
changeAdminKeyboardPwdRequest, changeAdminKeyboardPwdRequest, clearAllIcCard, deleteIcCardRequest,
deleteLockRequest, deleteLockRequest,
deletePasswordRequest, deletePasswordRequest,
getLastRecordTimeRequest, getLastRecordTimeRequest,
getLockNetTokenRequest, getLockNetTokenRequest,
getStarCloudToken, getStarCloudToken,
getUserNoListRequest, getUserNoListRequest,
updateElectricQuantityRequest, updateElectricQuantityRequest, updateIcCardRequest,
updateLockUserNoRequest, updateLockUserNoRequest,
updatePasswordRequest, updatePasswordRequest,
uploadRecordRequest uploadRecordRequest
} from './api' } from './api'
import { buildNumber, getStorage, setStorage, version } from './export' import {
buildNumber,
emitRegisterCardConfirmEvent,
emitRegisterFingerprintConfirmEvent, emitRegisterFingerprintProcessEvent,
getStorage,
setStorage,
version
} from './export'
import log from './log' import log from './log'
/** /**
@ -146,15 +153,15 @@ export async function cleanLockUser() {
this.updateLockInfo(searchResult.data) this.updateLockInfo(searchResult.data)
// 获取并处理锁信息 // 获取并处理锁信息
let { uid: authUid, keyId, token, bluetooth } = this.lockInfo let {uid: authUid, keyId, token, bluetooth} = this.lockInfo
let { uid } = this.userInfo let {uid} = this.userInfo
authUid = authUid.toString() authUid = authUid.toString()
uid = uid.toString() uid = uid.toString()
keyId = keyId.toString() keyId = keyId.toString()
const name = bluetooth.bluetoothDeviceName const name = bluetooth.bluetoothDeviceName
// 获取用户列表 // 获取用户列表
const { code: requestCode, data: requestData } = await getUserNoListRequest({ const {code: requestCode, data: requestData} = await getUserNoListRequest({
lockId: this.lockInfo.lockId lockId: this.lockInfo.lockId
}) })
console.log('获取用户列表请求结果', requestCode, requestData) console.log('获取用户列表请求结果', requestCode, requestData)
@ -222,7 +229,7 @@ export async function cleanLockUser() {
return writeResult 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 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 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 writeResult
} }
return this.getWriteResult(this.getLockStatus, { disconnect: false }) return this.getWriteResult(this.getLockStatus, {disconnect: false})
} }
// 获取联网token // 获取联网token
export async function getNetToken() { export async function getNetToken() {
const { code, data, message } = await getLockNetTokenRequest({ const {code, data, message} = await getLockNetTokenRequest({
lockId: this.lockInfo.lockId lockId: this.lockInfo.lockId
}) })
return new Result(code, data, message) return new Result(code, data, message)
@ -435,7 +442,7 @@ export async function getNetToken() {
// 添加用户 // 添加用户
export async function addLockUser(params) { export async function addLockUser(params) {
const { params: data } = params const {params: data} = params
// 确认设备连接正常 // 确认设备连接正常
if (!params.connected) { if (!params.connected) {
@ -566,11 +573,11 @@ export function getWriteResult(request, params) {
), ),
name: 'openDoor' 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) { } else if (data.code === Result.NotRegisteredLock.code) {
const checkResult = await this.checkLockUser(true) const checkResult = await this.checkLockUser(true)
if (checkResult.code === Result.Success.code) { if (checkResult.code === Result.Success.code) {
resolve(await request.call(this, { ...params, connected: true })) resolve(await request.call(this, {...params, connected: true}))
} else { } else {
clearTimeout(getWriteResultTimer) clearTimeout(getWriteResultTimer)
resolve(checkResult) resolve(checkResult)
@ -632,7 +639,7 @@ export async function checkLockUser(forceAdd = false) {
}) })
console.log('添加用户蓝牙结果', addUserResult) console.log('添加用户蓝牙结果', addUserResult)
if (addUserResult.code === Result.Success.code) { if (addUserResult.code === Result.Success.code) {
const { code } = await updateLockUserNoRequest({ const {code} = await updateLockUserNoRequest({
keyId: this.lockInfo.keyId, keyId: this.lockInfo.keyId,
lockUserNo: this.lockInfo.lockUserNo lockUserNo: this.lockInfo.lockUserNo
}) })
@ -641,7 +648,7 @@ export async function checkLockUser(forceAdd = false) {
} }
if (addUserResult.code === Result.NotMoreKeyLock.code) { if (addUserResult.code === Result.NotMoreKeyLock.code) {
console.log('用户达上限,开始清理用户') console.log('用户达上限,开始清理用户')
const { code: cleanCode } = await this.cleanLockUser() const {code: cleanCode} = await this.cleanLockUser()
console.log('清理用户蓝牙结果', cleanCode) console.log('清理用户蓝牙结果', cleanCode)
if (cleanCode === Result.Success.code) { if (cleanCode === Result.Success.code) {
return await this.checkLockUser() return await this.checkLockUser()
@ -679,6 +686,7 @@ export function updateLockInfo(lockInfo) {
export function listenCharacteristicValue(res) { export function listenCharacteristicValue(res) {
if (res.deviceId === this.lockInfo.deviceId) { if (res.deviceId === this.lockInfo.deviceId) {
let binaryData = new Uint8Array(res.value) let binaryData = new Uint8Array(res.value)
if ( if (
binaryData[0] === 0xef && binaryData[0] === 0xef &&
binaryData[1] === 0x01 && binaryData[1] === 0x01 &&
@ -689,7 +697,9 @@ export function listenCharacteristicValue(res) {
if (this.length + 14 > binaryData.length) { if (this.length + 14 > binaryData.length) {
this.completeArray = binaryData this.completeArray = binaryData
} else { } else {
this.parsingCharacteristicValue(binaryData).then(() => {}) this.parsingCharacteristicValue(binaryData).then(() => {
})
console.log('listenCharacteristicValue binaryData', binaryData)
} }
} else if (this.completeArray) { } else if (this.completeArray) {
const combinedArray = new Uint8Array(this.completeArray.length + binaryData.length) const combinedArray = new Uint8Array(this.completeArray.length + binaryData.length)
@ -697,7 +707,8 @@ export function listenCharacteristicValue(res) {
combinedArray.set(binaryData, this.completeArray.length) combinedArray.set(binaryData, this.completeArray.length)
this.completeArray = combinedArray this.completeArray = combinedArray
if (this.length + 14 === this.completeArray.length) { if (this.length + 14 === this.completeArray.length) {
this.parsingCharacteristicValue(this.completeArray).then(() => {}) this.parsingCharacteristicValue(this.completeArray).then(() => {
})
this.completeArray = null this.completeArray = null
} }
} }
@ -750,7 +761,6 @@ export async function parsingCharacteristicValue(binaryData) {
} }
} else { } else {
const cebBinaryData = binaryData.slice(12, binaryData.length - 2) const cebBinaryData = binaryData.slice(12, binaryData.length - 2)
const decrypted = sm4.decrypt(cebBinaryData, this.lockInfo.bluetooth.privateKey, { const decrypted = sm4.decrypt(cebBinaryData, this.lockInfo.bluetooth.privateKey, {
mode: 'ecb', mode: 'ecb',
output: 'array' output: 'array'
@ -906,7 +916,7 @@ export async function parsingCharacteristicValue(binaryData) {
} }
records.push(record) records.push(record)
} }
const { code, message } = await uploadRecordRequest({ const {code, message} = await uploadRecordRequest({
records, records,
lockId: this.lockInfo.lockId lockId: this.lockInfo.lockId
}) })
@ -923,6 +933,101 @@ export async function parsingCharacteristicValue(binaryData) {
this.characteristicValueCallback(new Result(decrypted[2])) this.characteristicValueCallback(new Result(decrypted[2]))
} }
break 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: default:
break break
} }
@ -938,16 +1043,17 @@ export async function parsingCharacteristicValue(binaryData) {
lockId: this.lockInfo.lockId, lockId: this.lockInfo.lockId,
electricQuantity: decrypted[7], electricQuantity: decrypted[7],
electricQuantityStandby: decrypted[9] electricQuantityStandby: decrypted[9]
}).then(() => {}) }).then(() => {
})
} }
this.characteristicValueCallback(new Result(decrypted[6], { lock: this.lockInfo })) this.characteristicValueCallback(new Result(decrypted[6], {lock: this.lockInfo}))
break break
case cmdIds.resetDevice: case cmdIds.resetDevice:
this.updateLockInfo({ this.updateLockInfo({
token: decrypted.slice(2, 6) token: decrypted.slice(2, 6)
}) })
if (decrypted[6] === Result.Success.code) { if (decrypted[6] === Result.Success.code) {
const { code, message } = await deleteLockRequest({ const {code, message} = await deleteLockRequest({
lockId: this.lockInfo.lockId lockId: this.lockInfo.lockId
}) })
if (code === Result.Success.code) { if (code === Result.Success.code) {
@ -1040,6 +1146,7 @@ export function createPackageHeader(encryptionType, originalLength) {
return headArray return headArray
} }
// 断开与设备的连接 // 断开与设备的连接
export async function disconnectDevice() { export async function disconnectDevice() {
return await closeBLEConnection(this.lockInfo.deviceId) return await closeBLEConnection(this.lockInfo.deviceId)