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

162 lines
4.1 KiB
JavaScript

// pages/device/add/add.js
Page({
data: {
mode: 'add', // add 或 edit
deviceId: '',
form: {
deviceName: '',
deviceCode: '',
specification: '',
manufacturer: '',
useUnit: '',
belongUnit: '',
location: '',
responsible: '',
purchaseDate: '',
lastInspectionDate: '',
inspectionCycle: '',
nextInspectionDate: '',
remark: ''
},
showPicker: false,
pickerTitle: '',
pickerData: [],
pickerValue: [],
currentField: '',
showDatePicker: false,
dateValue: '',
deviceNames: ['安全帽', '安全带', '绝缘手套', '绝缘鞋', '验电器', '接地线', '安全围栏', '绝缘杆', '绝缘垫', '标示牌'],
useUnits: ['施工一队', '施工二队', '施工三队', '维护班组', '检修班组']
},
onLoad(options) {
if (options.id && options.mode === 'edit') {
this.setData({
mode: 'edit',
deviceId: options.id
});
this.loadDeviceData();
}
},
loadDeviceData() {
// TODO: 加载设备数据
// 模拟数据
const mockData = {
deviceName: '绝缘手套',
deviceCode: 'SB-2023-001',
specification: 'YS101-10kV',
manufacturer: '某某电力设备厂',
useUnit: '施工一队',
belongUnit: '电力公司',
location: '工具仓库A区',
responsible: '张三',
purchaseDate: '2022-06-01',
lastInspectionDate: '2023-06-01',
inspectionCycle: '12',
nextInspectionDate: '2024-06-01',
remark: '该设备即将到期,请及时安排检验'
};
this.setData({ form: mockData });
},
showPicker(e) {
const field = e.currentTarget.dataset.field;
let pickerData = [];
let pickerTitle = '';
if (field === 'deviceName') {
pickerData = this.data.deviceNames.map(name => ({ label: name, value: name }));
pickerTitle = '选择设备名称';
} else if (field === 'useUnit') {
pickerData = this.data.useUnits.map(unit => ({ label: unit, value: unit }));
pickerTitle = '选择使用单位';
}
this.setData({
showPicker: true,
pickerData,
pickerTitle,
currentField: field
});
},
onPickerConfirm(e) {
const { value } = e.detail;
const field = this.data.currentField;
const selectedValue = this.data.pickerData[value[0]].value;
this.setData({
[`form.${field}`]: selectedValue,
showPicker: false
});
},
onPickerCancel() {
this.setData({ showPicker: false });
},
showDatePicker(e) {
const field = e.currentTarget.dataset.field;
const currentValue = this.data.form[field] || new Date().getTime();
this.setData({
showDatePicker: true,
dateValue: currentValue,
currentField: field
});
},
onDateConfirm(e) {
const timestamp = e.detail.value;
const date = new Date(timestamp);
const dateStr = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`;
this.setData({
[`form.${this.data.currentField}`]: dateStr,
showDatePicker: false
});
},
onDateCancel() {
this.setData({ showDatePicker: false });
},
submitForm() {
const { form, mode } = this.data;
// 验证必填项
if (!form.deviceName) {
wx.showToast({ title: '请选择设备名称', icon: 'none' });
return;
}
if (!form.deviceCode) {
wx.showToast({ title: '请输入设备编号', icon: 'none' });
return;
}
if (!form.useUnit) {
wx.showToast({ title: '请选择使用单位', icon: 'none' });
return;
}
if (!form.lastInspectionDate) {
wx.showToast({ title: '请选择上次检验日期', icon: 'none' });
return;
}
if (!form.nextInspectionDate) {
wx.showToast({ title: '请选择下次检验日期', icon: 'none' });
return;
}
// TODO: 提交数据到后端或本地存储
wx.showToast({
title: mode === 'edit' ? '保存成功' : '添加成功',
icon: 'success'
});
setTimeout(() => {
wx.navigateBack();
}, 1500);
}
});