1. 完成添加用户功能

2. 完善项目文档
3. 完成开门代码逻辑(目前回调0x09)
This commit is contained in:
peng fan 2024-08-09 19:22:58 +08:00
parent f62af579b2
commit 2f1c89ebf1
2 changed files with 336 additions and 6 deletions

View File

@ -6,9 +6,10 @@
## 使用工具
#### HBuilderX + 微信开发者工具推荐使用IntelliJ工具编写HBuilderX代码提示等功能做的较差
## 技术栈
#### Uni-app创建的Vue3项目
#### 运行只需npm i 安装依赖然后在HBuilderX中运行即可
## 技术栈 + 运行方式
#### Uni-app创建的vue3项目vue3 + pinia + uview-plus
#### 在HBuilderX中点击 文件 -> 导入 -> 从本地目录导入,选择项目目录即可导入项目
#### 运行只需执行`npm i` 安装依赖(node版本 v20.16)然后在HBuilderX中点击运行 -> 运行到微信开发者工具即可(运行前勾选运行时压缩代码)
## 目录
1. api 项目接口

View File

@ -11,6 +11,8 @@
<button @click="getPublicKey">获取锁公钥</button>
<button @click="getCommKey">获取锁私钥</button>
<button @click="getLockStatus">获取锁状态</button>
<button @click="addUser">添加用户</button>
<button @click="openDoor">开门</button>
</view>
</view>
</template>
@ -35,7 +37,15 @@ export default {
signKey: new Uint8Array(16),
showList: true,
number: 1,
lockStatus: new Uint8Array(1)
lockStatus: new Uint8Array(1),
token: new Uint8Array([0,0,0,0]),
keyId: '0',
uid: '294',
userId: '294',
// userId: '336',
password: '',
config: {},
notify: false
}
},
onLoad() {
@ -63,11 +73,314 @@ export default {
})
},
methods: {
//
addUser() {
//
let headArray = new Uint8Array(12)
//
headArray[0] = 0xEF
headArray[1] = 0x01
headArray[2] = 0xEE
headArray[3] = 0x02
//
headArray[4] = 0x01
//
headArray[5] = this.number / 256
headArray[6] = this.number % 256
this.number++
//
headArray[7] = 0x23
//
headArray[8] = 192 / 256
headArray[9] = 192 % 256
headArray[10] = 182 / 256
headArray[11] = 182 % 256
//
let paramsArray = new Uint8Array(182)
//
paramsArray[0] = 0x3001 / 256
paramsArray[1] = 0x3001 % 256
// ID
for (let i = 0; i < this.lockId.length; i++) {
paramsArray[2 + i] = this.lockId.charCodeAt(i)
}
// ID
for (let i = 0; i < this.uid.length; i++) {
paramsArray[42 + i] = this.uid.charCodeAt(i)
}
// keyId
for (let i = 0; i < this.keyId.length; i++) {
paramsArray[62 + i] = this.keyId.charCodeAt(i)
}
// userId
for (let i = 0; i < this.userId.length; i++) {
paramsArray[102 + i] = this.userId.charCodeAt(i)
}
//
paramsArray[122] = 0x01
//
paramsArray[123] = 0x01
//
const nowTime = parseInt(new Date().getTime() / 1000)
paramsArray[124] = (nowTime & 0xff000000) >> 24
paramsArray[125] = (nowTime & 0xff0000) >> 16
paramsArray[126] = (nowTime & 0xff00) >> 8
paramsArray[127] = (nowTime & 0xff)
//
const endTime = 0x11223344
paramsArray[128] = (endTime & 0xff000000) >> 24
paramsArray[129] = (endTime & 0xff0000) >> 16
paramsArray[130] = (endTime & 0xff00) >> 8
paramsArray[131] = (endTime & 0xff)
//
paramsArray[132] = 0xffff / 256
paramsArray[133] = 0xffff % 256
//
paramsArray[134] = 0
//
paramsArray[135] = 0
//
paramsArray[136] = 0
//
paramsArray[137] = 0
//
paramsArray[138] = 0
//
paramsArray[139] = 0
//
paramsArray[140] = 0xff
//
const password = this.password == '' ? (Math.floor(Math.random() * 900000) + 100000).toString() : this.password.toString()
console.log('随机密码', password)
for(let i = 0; i < password.length; i++){
paramsArray[141 + i] = password.charCodeAt(i)
}
for(let i = 0; i < this.token.length; i++){
paramsArray[161 + i] = this.token[i]
}
// MD5
paramsArray[165] = 16
const originAuthCodeLen = this.keyId.length + this.uid.length + 4 + 16
let authCodeBinaryData = new Uint8Array(originAuthCodeLen)
for(let i=0; i < this.uid.length; i++){
authCodeBinaryData[i] = this.uid.charCodeAt(i)
}
for(let i=0; i < this.keyId.length; i++){
authCodeBinaryData[this.uid.length + i] = this.keyId.charCodeAt(i)
}
const number = this.keyId.length + this.uid.length
if(this.token[0] === 0 && this.token[1] === 0 && this.token[2] === 0 && this.token[3] === 0){
authCodeBinaryData[number + 0] = (nowTime & 0xff000000) >> 24
authCodeBinaryData[number + 1] = (nowTime & 0xff0000) >> 16
authCodeBinaryData[number + 2] = (nowTime & 0xff00) >> 8
authCodeBinaryData[number + 3] = (nowTime & 0xff)
} else {
authCodeBinaryData[number + 0] = this.token[0]
authCodeBinaryData[number + 1] = this.token[1]
authCodeBinaryData[number + 2] = this.token[2]
authCodeBinaryData[number + 3] = this.token[3]
}
for(let i=0; i < 16; i++){
authCodeBinaryData[number + 4 + i] = this.publicKey[i]
}
console.log('md5前的数据', Array.from(authCodeBinaryData))
const md5Hex = md5(authCodeBinaryData)
const md5Array = new Uint8Array(md5Hex.match(/.{1,2}/g).map(byte => parseInt(byte, 16)))
console.log('md5后的数据', Array.from(md5Array))
for(let i = 0; i < md5Array.length; i++) {
paramsArray[166 + i] = md5Array[i]
}
console.log('未加密的数据', Array.from(paramsArray))
const encryptArray = sm4.encrypt(paramsArray, this.commKey, { mode: 'ecb', output: 'array' })
console.log('加密后的数据', Array.from(encryptArray))
//
let mergeBuffer = new ArrayBuffer(204)
let mergeArray = new Uint8Array(mergeBuffer)
for(let i = 0; i < 12; i++){
mergeArray[i] = headArray[i]
}
for(let i = 0; i < 192; i++){
mergeArray[i + 12] = encryptArray[i]
}
console.log('crc前的数据', Array.from(mergeArray))
let resultBuffer = new ArrayBuffer(206)
let resultArray = new Uint8Array(resultBuffer)
for(let i = 0; i < 204; i++){
resultArray[i] = mergeArray[i]
}
resultArray[204] = crc.crc16kermit(mergeBuffer) / 256
resultArray[205] = crc.crc16kermit(mergeBuffer) % 256
this.writeBLECharacteristicValue(resultBuffer)
},
reconnect() {
return new Promise(resolve => setTimeout(resolve, 2000))
},
//
async openDoor() {
const that = this
that.connect(that.config)
//
const result = await that.reconnect()
//
let headArray = new Uint8Array(12)
//
headArray[0] = 0xEF
headArray[1] = 0x01
headArray[2] = 0xEE
headArray[3] = 0x02
//
headArray[4] = 0x01
//
headArray[5] = this.number / 256
headArray[6] = this.number % 256
this.number++
//
headArray[7] = 0x23
//
headArray[8] = 112 / 256
headArray[9] = 112 % 256
headArray[10] = 104 / 256
headArray[11] = 104 % 256
//
let paramsArray = new Uint8Array(104)
//
paramsArray[0] = 0x3005 / 256
paramsArray[1] = 0x3005 % 256
// ID
for (let i = 0; i < this.lockId.length; i++) {
paramsArray[2 + i] = this.lockId.charCodeAt(i)
}
// ID
for (let i = 0; i < this.uid.length; i++) {
paramsArray[42 + i] = this.uid.charCodeAt(i)
}
//
paramsArray[62] = 0
//
const nowTime = parseInt(new Date().getTime() / 1000)
paramsArray[63] = (nowTime & 0xff000000) >> 24
paramsArray[64] = (nowTime & 0xff0000) >> 16
paramsArray[65] = (nowTime & 0xff00) >> 8
paramsArray[66] = (nowTime & 0xff)
// token
for (let i = 0; i < this.token.length; i++) {
paramsArray[67 + i] = this.token[i]
}
// authCode
paramsArray[71] = 16
const originAuthCodeLen = this.keyId.length + this.uid.length + 4 + 16
let authCodeBinaryData = new Uint8Array(originAuthCodeLen)
for (let i = 0; i < this.keyId.length; i++) {
authCodeBinaryData[i] = this.keyId.charCodeAt(i)
}
for (let i = 0; i < this.uid.length; i++) {
authCodeBinaryData[this.keyId.length + i] = this.uid.charCodeAt(i)
}
const number = this.keyId.length + this.uid.length
authCodeBinaryData[number + 0] = this.token[0]
authCodeBinaryData[number + 1] = this.token[1]
authCodeBinaryData[number + 2] = this.token[2]
authCodeBinaryData[number + 3] = this.token[3]
for (let i = 0; i < 16; i++) {
authCodeBinaryData[number + 4 + i] = this.signKey[i]
}
console.log('md5前的数据', Array.from(authCodeBinaryData))
const md5Hex = md5(authCodeBinaryData)
const md5Array = new Uint8Array(md5Hex.match(/.{1,2}/g).map(byte => parseInt(byte, 16)))
console.log('md5后的数据', Array.from(md5Array))
for (let i = 0; i < md5Array.length; i++) {
paramsArray[72 + i] = md5Array[i]
}
const onlineToken = '0'
for (let i = 0; i < onlineToken.length; i++) {
paramsArray[88 + i] = onlineToken.charCodeAt(i)
}
console.log('未加密的数据', Array.from(paramsArray))
const encryptArray = sm4.encrypt(paramsArray, this.commKey, { mode: 'ecb', output: 'array' })
console.log('加密后的数据', Array.from(encryptArray))
//
let mergeBuffer = new ArrayBuffer(124)
let mergeArray = new Uint8Array(mergeBuffer)
for (let i = 0; i < 12; i++) {
mergeArray[i] = headArray[i]
}
for (let i = 0; i < 112; i++) {
mergeArray[i + 12] = encryptArray[i]
}
console.log('crc前的数据', Array.from(mergeArray))
let resultBuffer = new ArrayBuffer(126)
let resultArray = new Uint8Array(resultBuffer)
for (let i = 0; i < 124; i++) {
resultArray[i] = mergeArray[i]
}
resultArray[124] = crc.crc16kermit(mergeBuffer) / 256
resultArray[125] = crc.crc16kermit(mergeBuffer) % 256
this.writeBLECharacteristicValue(resultBuffer)
},
//
getLockStatus() {
//
let buffer = new ArrayBuffer(12)
let binaryData = new Uint8Array(buffer)
let binaryData = new Uint8Array(12)
//
binaryData[0] = 0xEF
@ -411,6 +724,18 @@ export default {
if(decrypted[0] === 48 && decrypted[1] === 64) {
that.lockStatus = decrypted
console.log('锁状态', that.lockStatus)
} else {
that.token = decrypted.slice(42,46)
console.log('token', Array.from(that.token))
if(decrypted[0] === 48 && decrypted[1] === 1) {
if(decrypted[46] === 6) {
that.addUser()
}
} else if(decrypted[0] === 48 && decrypted[1] === 5) {
if(decrypted[46] === 6) {
that.openDoor()
}
}
}
}
},
@ -427,7 +752,9 @@ export default {
type: 'notification',
success(res) {
console.log(res)
if(that.notify) return
uni.onBLECharacteristicValueChange(function (res) {
that.notify = true
let binaryData = new Uint8Array(res.value)
console.log('设备返回的数据', Array.from(binaryData), Array.from(binaryData, byte =>
byte.toString(16)).join(','))
@ -454,8 +781,10 @@ export default {
}
})
},
//
connect(item) {
this.config = item
const that = this
if(item.pair) {
return