Page({ data: { address: '安平镇安坪村安平小区12栋305室', contact: '', phone: '', repairType: '', description: '', imageList: [], showNotification: true, countDown: 3, isFormValid: false, category: '投诉' }, onLoad: function(options) { this.startCountDown(); this.checkFormValidity(); }, startCountDown: function() { let that = this; let timer = setInterval(function() { if (that.data.countDown > 0) { that.setData({ countDown: that.data.countDown - 1 }); } else { clearInterval(timer); } }, 1000); }, goBack: function() { wx.navigateBack(); }, radioChange: function(e) { this.setData({ category: e.detail.value }); this.checkFormValidity(); }, inputContact: function(e) { this.setData({ contact: e.detail.value }); this.checkFormValidity(); }, inputPhone: function(e) { const value = e.detail.value; const phoneNumber = value.replace(/\D/g, ''); this.setData({ phone: phoneNumber }); this.checkFormValidity(); }, validatePhone: function(phone) { const phoneReg = /^1[3-9]\d{9}$/; return phoneReg.test(phone); }, inputDescription: function(e) { this.setData({ description: e.detail.value }); this.checkFormValidity(); }, chooseImage: function() { let that = this; if (that.data.imageList.length >= 10) { wx.showToast({ title: '最多只能上传10张图片', icon: 'none' }); return; } wx.chooseMedia({ count: 10 - that.data.imageList.length, mediaType: ['image'], sourceType: ['album', 'camera'], sizeType: ['compressed'], success: function(res) { let tempFiles = res.tempFiles; let validFiles = []; for (let i = 0; i < tempFiles.length; i++) { const file = tempFiles[i]; if (file.size <= 10 * 1024 * 1024) { validFiles.push(file.tempFilePath); } else { wx.showToast({ title: '图片大小不能超过10M', icon: 'none' }); } } if (validFiles.length > 0) { let newImageList = that.data.imageList.concat(validFiles); that.setData({ imageList: newImageList }); } } }); }, previewImage: function(e) { let index = e.currentTarget.dataset.index; wx.previewImage({ current: this.data.imageList[index], urls: this.data.imageList }); }, deleteImage: function(e) { let index = e.currentTarget.dataset.index; let imageList = this.data.imageList; imageList.splice(index, 1); this.setData({ imageList: imageList }); }, checkFormValidity: function() { const { contact, phone, description, category } = this.data; // 只检查必填项:联系人、联系电话和内容说明 const isValid = contact.trim() !== '' && this.validatePhone(phone) && description.trim() !== ''; this.setData({ isFormValid: isValid }); return isValid; }, onInputChange: function(e) { const { field } = e.currentTarget.dataset; const { value } = e.detail; this.setData({ [field]: value }); this.checkFormValidity(); }, bindPickerChange: function(e) { this.checkFormValidity(); }, // 添加submitForm方法 submitForm: function() { if (!this.checkFormValidity()) { let errorMsg = ''; if (!this.data.contact.trim()) { errorMsg = '请填写联系人'; } else if (!this.validatePhone(this.data.phone)) { errorMsg = '请输入正确的联系电话'; } else if (!this.data.description.trim()) { errorMsg = '请填写内容说明'; } wx.showToast({ title: errorMsg, icon: 'none' }); return; } // 构建提交数据 const submitData = { category: this.data.category, contact: this.data.contact, phone: this.data.phone, description: this.data.description, images: this.data.imageList }; console.log('提交的数据:', submitData); wx.showLoading({ title: '提交中...', }); setTimeout(() => { wx.hideLoading(); //保存成功 wx.showToast({ title: '提交成功', icon: 'success', duration: 2000, success: function() { setTimeout(() => { wx.navigateTo({ url: '/pages/tousujianyiSuccess/tousujianyiSuccess', }); }, 2000); } }); }, 1500); }, submitRepair: function() { this.submitForm(); } });