|
|
Page({ /** * 页面的初始数据 */ data: { formData: { petType: '', petAge: '', petGender: '', symptoms: '', images: [] }, symptomsLength: 0, isSubmitting: false, isFormValid: false },
/** * 生命周期函数--监听页面加载 */ onLoad(options) { // 初始化表单验证
this.checkFormValidity(); },
// 宠物类型输入
onPetTypeInput(e) { const value = e.detail.value; this.setData({ 'formData.petType': value }, () => { this.checkFormValidity(); }); },
// 宠物年龄输入
onPetAgeInput(e) { const value = e.detail.value; this.setData({ 'formData.petAge': value }, () => { this.checkFormValidity(); }); },
// 选择性别
selectGender(e) { const value = e.currentTarget.dataset.value; this.setData({ 'formData.petGender': value }, () => { this.checkFormValidity(); }); },
// 症状描述输入
onSymptomsInput(e) { const value = e.detail.value; this.setData({ 'formData.symptoms': value, symptomsLength: value.length }, () => { this.checkFormValidity(); }); },
// 选择图片
chooseImage() { if (this.data.formData.images.length >= 3) { wx.showToast({ title: '最多上传3张图片', icon: 'none' }); return; }
wx.chooseImage({ count: 3 - this.data.formData.images.length, sizeType: ['compressed'], sourceType: ['album', 'camera'], success: (res) => { const tempFilePaths = res.tempFilePaths; const images = [...this.data.formData.images, ...tempFilePaths]; this.setData({ 'formData.images': images }); } }); },
// 移除图片
removeImage(e) { const index = e.currentTarget.dataset.index; const images = [...this.data.formData.images]; images.splice(index, 1); this.setData({ 'formData.images': images }); },
// 预览图片
previewImage(e) { const current = e.currentTarget.dataset.url; const urls = e.currentTarget.dataset.urls; wx.previewImage({ current, urls }); },
// 检查表单有效性
checkFormValidity() { const { petType, petAge, petGender, symptoms } = this.data.formData; const isValid = petType.trim() && petAge && petGender && symptoms.trim(); this.setData({ isFormValid: !!isValid }); },
// 表单提交
submitForm(e) { if (this.data.isSubmitting || !this.data.isFormValid) { return; }
this.setData({ isSubmitting: true });
// 模拟提交
setTimeout(() => { wx.showToast({ title: '提交成功', icon: 'success', duration: 1500, success: () => { setTimeout(() => { wx.navigateTo({ url: '/pagesA/pages/askingSy/askingSy', }) }, 1500); } }); }, 1500); }});
|