97 lines
1.9 KiB
JavaScript
97 lines
1.9 KiB
JavaScript
// pages/device_management/index.js
|
|
const { deviceApi } = require("../../utils/api");
|
|
const Toast = require("../../miniprogram_npm/tdesign-miniprogram/toast/index");
|
|
|
|
Page({
|
|
/**
|
|
* 页面的初始数据
|
|
*/
|
|
data: {
|
|
devices: [], // 设备列表数据
|
|
loading: true, // 加载状态
|
|
loadError: false, // 加载错误状态
|
|
},
|
|
|
|
/**
|
|
* 生命周期函数--监听页面加载
|
|
*/
|
|
onLoad() {
|
|
this.fetchDevices();
|
|
},
|
|
|
|
/**
|
|
* 生命周期函数--监听页面显示
|
|
*/
|
|
onShow() {
|
|
// 页面显示时也刷新数据
|
|
this.fetchDevices();
|
|
},
|
|
|
|
/**
|
|
* 获取设备列表数据
|
|
*/
|
|
async fetchDevices() {
|
|
this.setData({
|
|
loading: true,
|
|
loadError: false,
|
|
});
|
|
|
|
try {
|
|
const response = await deviceApi.getDevices();
|
|
|
|
// 处理设备数据,计算子设备数量
|
|
const deviceList = response.data.list || response.data || [];
|
|
const formattedDevices = deviceList.map((device) => {
|
|
// 计算子设备数量
|
|
const subDeviceCount = device.subdevices ? device.subdevices.length : 0;
|
|
|
|
return {
|
|
...device,
|
|
subDeviceCount: subDeviceCount,
|
|
};
|
|
});
|
|
|
|
this.setData({
|
|
devices: formattedDevices,
|
|
loading: false,
|
|
});
|
|
} catch (error) {
|
|
this.setData({
|
|
loadError: true,
|
|
loading: false,
|
|
});
|
|
}
|
|
},
|
|
|
|
|
|
/**
|
|
* 查看子设备
|
|
*/
|
|
viewSubDevices(e) {
|
|
const device = e.currentTarget.dataset.device;
|
|
wx.navigateTo({
|
|
url: `/pages/device_subdevices/index?device_id=${device.id}`
|
|
});
|
|
},
|
|
|
|
|
|
/**
|
|
* 页面相关事件处理函数--监听用户下拉动作
|
|
*/
|
|
onPullDownRefresh() {
|
|
this.fetchDevices();
|
|
setTimeout(() => {
|
|
wx.stopPullDownRefresh();
|
|
}, 1000);
|
|
},
|
|
|
|
/**
|
|
* 用户点击右上角分享
|
|
*/
|
|
onShareAppMessage() {
|
|
return {
|
|
title: "设备管理",
|
|
path: "/pages/device_management/index",
|
|
};
|
|
}
|
|
}); |