baoxiudj.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. },
  13. onLoad: function(options) {
  14. this.startCountDown();
  15. },
  16. startCountDown: function() {
  17. let that = this;
  18. let timer = setInterval(function() {
  19. if (that.data.countDown > 0) {
  20. that.setData({
  21. countDown: that.data.countDown - 1
  22. });
  23. } else {
  24. clearInterval(timer);
  25. }
  26. }, 1000);
  27. },
  28. closeNotification: function() {
  29. if (this.data.countDown <= 0) {
  30. this.setData({
  31. showNotification: false
  32. });
  33. }
  34. },
  35. goBack: function() {
  36. wx.navigateBack();
  37. },
  38. inputContact: function(e) {
  39. this.setData({
  40. contact: e.detail.value
  41. });
  42. this.checkFormValidity();
  43. },
  44. inputPhone: function(e) {
  45. const value = e.detail.value;
  46. const phoneNumber = value.replace(/\D/g, '');
  47. this.setData({
  48. phone: phoneNumber
  49. });
  50. this.checkFormValidity();
  51. },
  52. validatePhone: function(phone) {
  53. const phoneReg = /^1[3-9]\d{9}$/;
  54. return phoneReg.test(phone);
  55. },
  56. showRepairTypeSelector: function() {
  57. let that = this;
  58. wx.showActionSheet({
  59. itemList: ['水管漏水', '水表故障', '水龙头故障', '其他问题'],
  60. success: function(res) {
  61. const types = ['水管漏水', '水表故障', '水龙头故障', '其他问题'];
  62. that.setData({
  63. repairType: types[res.tapIndex]
  64. });
  65. that.checkFormValidity();
  66. }
  67. });
  68. },
  69. inputDescription: function(e) {
  70. this.setData({
  71. description: e.detail.value
  72. });
  73. this.checkFormValidity();
  74. },
  75. chooseImage: function() {
  76. let that = this;
  77. if (that.data.imageList.length >= 10) {
  78. wx.showToast({
  79. title: '最多只能上传10张图片',
  80. icon: 'none'
  81. });
  82. return;
  83. }
  84. wx.chooseMedia({
  85. count: 3 - that.data.imageList.length,
  86. mediaType: ['image'],
  87. sourceType: ['album', 'camera'],
  88. sizeType: ['compressed'],
  89. success: function(res) {
  90. let tempFiles = res.tempFiles;
  91. let validFiles = [];
  92. for (let i = 0; i < tempFiles.length; i++) {
  93. const file = tempFiles[i];
  94. if (file.size <= 5 * 1024 * 1024) {
  95. validFiles.push(file.tempFilePath);
  96. } else {
  97. wx.showToast({
  98. title: '图片大小不能超过5M',
  99. icon: 'none'
  100. });
  101. }
  102. }
  103. if (validFiles.length > 0) {
  104. let newImageList = that.data.imageList.concat(validFiles);
  105. that.setData({
  106. imageList: newImageList
  107. });
  108. }
  109. }
  110. });
  111. },
  112. previewImage: function(e) {
  113. let index = e.currentTarget.dataset.index;
  114. wx.previewImage({
  115. current: this.data.imageList[index],
  116. urls: this.data.imageList
  117. });
  118. },
  119. deleteImage: function(e) {
  120. let index = e.currentTarget.dataset.index;
  121. let imageList = this.data.imageList;
  122. imageList.splice(index, 1);
  123. this.setData({
  124. imageList: imageList
  125. });
  126. },
  127. checkFormValidity: function() {
  128. const { contact, phone, address, repairType, description } = this.data;
  129. const isValid = contact && phone && address && repairType && description;
  130. this.setData({
  131. isFormValid: isValid
  132. });
  133. return isValid;
  134. },
  135. onInputChange: function(e) {
  136. const { field } = e.currentTarget.dataset;
  137. const { value } = e.detail;
  138. this.setData({
  139. [field]: value
  140. });
  141. this.checkFormValidity();
  142. },
  143. bindPickerChange: function(e) {
  144. this.checkFormValidity();
  145. },
  146. submitRepair: function() {
  147. if (!this.checkFormValidity()) {
  148. wx.showToast({
  149. title: '请填写完整信息',
  150. icon: 'none'
  151. });
  152. return;
  153. }
  154. if (!this.validatePhone(this.data.phone)) {
  155. wx.showToast({
  156. title: '请输入正确的手机号',
  157. icon: 'none'
  158. });
  159. return;
  160. }
  161. const submitData = {
  162. address: this.data.address,
  163. contact: this.data.contact,
  164. phone: this.data.phone,
  165. repairType: this.data.repairType,
  166. description: this.data.description,
  167. images: this.data.imageList
  168. };
  169. console.log('提交的数据:', submitData);
  170. wx.showLoading({
  171. title: '提交中...',
  172. });
  173. setTimeout(() => {
  174. wx.hideLoading();
  175. wx.showToast({
  176. icon: 'success',
  177. duration: 2000,
  178. success: function() {
  179. setTimeout(() => {
  180. wx.navigateTo({
  181. url: '/pages/baoxiuSuccess/baoxiuSuccess',
  182. });
  183. }, 2000);
  184. }
  185. });
  186. }, 1500);
  187. }
  188. });