Novecane
2025-02-15 d13e470cf0256e3ffbc3b0ad34df27ce0b0b5e5f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<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>