<template>
|
<view class="device-page">
|
<view v-if="devices.length === 0" class="no-devices">
|
暂无设备
|
<button class="add-device-btn" @click="addHandler">
|
添加设备
|
</button>
|
</view>
|
<view v-else class="deviceList">
|
<view v-for="(device, index) in devices" :key="index" class="device-item" @click="editDeviceInfo(device)">
|
<img src="/assets/device.png" class="device-image" />
|
<div class="device-info">
|
<p class="device-name">{{ device.facilityName }}</p>
|
<p class="device-model">SN:{{ device.facilityCode }}</p>
|
<p v-if="device.isOnLine==1" class="device-status_zx">在线</p>
|
<p v-else class="device-status_lx">离线</p>
|
</div>
|
</view>
|
</view>
|
</view>
|
</template>
|
|
<script setup>
|
import {
|
onMounted,
|
ref
|
} from 'vue';
|
import {
|
getDevicesByiOpenId,
|
bindDevice
|
} from '@/api/index.js'
|
|
// 设备信息
|
const devices = ref([{}]);
|
|
// 传递给后端的参数
|
const state = {
|
productId: "",
|
facilityCode: "",
|
signature: ""
|
}
|
|
// 获取用户设备table
|
const getDeviceTable = () => {
|
getDevicesByiOpenId(uni.getStorageSync("openId")).then((res) => {
|
devices.value = res.data;
|
})
|
}
|
// 查看设备信息
|
const editDeviceInfo = (device) => {
|
//console.log(facilityCode);
|
uni.navigateTo({
|
url: `/pages/router/deviceInfo/index?device=${encodeURIComponent(JSON.stringify(device))}`,
|
})
|
}
|
|
//绑定设备
|
const addHandler = () => {
|
uni.scanCode({
|
scanType: ['qrCode', 'barCode'], // 扫码类型为二维码或条形码
|
success: function(res) {
|
// 扫码成功后的回调
|
let temp = JSON.parse(res.result);
|
state.facilityCode = temp.DeviceName;
|
state.productId = temp.ProductId;
|
state.signature = temp.Signature;
|
// console.log(state);
|
bindDevice(state).then((res) => {
|
if (res.code === 200) {
|
uni.showToast({
|
title: '设备绑定成功',
|
icon: 'success',
|
}),
|
// 刷新页面
|
uni.redirectTo({
|
url: "/pages/layout/index"
|
})
|
} else {
|
uni.showToast({
|
title: '设备绑定失败',
|
icon: 'fail',
|
})
|
}
|
})
|
},
|
fail: function(err) {
|
// 扫码失败后的回调
|
uni.showToast({
|
title: '扫描失败',
|
icon: 'fail',
|
})
|
}
|
});
|
}
|
|
onMounted(() => {
|
getDeviceTable();
|
})
|
</script>
|
|
<style scoped>
|
.device-page {
|
text-align: center;
|
padding: 10px;
|
}
|
|
.no-devices {
|
font-size: 18px;
|
color: #666;
|
margin-top: 300rpx;
|
}
|
|
.add-device-btn {
|
padding: 10px 20px;
|
font-size: 16px;
|
color: #fff;
|
background-color: #007bff;
|
border: none;
|
border-radius: 5px;
|
cursor: pointer;
|
margin-top: 10px;
|
}
|
|
.deviceList {
|
width: 100%;
|
display: flex;
|
flex-direction: column;
|
align-items: center;
|
gap: 20px;
|
/* 设备项之间的间距 */
|
max-height: calc(100vh - 120px);
|
/* 假设你希望为其他内容(如页眉/页脚)留出空间 */
|
overflow-y: auto;
|
/* 添加垂直滚动条 */
|
padding: 0 10rpx;
|
/* 可选:为滚动内容添加一些内边距 */
|
box-sizing: border-box;
|
/* 确保内边距和边框不会增加容器的高度 */
|
}
|
|
.device-item {
|
display: flex;
|
align-items: center;
|
text-align: center;
|
width: 100%;
|
/* 根据需要调整宽度 */
|
max-width: 600rpx;
|
/* 设置最大宽度,以便在桌面端更好地展示 */
|
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
|
/* 添加一些阴影效果 */
|
padding: 10px;
|
border-radius: 5px;
|
}
|
|
/*图片样式*/
|
.device-image {
|
width: 40%;
|
height: 250rpx;
|
/* box-shadow: #666; */
|
border-radius: 5px;
|
margin-bottom: 10px;
|
margin-right: 20rpx;
|
border: 1rpx gray solid;
|
}
|
|
.device-info {
|
text-align: left;
|
/* 设备信息左对齐 */
|
}
|
|
.device-name {
|
margin: 5rpx 0;
|
font-size: 35rpx;
|
font-weight: bold;
|
}
|
|
.device-model {
|
margin: 5px 0;
|
font-size: small;
|
}
|
|
.device-status_zx {
|
background-color: #4CAF50;
|
color: #fff;
|
padding: 4px 12px;
|
border-radius: 12px;
|
text-align: center;
|
}
|
|
.device-status_lx {
|
background-color: red;
|
color: #fff;
|
padding: 4px 12px;
|
border-radius: 12px;
|
text-align: center;
|
}
|
</style>
|