congming_huose-apk/common/components/DeviceTab.vue

360 lines
9.6 KiB
Vue
Raw Normal View History

2025-11-08 11:30:06 +08:00
<template>
<view class="device-tab">
<!-- 设备列表 -->
<scroll-view scroll-y @scrolltolower="handqixing" refresher-enabled @refresherrefresh="onRefresh" :refresher-triggered="isRefreshing" refresher-default-style="black" class="device-list">
<view
v-for="(device, index) in deviceList"
:key="index"
class="device-item"
@click="handleDeviceClick(device)">
<!-- 红色角标 -->
<!-- <view class="device-badge">{{ device.badge || 3 }}</view> -->
<!-- 灰色圆角头像 -->
<view class="device-avatar">
<image :src="device.productPicture" mode="aspectFill"></image>
</view>
<!-- 信息区 -->
<view class="device-info">
<text class="device-name">{{ device.name }}</text>
<text class="device-name" style="font-weight: 400;">{{ device.productName}}</text>
<text class="device-room" v-if="device.roomName">{{ device.roomName }}</text>
<view class="device-status-bar">
<image v-if="device.productType == 'HUB'" src="https://api.ccttiot.com/smartmeter/img/static/uQiIeJDApFO5xSvepE4k" mode="aspectFill"></image>
<image src="https://api.ccttiot.com/smartmeter/img/static/uqQvPIflXlEoSaHjqc4q" mode="aspectFill"></image>
<image style="" src="https://api.ccttiot.com/smartmeter/img/static/uQ7HTwLk1XDaV11mohr4" mode="aspectFill"></image>
<!-- <image src="https://api.ccttiot.com/smartmeter/img/static/uJlfAizVTP6tBYDpTlab" mode=""></image>
<image src="https://api.ccttiot.com/smartmeter/img/static/u8yd27DMbdMief7MsEaZ" mode=""></image>
<image src="https://api.ccttiot.com/smartmeter/img/static/uCVxb1dSg0PmgDRNofWg" mode=""></image> -->
</view>
</view>
<!-- 右侧箭头 -->
<view class="device-arrow"></view>
</view>
<view v-if="deviceList.length == 0" style="margin: auto;margin-top: 50rpx;width: 272rpx;height: 272rpx;border-radius: 50%;border: 1px solid #ccc;">
<image style="width: 272rpx;height: 272rpx;" src="https://api.ccttiot.com/ad93702b02429b8adf394569460b8b2a-1758263546164.png" mode=""></image>
</view>
<view v-if="deviceList.length == 0" style="width: 600rpx;text-align: center;margin: auto;margin-top: 30rpx;">
{{$i18n.t('hubwz')}}
</view>
</scroll-view>
2026-01-17 17:34:06 +08:00
<!-- 添加设备按钮 -->
<view class="add-device-btn" @click="handleAddDevice">
<text class="add-icon">+</text>
<text>{{ $i18n.t('addDevice') }}</text>
</view>
2025-11-08 11:30:06 +08:00
</view>
</template>
<script>
export default {
name: 'DeviceTab',
data() {
return {
deviceList: [],
isRefreshing:false,
kjid:'', // 添加空间ID
pageNum:1,
pageSize:10,
total:0,
hasMore:true,
2025-12-20 14:35:59 +08:00
requestInProgress:false,
2025-11-08 11:30:06 +08:00
}
},
computed: {
// 计算当前语言,用于触发更新
currentLanguage() {
return this.$i18n.getCurrentLanguage()
}
},
created() {
this.updateDeviceList()
},
watch: {
// 监听语言变化
currentLanguage() {
console.log('设备页面语言变化:', this.currentLanguage)
this.updateDeviceList()
}
},
mounted() {
// 监听语言变化事件
uni.$on('languageChanged', this.handleLanguageChange)
// 监听空间切换事件
uni.$on('spaceChanged', this.handleSpaceChanged)
// 初始化空间ID并首次拉取列表
if(uni.getStorageSync('kjid')){
this.kjid = uni.getStorageSync('kjid')
this.getDeviceList()
}else{
this.$http.get('/bst/space/list').then(res =>{
if(res.code == 200 && res.rows.length > 0){
this.kjid = res.rows[0].id
uni.setStorageSync('kjid',this.kjid)
this.getDeviceList()
}
})
}
},
beforeDestroy() {
// 移除事件监听
uni.$off('languageChanged', this.handleLanguageChange)
uni.$off('spaceChanged', this.handleSpaceChanged)
},
methods: {
2026-01-17 17:34:06 +08:00
2025-11-08 11:30:06 +08:00
// 请求设备列表
getDeviceList(){
if(this.requestInProgress) return
this.requestInProgress = true
this.$http.get(`/bst/device/list?spaceId=${this.kjid}&pageNum=${this.pageNum}&pageSize=${this.pageSize}`).then((res) => {
if (res.code == 200) {
this.total = Number(res.total || 0)
const rows = Array.isArray(res.rows) ? res.rows : []
if(this.pageNum == 1){
this.deviceList = rows
}else{
this.deviceList = this.deviceList.concat(rows)
}
this.hasMore = this.deviceList.length < this.total
}
}).catch(() => {
// 如果请求失败,使用模拟数据
this.updateDeviceList()
}).finally(()=>{
this.requestInProgress = false
if(this.isRefreshing){
this.isRefreshing = false
}
})
},
// 处理空间变化
handleSpaceChanged(payload){
try{
this.kjid = (payload && payload.kjid) || uni.getStorageSync('kjid')
this.pageNum = 1
this.getDeviceList()
}catch(e){
console.warn('处理空间切换失败:', e)
}
},
// 上拉加载更多
handqixing() {
if(!this.hasMore) return
this.pageNum += 1
this.getDeviceList()
},
// 下拉刷新
onRefresh() {
if(this.isRefreshing || this.requestInProgress) return
this.isRefreshing = true
2026-01-15 14:41:50 +08:00
setTimeout(()=>{
this.pageNum = 1
this.getDeviceList()
},1000)
2025-11-08 11:30:06 +08:00
},
handleLanguageChange(lang) {
console.log('设备页面语言切换事件:', lang)
this.updateDeviceList()
},
updateDeviceList() {
console.log('更新设备列表')
// 强制更新组件
this.$forceUpdate()
},
// 点击设备跳转到详情
handleDeviceClick(device) {
2025-12-20 14:35:59 +08:00
console.log(device);
if(device.productType == 'HUB'){ //hub
2025-11-08 11:30:06 +08:00
uni.navigateTo({
2025-12-20 14:35:59 +08:00
url: '/subpackage/device/devicexq?id=' + device.id + '&mac=' + device.mac
2025-11-08 11:30:06 +08:00
})
2025-12-20 14:35:59 +08:00
}else if(device.productType == 'FIRE'){ //烟感
2025-11-08 11:30:06 +08:00
uni.navigateTo({
2025-12-20 14:35:59 +08:00
url: '/pages/device/yangan?id=' + device.id + '&mac=' + device.mac
2025-11-08 11:30:06 +08:00
})
2025-12-20 14:35:59 +08:00
}else if(device.productType == 'DOOR'){ //门磁
2025-11-08 11:30:06 +08:00
uni.navigateTo({
url:'/pages/device/mcgydevice?id=' + device.id
})
2025-12-20 14:35:59 +08:00
}else if(device.productType == 'INFRARED'){//人体红外传感器
uni.navigateTo({
url:'/subpackage/device/rentiredwai/index?id=' + device.id
})
}else if(device.productType == 'CONTROL'){//遥控器
uni.navigateTo({
url:'/subpackage/device/yaokongqi/index?id=' + device.id + '&mac=' + device.mac
})
}else if(device.productType == 'GLASS'){//玻璃破碎传感器
uni.navigateTo({
url:'/subpackage/device/boligan/index?id=' + device.id
})
}else if(device.productType == 'WATER'){//水浸传感器
uni.navigateTo({
url:'/subpackage/device/shuiqinganying/index?id=' + device.id
})
2025-11-08 11:30:06 +08:00
}
},
// 点击添加设备
handleAddDevice() {
2025-11-25 09:17:34 +08:00
this.$http.get(`/bst/room/list?spaceId=${this.kjid}&pageNum=${this.pageNum}&pageSize=${this.pageSize}`).then((res) => {
if (res.code == 200) {
if(res.total > 0){
this.$http.get(`/bst/device/list?spaceId=${this.kjid}&pageNum=${this.pageNum}&pageSize=${this.pageSize}`).then(res =>{
if(res.code == 200){
2026-01-17 17:34:06 +08:00
if(res.rows.length == 0){ // 先判断是否有无HUB设备没有则必须先添加HUB设备才能进行下一步
2025-11-08 11:30:06 +08:00
uni.navigateTo({
2025-11-25 09:17:34 +08:00
url:'/pages/device/addhub'
2025-11-08 11:30:06 +08:00
})
}else{
uni.navigateTo({
2025-11-25 09:17:34 +08:00
url:'/subpackage/device/adddevice'
2025-11-08 11:30:06 +08:00
})
}
2025-11-25 09:17:34 +08:00
}else{
uni.showToast({
title: res.msg,
icon: 'none',
2026-01-17 17:34:06 +08:00
duration:5000
2025-11-25 09:17:34 +08:00
})
2025-11-08 11:30:06 +08:00
}
})
2025-11-25 09:17:34 +08:00
}else{
uni.navigateTo({
url:'/pages/room/addroom'
})
2025-11-08 11:30:06 +08:00
}
}
})
}
}
}
2025-12-20 14:35:59 +08:00
</script>s
2025-11-08 11:30:06 +08:00
<style lang="scss" scoped>
2025-12-20 14:35:59 +08:00
.container {
padding: 100rpx 30rpx;
}
2025-11-08 11:30:06 +08:00
.device-tab {
// padding: 20rpx;
// background: #f5f5f5;
}
.device-list {
2026-01-17 17:34:06 +08:00
// margin-bottom: 30rpx;
height: 71vh;
2025-11-08 11:30:06 +08:00
overflow: scroll;
}
.device-item {
position: relative;
display: flex;
align-items: center;
margin: auto;
box-sizing: border-box;
margin-top: 20rpx;
background: #fff;
border-radius: 20rpx;
margin-bottom: 20rpx;
padding: 30rpx 40rpx;
box-shadow: -2rpx 2rpx 6rpx rgba(0, 0, 0, 0.08);
transition: all 0.2s ease;
width: 700rpx;
}
.device-item:active {
transform: scale(0.98);
background: #f8f8f8;
box-shadow: 0 1rpx 5rpx rgba(0,0,0,0.1);
}
.device-badge {
position: absolute;
top: 30rpx;
left: 0;
background: #ff4444;
color: #fff;
font-size: 22rpx;
border-radius:0 8rpx 8rpx 0;
padding: 4rpx 12rpx;
z-index: 1;
}
.device-avatar {
width: 128rpx;
height: 128rpx;
background: #e0e0e0;
border-radius: 20rpx;
overflow: hidden;
margin-right: 20rpx;
display: flex;
align-items: center;
justify-content: center;
margin-left: 10rpx;
image{
width: 128rpx;
height: 128rpx;
}
}
.device-icon {
font-size: 40rpx;
}
.device-info {
flex: 1;
display: flex;
flex-direction: column;
justify-content: center;
}
.device-name {
font-size: 30rpx;
color: #333;
font-weight: bold;
margin-bottom: 8rpx;
}
.device-room {
font-size: 26rpx;
color: #999;
margin-bottom: 12rpx;
}
.device-status-bar {
display: flex;
align-items: center;
image{
width: 44rpx;
height: 44rpx;
}
}
.status-icon {
font-size: 28rpx;
color: #666;
margin-right: 16rpx;
}
.device-arrow {
font-size: 66rpx;
color: #bbb;
margin-left: 20rpx;
}
.add-device-btn {
2026-01-17 17:34:06 +08:00
position: fixed;
bottom: 180rpx;
left: 0;
right: 0;
2025-11-08 11:30:06 +08:00
margin: 40rpx auto 0 auto;
width: 700rpx;
height: 80rpx;
border: 2rpx solid #bbb;
border-radius: 40rpx;
display: flex;
align-items: center;
justify-content: center;
font-size: 30rpx;
color: #333;
background: #fff;
transition: all 0.2s ease;
.add-icon {
font-size: 38rpx;
margin-right: 16rpx;
}
}
.add-device-btn:active {
transform: scale(0.95);
background: #f0f0f0;
border-color: #999;
}
</style>