Suzhou-SafetyToolsWarning-WX/pages/device/list/list.js

276 lines
6.6 KiB
JavaScript

// pages/device/list/list.js
const app = getApp();
Page({
data: {
searchValue: '',
activeFilter: 'all',
deviceList: [],
allDeviceList: [], // 保存所有设备数据
statistics: {
total: 0,
normal: 0,
warning: 0,
expired: 0
},
loading: false,
showFilter: false,
deviceTypes: ['全部', '安全帽', '安全带', '绝缘手套', '绝缘鞋', '验电器', '接地线', '安全围栏'],
useUnits: ['全部', '施工一队', '施工二队', '施工三队', '维护班组', '检修班组'],
selectedType: '全部',
selectedUnit: '全部'
},
onLoad() {
this.loadDeviceList();
},
onShow() {
// 每次显示时刷新列表
this.loadDeviceList();
},
// 加载设备列表
loadDeviceList() {
this.setData({ loading: true });
// TODO: 从后端或本地存储加载数据
// 这里使用模拟数据
setTimeout(() => {
const mockData = this.getMockDeviceList();
this.setData({
allDeviceList: mockData,
deviceList: mockData,
loading: false
});
this.calculateStatistics();
}, 500);
},
// 模拟设备数据
getMockDeviceList() {
const today = new Date();
const devices = [
{
id: '1',
deviceName: '绝缘手套',
deviceCode: 'SB-2023-001',
deviceType: '绝缘手套',
specification: 'YS101-10kV',
useUnit: '施工一队',
belongUnit: '电力公司',
lastInspectionDate: '2023-06-01',
nextInspectionDate: '2024-06-01',
status: 'warning'
},
{
id: '2',
deviceName: '安全帽',
deviceCode: 'SB-2023-015',
deviceType: '安全帽',
specification: 'ABS-001',
useUnit: '施工二队',
belongUnit: '电力公司',
lastInspectionDate: '2023-08-15',
nextInspectionDate: '2024-08-15',
status: 'normal'
},
{
id: '3',
deviceName: '安全带',
deviceCode: 'SB-2023-028',
deviceType: '安全带',
specification: 'AD-500',
useUnit: '施工三队',
belongUnit: '电力公司',
lastInspectionDate: '2023-05-20',
nextInspectionDate: '2024-05-20',
status: 'expired'
},
{
id: '4',
deviceName: '验电器',
deviceCode: 'SB-2023-042',
deviceType: '验电器',
specification: 'YDQ-10kV',
useUnit: '维护班组',
belongUnit: '电力公司',
lastInspectionDate: '2023-09-10',
nextInspectionDate: '2024-09-10',
status: 'normal'
},
{
id: '5',
deviceName: '绝缘鞋',
deviceCode: 'SB-2023-056',
deviceType: '绝缘鞋',
specification: 'JYX-10kV',
useUnit: '检修班组',
belongUnit: '电力公司',
lastInspectionDate: '2023-07-01',
nextInspectionDate: '2024-07-01',
status: 'warning'
}
];
// 计算每个设备的状态和剩余天数
return devices.map(device => {
const nextDate = new Date(device.nextInspectionDate);
const diffTime = nextDate - today;
const daysLeft = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
let status = 'normal';
let statusText = '正常';
let statusTheme = 'success';
let isWarning = false;
if (daysLeft < 0) {
status = 'expired';
statusText = '已到期';
statusTheme = 'danger';
isWarning = true;
} else if (daysLeft <= 30) {
status = 'warning';
statusText = '预警';
statusTheme = 'warning';
isWarning = true;
}
return {
...device,
status,
statusText,
statusTheme,
daysLeft,
isWarning
};
});
},
// 计算统计数据
calculateStatistics() {
const { deviceList } = this.data;
const statistics = {
total: deviceList.length,
normal: deviceList.filter(d => d.status === 'normal').length,
warning: deviceList.filter(d => d.status === 'warning').length,
expired: deviceList.filter(d => d.status === 'expired').length
};
this.setData({ statistics });
},
// 搜索
onSearchChange(e) {
this.setData({ searchValue: e.detail.value });
},
onSearch() {
this.filterDeviceList();
},
onSearchClear() {
this.setData({ searchValue: '' });
this.filterDeviceList();
},
// 筛选
onFilterChange(e) {
const filter = e.currentTarget.dataset.filter;
this.setData({ activeFilter: filter });
this.filterDeviceList();
},
filterDeviceList() {
const { allDeviceList, searchValue, activeFilter, selectedType, selectedUnit } = this.data;
let filtered = allDeviceList;
// 搜索过滤
if (searchValue) {
filtered = filtered.filter(device =>
device.deviceName.includes(searchValue) ||
device.deviceCode.includes(searchValue) ||
device.specification.includes(searchValue)
);
}
// 状态过滤
if (activeFilter !== 'all') {
filtered = filtered.filter(device => device.status === activeFilter);
}
// 类型过滤
if (selectedType !== '全部') {
filtered = filtered.filter(device => device.deviceType === selectedType);
}
// 单位过滤
if (selectedUnit !== '全部') {
filtered = filtered.filter(device => device.useUnit === selectedUnit);
}
this.setData({ deviceList: filtered });
this.calculateStatistics();
},
// 显示筛选弹窗
showFilterDialog() {
this.setData({ showFilter: true });
},
// 选择类型
selectType(e) {
this.setData({ selectedType: e.currentTarget.dataset.type });
},
// 选择单位
selectUnit(e) {
this.setData({ selectedUnit: e.currentTarget.dataset.unit });
},
// 确认筛选
onFilterConfirm() {
this.setData({ showFilter: false });
this.filterDeviceList();
},
// 重置筛选
onFilterReset() {
this.setData({
selectedType: '全部',
selectedUnit: '全部',
showFilter: false
});
this.filterDeviceList();
},
// 关闭筛选弹窗
onFilterClose() {
this.setData({ showFilter: false });
},
// 跳转到详情
goToDetail(e) {
const id = e.currentTarget.dataset.id;
wx.navigateTo({
url: `/pages/device/detail/detail?id=${id}`
});
},
// 跳转到添加页面
goToAdd() {
wx.navigateTo({
url: '/pages/device/add/add'
});
},
// 下拉刷新
onPullDownRefresh() {
this.loadDeviceList();
setTimeout(() => {
wx.stopPullDownRefresh();
}, 1000);
}
});