wx-starlock/pages/index/index.vue

131 lines
4.3 KiB
Vue
Raw Normal View History

2024-08-06 09:40:20 +08:00
<template>
<view class="content">
<view v-if="!showConnect">
<button class="button" v-if="showAdd" @click="getList">添加设备</button>
<button class="button" v-else @click="stopGetList">停止搜索</button>
<view class="device-list" v-for="(item, index) in deviceList" :key="item.deviceId" @click="connect(item)">{{item.name}}</view>
</view>
<view v-else>
<button class="button" @click="bindUser">添加用户</button>
<button class="button">开门</button>
<button class="button">关门</button>
<button class="button" @click="getLockStatusResult">获取锁状态</button>
<view>名称{{currentLockInfo.name}}</view>
<view>设备Id{{currentLockInfo.deviceId}}</view>
<view v-if="currentLockInfo.serviceId">服务{{currentLockInfo.serviceId}}</view>
<view v-if="currentLockInfo.writeCharacteristicId">写入特征值Id{{currentLockInfo.writeCharacteristicId}}</view>
<view v-if="currentLockInfo.notifyCharacteristicId">监听特征值Id{{currentLockInfo.notifyCharacteristicId}}</view>
</view>
2024-08-06 09:40:20 +08:00
</view>
</template>
<script>
import { useBluetoothStore } from '@/stores/bluetooth'
2024-08-06 10:20:56 +08:00
import { mapState, mapActions } from 'pinia'
2024-08-06 09:40:20 +08:00
export default {
data() {
return {
showAdd: true,
showConnect: false,
keyId: '0',
authUid: '294'
}
2024-08-06 09:40:20 +08:00
},
2024-08-06 10:20:56 +08:00
computed: {
...mapState(useBluetoothStore, ['deviceList', 'currentLockInfo'])
2024-08-06 12:11:38 +08:00
},
onLoad () {},
2024-08-06 09:40:20 +08:00
methods: {
...mapActions(useBluetoothStore, ['getBluetoothDevices', 'stopGetBluetoothDevices', 'updateCurrentLockInfo',
'connectBluetoothDevice', 'getPublicKey', 'getCommKey', 'getLockStatus', 'addLockUser']),
getList() {
this.getBluetoothDevices()
this.showAdd = false
},
stopGetList() {
this.stopGetBluetoothDevices()
this.showAdd = true
},
async connect(item) {
this.updateCurrentLockInfo({
name: item.name,
deviceId: item.deviceId
})
const result = await this.connectBluetoothDevice()
if(result) {
this.showConnect = true
this.stopGetBluetoothDevices()
}
},
async bindUser() {
const { code } = await this.getPublicKey(this.currentLockInfo.name)
console.log('获取公钥返回', code)
if(code !== 0) {
return
}
const { code: getCommKeyCode } = await this.getCommKey(this.currentLockInfo.name, this.keyId, this.authUid,
parseInt(new Date().getTime() / 1000), this.currentLockInfo.publicKey)
console.log('获取私钥返回', getCommKeyCode)
if(getCommKeyCode !== 0) {
return
}
// this.addLockUser({
// name: this.currentLockInfo.name,
// keyId: this.keyId,
// authUid: this.authUid,
// uid: this.authUid,
// publicKey: this.currentLockInfo.publicKey,
// openMode: 1,
// keyType: 1,
// startDate: parseInt(new Date().getTime() / 1000),
// expireDate: 0xffffffff,
// useCountLimit: 0xffff,
// isRound: 0,
// weekRound: 0,
// startHour: 0,
// startMin: 0,
// endHour: 0,
// endMin: 0,
// role: 0xff,
// token: this.currentLockInfo.token || new Uint8Array([0,0,0,0]),
// password: (Math.floor(Math.random() * 900000) + 100000).toString()
// })
},
getLockStatusResult() {
const timnestamp = parseInt(new Date().getTime() / 1000)
this.getLockStatus(this.currentLockInfo.name, this.authUid,timnestamp, timnestamp,
this.currentLockInfo.commKey)
2024-08-06 10:20:56 +08:00
}
2024-08-06 09:40:20 +08:00
}
}
</script>
<style lang="scss" scoped>
2024-08-06 09:40:20 +08:00
.content {
padding-top: 160rpx;
2024-08-06 09:40:20 +08:00
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.button {
margin-top: 40rpx;
width: 400rpx;
height: 108rpx;
line-height: 108rpx;
background: #3F536E;
color: #ffffff !important;
}
.device-list {
margin-top: 10rpx;
height: 100rpx;
width: 500rpx;
line-height: 100rpx;
text-align: center;
border: #dd524d solid 2rpx;
2024-08-06 10:20:56 +08:00
}
</style>