index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. const { museumApi } = require('../../../utils/api.js');
  2. Page({
  3. data: {
  4. formData: {
  5. title: '',
  6. content: '',
  7. name: '',
  8. email: '',
  9. feedbackId: 0
  10. }
  11. },
  12. onLoad() {
  13. // 页面加载时的初始化
  14. },
  15. // 标题输入
  16. onTitleInput(e) {
  17. this.setData({
  18. 'formData.title': e.detail.value
  19. });
  20. },
  21. // 内容输入
  22. onContentInput(e) {
  23. this.setData({
  24. 'formData.content': e.detail.value
  25. });
  26. },
  27. // 姓名输入
  28. onNameInput(e) {
  29. this.setData({
  30. 'formData.name': e.detail.value
  31. });
  32. },
  33. // 邮箱输入
  34. onEmailInput(e) {
  35. this.setData({
  36. 'formData.email': e.detail.value
  37. });
  38. },
  39. // 验证表单
  40. validateForm() {
  41. const { title, content, name, email } = this.data.formData;
  42. if (!title.trim()) {
  43. wx.showToast({
  44. title: '请输入标题',
  45. icon: 'none'
  46. });
  47. return false;
  48. }
  49. if (!content.trim()) {
  50. wx.showToast({
  51. title: '请输入反馈内容',
  52. icon: 'none'
  53. });
  54. return false;
  55. }
  56. if (!name.trim()) {
  57. wx.showToast({
  58. title: '请输入姓名',
  59. icon: 'none'
  60. });
  61. return false;
  62. }
  63. if (!email.trim()) {
  64. wx.showToast({
  65. title: '请输入邮箱',
  66. icon: 'none'
  67. });
  68. return false;
  69. }
  70. // 简单的邮箱格式验证
  71. const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
  72. if (!emailRegex.test(email)) {
  73. wx.showToast({
  74. title: '请输入正确的邮箱格式',
  75. icon: 'none'
  76. });
  77. return false;
  78. }
  79. return true;
  80. },
  81. // 提交反馈
  82. onSubmit() {
  83. if (!this.validateForm()) {
  84. return;
  85. }
  86. wx.showLoading({
  87. title: '提交中...'
  88. });
  89. const submitData = {
  90. title: this.data.formData.title.trim(),
  91. content: this.data.formData.content.trim(),
  92. name: this.data.formData.name.trim(),
  93. email: this.data.formData.email.trim(),
  94. feedbackId: this.data.formData.feedbackId
  95. };
  96. museumApi.submitFeedback(submitData)
  97. .then((res) => {
  98. wx.hideLoading();
  99. wx.showToast({
  100. title: '提交成功',
  101. icon: 'success',
  102. });
  103. // 清空表单
  104. this.setData({
  105. formData: {
  106. title: '',
  107. content: '',
  108. name: '',
  109. email: '',
  110. feedbackId: 0
  111. }
  112. });
  113. // 延迟返回上一页
  114. setTimeout(() => {
  115. wx.navigateBack();
  116. }, 1000);
  117. })
  118. .catch((error) => {
  119. console.error('提交反馈失败:', error);
  120. wx.showToast({
  121. title: error.message || '提交失败,请重试',
  122. icon: 'none'
  123. });
  124. wx.hideLoading();
  125. });
  126. }
  127. });