// pages/device_status/index.js const { deviceApi } = require("../../utils/api"); Page({ /** * 页面的初始数据 */ data: { devices: [], statusMap: { 0: "空闲", 1: "使用中", 2: "维修中", }, loading: true, // 添加加载状态 activeDevices: [], // 当前展开的设备面板,空数组表示全部收起 }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { // 检查用户是否已登录 const app = getApp(); if (app.globalData.token) { // 已登录用户,跳转到设备管理页面 wx.redirectTo({ url: '/pages/device_management/index', }); return; } this.fetchDeviceStatus(); }, /** * 获取设备状态数据 */ fetchDeviceStatus() { const that = this; // 保存当前展开状态 const currentActiveDevices = this.data.activeDevices || []; // 显示加载中 that.setData({ loading: true }); deviceApi .getDeviceStatus() .then((response) => { // 处理API响应格式,获取设备数组 const devices = response.data || []; // 处理设备状态的显示文本 devices.forEach((device) => { if (device.sub_devices && device.sub_devices.length) { device.sub_devices.forEach((subDevice) => { subDevice.statusText = that.data.statusMap[subDevice.status] || subDevice.status; }); } }); // 保持之前的展开状态 that.setData({ devices, activeDevices: currentActiveDevices, // 保持之前的展开状态 loading: false, }); }) .catch((err) => { wx.showToast({ title: "获取设备状态失败", icon: "error", }); }) .finally(() => { // 停止下拉刷新动画 wx.stopPullDownRefresh(); that.setData({ loading: false }); }); }, /** * 处理折叠面板展开/收起事件 */ handleCollapseChange(e) { this.setData({ activeDevices: e.detail.value, }); }, /** * 跳转到保养记录页面 */ goToMaintenancePage(e) { const subDeviceId = e.currentTarget.dataset.subDeviceId; wx.navigateTo({ url: `/pages/device_maintenance/index?sub_device_id=${subDeviceId}`, }); }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { this.fetchDeviceStatus(); }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() {}, /** * 用户点击右上角分享 */ onShareAppMessage() { return { title: "设备状态监控", path: "/pages/device_status/index", }; }, });