tousujianyi.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. Page({
  2. data: {
  3. address: '安平镇安坪村安平小区12栋305室',
  4. contact: '',
  5. phone: '',
  6. repairType: '',
  7. description: '',
  8. imageList: [],
  9. showNotification: true,
  10. countDown: 3,
  11. isFormValid: false,
  12. category: '投诉'
  13. },
  14. onLoad: function(options) {
  15. this.startCountDown();
  16. this.checkFormValidity();
  17. },
  18. startCountDown: function() {
  19. let that = this;
  20. let timer = setInterval(function() {
  21. if (that.data.countDown > 0) {
  22. that.setData({
  23. countDown: that.data.countDown - 1
  24. });
  25. } else {
  26. clearInterval(timer);
  27. }
  28. }, 1000);
  29. },
  30. goBack: function() {
  31. wx.navigateBack();
  32. },
  33. radioChange: function(e) {
  34. this.setData({
  35. category: e.detail.value
  36. });
  37. this.checkFormValidity();
  38. },
  39. inputContact: function(e) {
  40. this.setData({
  41. contact: e.detail.value
  42. });
  43. this.checkFormValidity();
  44. },
  45. inputPhone: function(e) {
  46. const value = e.detail.value;
  47. const phoneNumber = value.replace(/\D/g, '');
  48. this.setData({
  49. phone: phoneNumber
  50. });
  51. this.checkFormValidity();
  52. },
  53. validatePhone: function(phone) {
  54. const phoneReg = /^1[3-9]\d{9}$/;
  55. return phoneReg.test(phone);
  56. },
  57. inputDescription: function(e) {
  58. this.setData({
  59. description: e.detail.value
  60. });
  61. this.checkFormValidity();
  62. },
  63. chooseImage: function() {
  64. let that = this;
  65. if (that.data.imageList.length >= 10) {
  66. wx.showToast({
  67. title: '最多只能上传10张图片',
  68. icon: 'none'
  69. });
  70. return;
  71. }
  72. wx.chooseMedia({
  73. count: 10 - that.data.imageList.length,
  74. mediaType: ['image'],
  75. sourceType: ['album', 'camera'],
  76. sizeType: ['compressed'],
  77. success: function(res) {
  78. let tempFiles = res.tempFiles;
  79. let validFiles = [];
  80. for (let i = 0; i < tempFiles.length; i++) {
  81. const file = tempFiles[i];
  82. if (file.size <= 10 * 1024 * 1024) {
  83. validFiles.push(file.tempFilePath);
  84. } else {
  85. wx.showToast({
  86. title: '图片大小不能超过10M',
  87. icon: 'none'
  88. });
  89. }
  90. }
  91. if (validFiles.length > 0) {
  92. let newImageList = that.data.imageList.concat(validFiles);
  93. that.setData({
  94. imageList: newImageList
  95. });
  96. }
  97. }
  98. });
  99. },
  100. previewImage: function(e) {
  101. let index = e.currentTarget.dataset.index;
  102. wx.previewImage({
  103. current: this.data.imageList[index],
  104. urls: this.data.imageList
  105. });
  106. },
  107. deleteImage: function(e) {
  108. let index = e.currentTarget.dataset.index;
  109. let imageList = this.data.imageList;
  110. imageList.splice(index, 1);
  111. this.setData({
  112. imageList: imageList
  113. });
  114. },
  115. checkFormValidity: function() {
  116. const { contact, phone, description, category } = this.data;
  117. // 只检查必填项:联系人、联系电话和内容说明
  118. const isValid = contact.trim() !== '' && this.validatePhone(phone) && description.trim() !== '';
  119. this.setData({
  120. isFormValid: isValid
  121. });
  122. return isValid;
  123. },
  124. onInputChange: function(e) {
  125. const { field } = e.currentTarget.dataset;
  126. const { value } = e.detail;
  127. this.setData({
  128. [field]: value
  129. });
  130. this.checkFormValidity();
  131. },
  132. bindPickerChange: function(e) {
  133. this.checkFormValidity();
  134. },
  135. // 添加submitForm方法
  136. submitForm: function() {
  137. if (!this.checkFormValidity()) {
  138. let errorMsg = '';
  139. if (!this.data.contact.trim()) {
  140. errorMsg = '请填写联系人';
  141. } else if (!this.validatePhone(this.data.phone)) {
  142. errorMsg = '请输入正确的联系电话';
  143. } else if (!this.data.description.trim()) {
  144. errorMsg = '请填写内容说明';
  145. }
  146. wx.showToast({
  147. title: errorMsg,
  148. icon: 'none'
  149. });
  150. return;
  151. }
  152. // 构建提交数据
  153. const submitData = {
  154. category: this.data.category,
  155. contact: this.data.contact,
  156. phone: this.data.phone,
  157. description: this.data.description,
  158. images: this.data.imageList
  159. };
  160. console.log('提交的数据:', submitData);
  161. wx.showLoading({
  162. title: '提交中...',
  163. });
  164. setTimeout(() => {
  165. wx.hideLoading();
  166. //保存成功
  167. wx.showToast({
  168. title: '提交成功',
  169. icon: 'success',
  170. duration: 2000,
  171. success: function() {
  172. setTimeout(() => {
  173. wx.navigateTo({
  174. url: '/pages/tousujianyiSuccess/tousujianyiSuccess',
  175. });
  176. }, 2000);
  177. }
  178. });
  179. }, 1500);
  180. },
  181. submitRepair: function() {
  182. this.submitForm();
  183. }
  184. });