tousujianyi.js 5.2 KB

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