baoxiudj.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. const app=getApp();
  2. Page({
  3. data: {
  4. address: app.globalData.currentAccountInfo.address,
  5. contact: '',
  6. phone: '',
  7. repairType: '',
  8. description: '',
  9. imageList: [],
  10. showNotification: true,
  11. countDown: 3,
  12. isFormValid: false
  13. },
  14. onLoad: function(options) {
  15. this.startCountDown();
  16. },
  17. startCountDown: function() {
  18. let that = this;
  19. let timer = setInterval(function() {
  20. if (that.data.countDown > 0) {
  21. that.setData({
  22. countDown: that.data.countDown - 1
  23. });
  24. } else {
  25. clearInterval(timer);
  26. }
  27. }, 1000);
  28. },
  29. closeNotification: function() {
  30. if (this.data.countDown <= 0) {
  31. this.setData({
  32. showNotification: false
  33. });
  34. }
  35. },
  36. goBack: function() {
  37. wx.navigateBack();
  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. showRepairTypeSelector: function() {
  58. let that = this;
  59. wx.showActionSheet({
  60. itemList: ['水管漏水', '水表故障', '水龙头故障', '其他问题'],
  61. success: function(res) {
  62. const types = ['水管漏水', '水表故障', '水龙头故障', '其他问题'];
  63. that.setData({
  64. repairType: types[res.tapIndex]
  65. });
  66. that.checkFormValidity();
  67. }
  68. });
  69. },
  70. inputDescription: function(e) {
  71. this.setData({
  72. description: e.detail.value
  73. });
  74. this.checkFormValidity();
  75. },
  76. chooseImage: function() {
  77. let that = this;
  78. if (that.data.imageList.length >= 10) {
  79. wx.showToast({
  80. title: '最多只能上传10张图片',
  81. icon: 'none'
  82. });
  83. return;
  84. }
  85. wx.chooseMedia({
  86. count: 3 - that.data.imageList.length,
  87. mediaType: ['image'],
  88. sourceType: ['album', 'camera'],
  89. sizeType: ['compressed'],
  90. success: function(res) {
  91. let tempFiles = res.tempFiles;
  92. let validFiles = [];
  93. debugger;
  94. for (let i = 0; i < tempFiles.length; i++) {
  95. const file = tempFiles[i];
  96. if (file.size <= 5 * 1024 * 1024) {
  97. validFiles.push(file);
  98. } else {
  99. wx.showToast({
  100. title: '图片大小不能超过5M',
  101. icon: 'none'
  102. });
  103. }
  104. }
  105. if (validFiles.length > 0) {
  106. let newImageList = that.data.imageList.concat(validFiles);
  107. that.setData({
  108. imageList: newImageList
  109. });
  110. }
  111. }
  112. });
  113. },
  114. previewImage: function(e) {
  115. let index = e.currentTarget.dataset.index;
  116. wx.previewImage({
  117. current: this.data.imageList[index],
  118. urls: this.data.imageList
  119. });
  120. },
  121. deleteImage: function(e) {
  122. let index = e.currentTarget.dataset.index;
  123. let imageList = this.data.imageList;
  124. imageList.splice(index, 1);
  125. this.setData({
  126. imageList: imageList
  127. });
  128. },
  129. checkFormValidity: function() {
  130. const { contact, phone, address, repairType, description } = this.data;
  131. const isValid = contact && phone && address && repairType && description;
  132. this.setData({
  133. isFormValid: isValid
  134. });
  135. return isValid;
  136. },
  137. onInputChange: function(e) {
  138. const { field } = e.currentTarget.dataset;
  139. const { value } = e.detail;
  140. this.setData({
  141. [field]: value
  142. });
  143. this.checkFormValidity();
  144. },
  145. bindPickerChange: function(e) {
  146. this.checkFormValidity();
  147. },
  148. submitRepair: function() {
  149. if (!this.checkFormValidity()) {
  150. wx.showToast({
  151. title: '请填写完整信息',
  152. icon: 'none'
  153. });
  154. return;
  155. }
  156. if (!this.validatePhone(this.data.phone)) {
  157. wx.showToast({
  158. title: '请输入正确的手机号',
  159. icon: 'none'
  160. });
  161. return;
  162. }
  163. const fileManager = wx.getFileSystemManager();
  164. this.data.imageList.map(imgInfo=>{
  165. const base64 = fileManager.readFileSync(imgInfo.tempFilePath, 'base64');
  166. imgInfo.base64=base64;
  167. return imgInfo;
  168. })
  169. const submitData = {
  170. address: this.data.address,
  171. contact: this.data.contact,
  172. phone: this.data.phone,
  173. repairType: this.data.repairType,
  174. description: this.data.description,
  175. images: this.data.imageList
  176. };
  177. console.log('提交的数据:', submitData);
  178. wx.showLoading({
  179. title: '提交中...',
  180. });
  181. wx.request({
  182. url: app.globalData.interfaceUrls.repairRegistration,
  183. method: 'POST',
  184. header: {
  185. 'content-type': 'application/json', // 默认值
  186. 'token': app.globalData.userWxInfo.token,
  187. 'source': "wc",
  188. '!SAAS_LOGIN_TOKEN_!': app.globalData.currentAccountInfo.dsKey
  189. },
  190. data:submitData,
  191. success(res) {
  192. wx.hideLoading();
  193. if(res.data.code=='200'){
  194. wx.navigateTo({
  195. url: '/pages/baoxiuSuccess/baoxiuSuccess',
  196. });
  197. }
  198. },
  199. fail(error) {
  200. wx.hideLoading()
  201. utils.simleInfo('登记失败,请稍后再试')
  202. }
  203. })
  204. }
  205. });