wx-starlock/pages/keyList/keyList.vue
2024-09-02 17:18:14 +08:00

314 lines
8.4 KiB
Vue

<template>
<view>
<scroll-view v-if="deviceInfo" scroll-y="true" :style="{height: deviceInfo.screenHeight - deviceInfo.safeArea.top + 'px'}" lower-threshold="100"
@refresherrefresh="refresherList" :refresher-enabled="true" @scrolltolower="nextPage"
:refresher-triggered="refresherTriggered">
<view class="search">
<up-search shape="square" :searchIconSize="48" :inputStyle="{ fontSize: '32rpx' }" :height="80" placeholder="搜索"
:clearabled="false" @change="changeSearch"
v-model="keySearch.searchStr" bgColor="#ffffff" :showAction="false" maxlength="20"></up-search>
</view>
<view style="padding: 32rpx 0 calc(env(safe-area-inset-bottom) + 250rpx) 0">
<view v-if="keyList.length === 0 && requestFinished">
<image class="empty-list" src="/static/images/background_empty_list.png" mode="aspectFill"></image>
<view class="empty-list-text">暂无数据</view>
</view>
<view v-else>
<up-swipe-action>
<up-swipe-action-item ref="swipeItem" :options="options" v-for="(key, index) in keyList"
:key="key.keyboardPwdId" :threshold="50" @click="deleteKey(key)">
<view class="key" @click="toKeyDetail(key)">
<image class="key-left" :src="key.headUrl" mode="aspectFill"></image>
<view class="key-right">
<view style="display: flex; align-items: center">
<view class="key-right-top">{{ key.keyName }}</view>
<view class="key-status" :style="{ color: (key.keyStatus === 110401) ? '#63b8af' : '#df282d' }">
{{ getKeyStatus(key.keyStatus) }}
</view>
</view>
<view class="key-right-bottom">{{ key.timeText }}</view>
</view>
</view>
<view class="line"></view>
</up-swipe-action-item>
</up-swipe-action>
</view>
</view>
</scroll-view>
<view class="button">
<view class="button-reset" @click="resetKey">重置钥匙</view>
<view class="button-create" @click="toCreateKey">发送钥匙</view>
</view>
</view>
</template>
<script>
import { useBasicStore } from '@/stores/basic'
import { mapActions, mapState } from 'pinia'
import { useBluetoothStore } from '@/stores/bluetooth'
import { useLockStore } from '@/stores/lock'
import { useUserStore } from '@/stores/user'
import { deletePsaawordRequest, resetPsaawordListRequest } from '@/api/keyboardPwd'
import { deleteKeyRequest, resetKeyRequest } from '@/api/key'
export default {
data () {
return {
deviceInfo: null,
refresherTriggered: false,
requestFinished: false,
options: [{
text: '删除',
style: {
backgroundColor: '#f56c6c'
}
}]
}
},
computed: {
...mapState(useUserStore, ['userInfo']),
...mapState(useBluetoothStore, ['currentLockInfo', 'keyId']),
...mapState(useLockStore, ['keyTotal', 'keyList', 'keySearch']),
},
async onLoad() {
uni.showLoading({
title: '加载中',
mask: true
})
this.deviceInfo = await this.getDeviceInfo()
this.updateKeySearch({
...this.keySearch,
lockId: this.currentLockInfo.lockId
})
const { code, meesage } = await this.getKeyList(this.keySearch)
this.requestFinished = true
uni.hideLoading()
},
methods: {
...mapActions(useBasicStore, ['routeJump', 'getDeviceInfo']),
...mapActions(useLockStore, ['getKeyList', 'updateCurrentKeyInfo', 'updateKeySearch', 'getKeyStatus']),
toKeyDetail(key) {
this.updateCurrentKeyInfo(key)
this.routeJump({
name: 'keyDetail'
})
},
async deleteKey(data) {
const key = data
const that = this
let index = this.keyList.findIndex(item => item.keyId === key.keyId)
that.$refs.swipeItem[index].closeHandler()
uni.showModal({
title: '提示',
content: '确定要删除该钥匙',
async success(res) {
if(res.confirm) {
uni.showLoading({
title: '删除中',
mask: true
})
const { code: requestCode, message } = await deleteKeyRequest({
keyId: key.keyId
})
if(requestCode === 0) {
uni.hideLoading()
uni.showToast({
title: '删除成功',
icon: 'none'
})
that.updateKeySearch({
...that.keySearch,
pageNo: 1
})
await that.getKeyList(that.keySearch)
} else {
uni.hideLoading()
uni.showToast({
title: message,
icon: 'none'
})
}
}
}
})
},
async resetKey() {
const that = this
uni.showModal({
title: '提示',
content: '确定要重置钥匙,该锁的所有钥匙都将被删除',
async success(res) {
if(res.confirm) {
const { code: requestCode, message } = await resetKeyRequest({
lockId: that.currentLockInfo.lockId,
})
console.log('重置钥匙返回', requestCode, message)
if(requestCode === 0) {
uni.showToast({
title: '重置钥匙成功',
icon: 'none'
})
that.updateKeySearch({
...that.keySearch,
pageNo: 1
})
await that.getKeyList(that.keySearch)
} else {
uni.showToast({
title: message,
icon: 'none'
})
}
}
}
})
},
toCreateKey() {
this.routeJump({
name: 'createKey'
})
},
async refresherList() {
this.refresherTriggered = true
this.updateKeySearch({
...this.keySearch,
pageNo: 1
})
const { code, meesage } = await this.getKeyList(this.keySearch)
if(code === 0) {
uni.showToast({
title: '刷新成功',
icon: 'none'
})
}
this.refresherTriggered = false
},
async nextPage() {
if(this.keyTotal <= this.keySearch.pageNo * this.keySearch.pageSize) {
return
}
const pageNo = this.keySearch.pageNo + 1
const params = {
...this.keySearch,
pageNo
}
const { code, meesage } = await this.getKeyList(params)
if(code === 0) {
this.updateKeySearch({
...this.keySearch,
pageNo
})
}
},
async changeSearch(data) {
this.updateKeySearch({
...this.keySearch,
searchStr: data
})
const { code, meesage } = await this.getKeyList(this.keySearch)
},
},
}
</script>
<style lang="scss">
page {
background-color: $uni-bg-color-grey;
}
</style>
<style lang="scss" scoped>
.search {
margin-top: 32rpx;
width: 686rpx !important;
margin-left: 32rpx;
}
.button {
display: flex;
align-items: center;
position: fixed;
bottom: calc(env(safe-area-inset-bottom) + 48rpx);
font-weight: bold;
.button-reset {
margin-left: 50rpx;
width: 300rpx;
height: 88rpx;
background-color: #df282d;
color: white;
text-align: center;
line-height: 88rpx;
border-radius: 44rpx;
}
.button-create {
margin-left: 50rpx;
width: 300rpx;
height: 88rpx;
background-color: #63b8af;
color: white;
text-align: center;
line-height: 88rpx;
border-radius: 44rpx;
}
}
.key {
display: flex;
align-items: center;
background-color: #FFFFFF;
height: 120rpx;
width: 750rpx;
.key-left {
margin-left: 32rpx;
width: 80rpx;
height: 80rpx;
border-radius: 50%;
}
.key-right {
margin-left: 32rpx;
margin-right: 32rpx;
width: 574rpx;
.key-right-top {
font-size: 32rpx;
font-weight: bold;
padding-bottom: 6rpx;
}
.key-right-bottom {
font-size: 24rpx;
color: #999999;
}
}
.key-status {
margin-left: auto;
font-size: 26rpx;
color: #63b8af;
}
}
.line {
width: 100%;
height: 2rpx;
background: #EBEBEB;
}
.empty-list {
width: 150rpx;
height: 150rpx;
margin: 300rpx auto 20rpx 50%;
transform: translateX(-50%);
}
.empty-list-text {
text-align: center;
font-size: 32rpx;
color: #999999;
}
</style>