const { deviceApi } = require("../../utils/api"); const Toast = require("../../miniprogram_npm/tdesign-miniprogram/toast/index"); Page({ data: { device: null, device_id: null, loading: true, loadError: false, // 设备状态相关 deviceStatus: 'normal', deviceStatusMap: { normal: '正常', maintenance: '保养中', repair: '维修中', fault: '故障', }, statusDialogVisible: false, selectedStatus: 'normal', // 设备编辑相关 editDialogVisible: false, editForm: { name: '', description: '', }, // 子设备相关 subDevices: [], subDeviceLoading: false, // 保养记录相关 maintenanceStats: { total: 0, completed: 0, inProgress: 0, }, // 操作选项 actions: [ { icon: 'setting', text: '更改状态', color: '#0052d9', action: 'changeStatus' }, { icon: 'add-circle', text: '添加保养记录', color: '#06a56c', action: 'addMaintenance' }, { icon: 'time', text: '保养记录', color: '#fa8c16', action: 'viewMaintenance' }, { icon: 'edit', text: '编辑设备', color: '#722ed1', action: 'editDevice' }, { icon: 'view-list', text: '管理子设备', color: '#0052d9', action: 'manageSubDevices' }, { icon: 'delete', text: '删除设备', color: '#e34d59', action: 'deleteDevice' } ] }, onLoad(options) { if (options.device_id) { this.setData({ device_id: options.device_id }); this.loadDeviceInfo(); this.loadSubDevices(); this.loadMaintenanceStats(); } else { Toast({ context: this, selector: '#t-toast', message: '缺少设备ID', theme: 'error', }); wx.navigateBack(); } }, onShow() { // 页面显示时刷新数据 if (this.data.device_id) { this.loadDeviceInfo(); this.loadSubDevices(); this.loadMaintenanceStats(); } }, // 加载设备信息 async loadDeviceInfo() { this.setData({ loading: true, loadError: false }); try { const response = await deviceApi.getDevices(); const deviceList = response.data.list || response.data || []; const device = deviceList.find(d => d.id === parseInt(this.data.device_id)); if (device) { // 计算子设备数量 const subDeviceCount = device.sub_devices ? device.sub_devices.length : 0; this.setData({ device: { ...device, subDeviceCount }, deviceStatus: device.status || 'normal', selectedStatus: device.status || 'normal', editForm: { name: device.name, description: device.description || '', }, loading: false, }); } else { this.setData({ loadError: true, loading: false, }); } } catch (error) { this.setData({ loadError: true, loading: false, }); } }, // 加载子设备 async loadSubDevices() { this.setData({ subDeviceLoading: true }); try { const response = await deviceApi.getSubDevicesByDeviceId(this.data.device_id); const subDevices = response.data.list || response.data || []; this.setData({ subDevices: subDevices, subDeviceLoading: false, }); } catch (error) { this.setData({ subDevices: [], subDeviceLoading: false, }); } }, // 加载保养记录统计 async loadMaintenanceStats() { try { // 这里可以调用保养记录统计API // 目前使用模拟数据 this.setData({ maintenanceStats: { total: 0, completed: 0, inProgress: 0, } }); } catch (error) { // 加载保养统计失败,使用默认值 } }, // 处理操作按钮点击 handleAction(e) { const action = e.currentTarget.dataset.action; switch (action) { case 'changeStatus': this.showStatusDialog(); break; case 'addMaintenance': this.addMaintenance(); break; case 'viewMaintenance': this.viewMaintenance(); break; case 'editDevice': this.showEditDialog(); break; case 'manageSubDevices': this.manageSubDevices(); break; case 'deleteDevice': this.deleteDevice(); break; } }, // 显示状态更改弹窗 showStatusDialog() { this.setData({ statusDialogVisible: true, selectedStatus: this.data.deviceStatus, }); }, // 关闭状态更改弹窗 closeStatusDialog() { this.setData({ statusDialogVisible: false, }); }, // 选择状态 selectStatus(e) { const status = e.currentTarget.dataset.status; this.setData({ selectedStatus: status, }); }, // 确认状态更改 async confirmStatusChange() { const { selectedStatus, device } = this.data; try { await deviceApi.updateDevice(device.id, { status: selectedStatus }); this.setData({ deviceStatus: selectedStatus, statusDialogVisible: false, }); Toast({ context: this, selector: '#t-toast', message: '状态更新成功', theme: 'success', }); // 刷新设备信息 this.loadDeviceInfo(); } catch (error) { Toast({ context: this, selector: '#t-toast', message: '状态更新失败', theme: 'error', }); } }, // 显示编辑弹窗 showEditDialog() { this.setData({ editDialogVisible: true, }); }, // 关闭编辑弹窗 closeEditDialog() { this.setData({ editDialogVisible: false, }); }, // 确认编辑 async confirmEdit() { const { editForm, device } = this.data; if (!editForm.name.trim()) { Toast({ context: this, selector: '#t-toast', message: '请输入设备名称', theme: 'warning', }); return; } try { await deviceApi.updateDevice(device.id, { name: editForm.name.trim(), description: editForm.description.trim(), }); this.setData({ editDialogVisible: false, }); Toast({ context: this, selector: '#t-toast', message: '设备信息更新成功', theme: 'success', }); // 刷新设备信息 this.loadDeviceInfo(); } catch (error) { Toast({ context: this, selector: '#t-toast', message: '更新失败', theme: 'error', }); } }, // 添加保养记录 addMaintenance() { wx.navigateTo({ url: `/pages/device_maintenance/create/index?device_id=${this.data.device_id}`, }); }, // 查看保养记录 viewMaintenance() { wx.navigateTo({ url: `/pages/device_maintenance/index?device_id=${this.data.device_id}`, }); }, // 管理子设备 manageSubDevices() { wx.navigateTo({ url: `/pages/device_subdevices/index?device_id=${this.data.device_id}`, }); }, // 查看子设备详情 viewSubDeviceDetail(e) { const subDevice = e.currentTarget.dataset.subdevice; wx.navigateTo({ url: `/pages/device_subdevices/index?device_id=${this.data.device_id}&sub_device_id=${subDevice.id}`, }); }, // 删除设备 deleteDevice() { wx.showModal({ title: '确认删除', content: '确定要删除此设备吗?此操作不可撤销。', success: (res) => { if (res.confirm) { this.confirmDelete(); } }, }); }, // 确认删除 async confirmDelete() { const { device } = this.data; try { await deviceApi.deleteDevice(device.id); Toast({ context: this, selector: '#t-toast', message: '删除成功', theme: 'success', }); // 返回设备管理页面 wx.navigateBack(); } catch (error) { Toast({ context: this, selector: '#t-toast', message: '删除失败', theme: 'error', }); } }, // 返回上一页 onBack() { wx.navigateBack(); }, // 下拉刷新 onPullDownRefresh() { Promise.all([ this.loadDeviceInfo(), this.loadSubDevices(), this.loadMaintenanceStats(), ]).then(() => { wx.stopPullDownRefresh(); }).catch(() => { wx.stopPullDownRefresh(); }); }, });