const { museumApi } = require('../../../utils/api.js'); Page({ data: { formData: { title: '', content: '', name: '', email: '', feedbackId: 0 } }, onLoad() { // 页面加载时的初始化 }, // 标题输入 onTitleInput(e) { this.setData({ 'formData.title': e.detail.value }); }, // 内容输入 onContentInput(e) { this.setData({ 'formData.content': e.detail.value }); }, // 姓名输入 onNameInput(e) { this.setData({ 'formData.name': e.detail.value }); }, // 邮箱输入 onEmailInput(e) { this.setData({ 'formData.email': e.detail.value }); }, // 验证表单 validateForm() { const { title, content, name, email } = this.data.formData; if (!title.trim()) { wx.showToast({ title: '请输入标题', icon: 'none' }); return false; } if (!content.trim()) { wx.showToast({ title: '请输入反馈内容', icon: 'none' }); return false; } if (!name.trim()) { wx.showToast({ title: '请输入姓名', icon: 'none' }); return false; } if (!email.trim()) { wx.showToast({ title: '请输入邮箱', icon: 'none' }); return false; } // 简单的邮箱格式验证 const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; if (!emailRegex.test(email)) { wx.showToast({ title: '请输入正确的邮箱格式', icon: 'none' }); return false; } return true; }, // 提交反馈 onSubmit() { if (!this.validateForm()) { return; } wx.showLoading({ title: '提交中...' }); const submitData = { title: this.data.formData.title.trim(), content: this.data.formData.content.trim(), name: this.data.formData.name.trim(), email: this.data.formData.email.trim(), feedbackId: this.data.formData.feedbackId }; museumApi.submitFeedback(submitData) .then((res) => { wx.showToast({ title: '提交成功', icon: 'success', duration: 2000 }); // 清空表单 this.setData({ formData: { title: '', content: '', name: '', email: '', feedbackId: 0 } }); // 延迟返回上一页 setTimeout(() => { wx.navigateBack(); }, 2000); wx.hideLoading(); }) .catch((error) => { console.error('提交反馈失败:', error); wx.showToast({ title: error.message || '提交失败,请重试', icon: 'none' }); wx.hideLoading(); }); } });