baoxiudj.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. isPreviewMode: false,
  14. replyTime: '',
  15. replyContent: '',
  16. id: '',
  17. mode: '',
  18. isReplied: false,
  19. },
  20. onLoad: function(options) {
  21. const isReplied = options.isReplied === 'true';
  22. if (options.mode === 'preview') {
  23. this.setData({
  24. isPreviewMode: true,
  25. showNotification: false,
  26. id: options.id,
  27. mode: options.mode,
  28. isReplied: isReplied
  29. });
  30. this.loadPreviewData(options.id);
  31. } else {
  32. this.startCountDown();
  33. this.setData({
  34. id: options.id || '',
  35. mode: options.mode || '',
  36. isReplied: isReplied
  37. });
  38. }
  39. },
  40. startCountDown: function() {
  41. let that = this;
  42. let timer = setInterval(function() {
  43. if (that.data.countDown > 0) {
  44. that.setData({
  45. countDown: that.data.countDown - 1
  46. });
  47. } else {
  48. clearInterval(timer);
  49. }
  50. }, 1000);
  51. },
  52. closeNotification: function() {
  53. if (this.data.countDown <= 0) {
  54. this.setData({
  55. showNotification: false
  56. });
  57. }
  58. },
  59. goBack: function() {
  60. wx.navigateBack();
  61. },
  62. inputContact: function(e) {
  63. this.setData({
  64. contact: e.detail.value
  65. });
  66. this.checkFormValidity();
  67. },
  68. inputPhone: function(e) {
  69. const value = e.detail.value;
  70. const phoneNumber = value.replace(/\D/g, '');
  71. this.setData({
  72. phone: phoneNumber
  73. });
  74. this.checkFormValidity();
  75. },
  76. validatePhone: function(phone) {
  77. const phoneReg = /^1[3-9]\d{9}$/;
  78. return phoneReg.test(phone);
  79. },
  80. showRepairTypeSelector: function() {
  81. let that = this;
  82. wx.showActionSheet({
  83. itemList: ['水管漏水', '水表故障', '水龙头故障', '其他问题'],
  84. success: function(res) {
  85. const types = ['水管漏水', '水表故障', '水龙头故障', '其他问题'];
  86. that.setData({
  87. repairType: types[res.tapIndex]
  88. });
  89. that.checkFormValidity();
  90. }
  91. });
  92. },
  93. inputDescription: function(e) {
  94. this.setData({
  95. description: e.detail.value
  96. });
  97. this.checkFormValidity();
  98. },
  99. chooseImage: function() {
  100. let that = this;
  101. if (that.data.imageList.length >= 10) {
  102. wx.showToast({
  103. title: '最多只能上传10张图片',
  104. icon: 'none'
  105. });
  106. return;
  107. }
  108. wx.chooseMedia({
  109. count: 3 - that.data.imageList.length,
  110. mediaType: ['image'],
  111. sourceType: ['album', 'camera'],
  112. sizeType: ['compressed'],
  113. success: function(res) {
  114. let tempFiles = res.tempFiles;
  115. let validFiles = [];
  116. debugger;
  117. for (let i = 0; i < tempFiles.length; i++) {
  118. const file = tempFiles[i];
  119. if (file.size <= 5 * 1024 * 1024) {
  120. validFiles.push(file);
  121. } else {
  122. wx.showToast({
  123. title: '图片大小不能超过5M',
  124. icon: 'none'
  125. });
  126. }
  127. }
  128. if (validFiles.length > 0) {
  129. let newImageList = that.data.imageList.concat(validFiles);
  130. that.setData({
  131. imageList: newImageList
  132. });
  133. }
  134. }
  135. });
  136. },
  137. previewImage: function(e) {
  138. let index = e.currentTarget.dataset.index;
  139. wx.previewImage({
  140. current: this.data.imageList[index],
  141. urls: this.data.imageList
  142. });
  143. },
  144. deleteImage: function(e) {
  145. let index = e.currentTarget.dataset.index;
  146. let imageList = this.data.imageList;
  147. imageList.splice(index, 1);
  148. this.setData({
  149. imageList: imageList
  150. });
  151. },
  152. checkFormValidity: function() {
  153. const { contact, phone, address, repairType, description } = this.data;
  154. const isValid = contact && phone && address && repairType && description;
  155. this.setData({
  156. isFormValid: isValid
  157. });
  158. return isValid;
  159. },
  160. onInputChange: function(e) {
  161. const { field } = e.currentTarget.dataset;
  162. const { value } = e.detail;
  163. this.setData({
  164. [field]: value
  165. });
  166. this.checkFormValidity();
  167. },
  168. bindPickerChange: function(e) {
  169. this.checkFormValidity();
  170. },
  171. submitRepair: function() {
  172. if (!this.checkFormValidity()) {
  173. wx.showToast({
  174. title: '请填写完整信息',
  175. icon: 'none'
  176. });
  177. return;
  178. }
  179. if (!this.validatePhone(this.data.phone)) {
  180. wx.showToast({
  181. title: '请输入正确的手机号',
  182. icon: 'none'
  183. });
  184. return;
  185. }
  186. const submitData = {
  187. address: this.data.address,
  188. contact: this.data.contact,
  189. phone: this.data.phone,
  190. repairType: this.data.repairType,
  191. description: this.data.description,
  192. images: this.data.imageList
  193. };
  194. console.log('提交的数据:', submitData);
  195. wx.showLoading({
  196. title: '提交中...',
  197. });
  198. setTimeout(() => {
  199. wx.hideLoading();
  200. wx.showToast({
  201. icon: 'success',
  202. duration: 2000,
  203. success: function() {
  204. setTimeout(() => {
  205. wx.navigateTo({
  206. url: '/pages/baoxiuSuccess/baoxiuSuccess',
  207. });
  208. }, 2000);
  209. }
  210. });
  211. }, 1500);
  212. },
  213. loadPreviewData: function(id) {
  214. wx.showLoading({
  215. title: '加载中...',
  216. });
  217. // 从上一个页面获取数据
  218. const pages = getCurrentPages();
  219. const prevPage = pages[pages.length - 2]; // 获取上一个页面
  220. if (prevPage && prevPage.data && prevPage.data.noticeList) {
  221. // 根据id查找对应的报修项
  222. const item = prevPage.data.noticeList.find(item => item.id == id);
  223. if (item) {
  224. this.setData({
  225. address: item.address || '',
  226. contact: item.contact || '',
  227. phone: item.phone || '',
  228. repairType: item.type || '',
  229. description: item.content || '',
  230. imageList: item.hasAttachment ? item.attachments : [],
  231. replyTime: item.isReplied ? item.replyTime : '',
  232. replyContent: item.isReplied ? item.replyContent : ''
  233. });
  234. }
  235. }
  236. wx.hideLoading();
  237. },
  238. submitForm: function() {
  239. if (this.data.isPreviewMode) {
  240. return;
  241. }
  242. if (!this.checkFormValidity()) {
  243. wx.showToast({
  244. title: '请填写完整信息',
  245. icon: 'none'
  246. });
  247. return;
  248. }
  249. if (!this.validatePhone(this.data.phone)) {
  250. wx.showToast({
  251. title: '请输入正确的手机号',
  252. icon: 'none'
  253. });
  254. return;
  255. }
  256. const fileManager = wx.getFileSystemManager();
  257. this.data.imageList.map(imgInfo=>{
  258. const base64 = fileManager.readFileSync(imgInfo.tempFilePath, 'base64');
  259. imgInfo.base64=base64;
  260. return imgInfo;
  261. })
  262. const submitData = {
  263. address: this.data.address,
  264. contact: this.data.contact,
  265. phone: this.data.phone,
  266. repairType: this.data.repairType,
  267. description: this.data.description,
  268. images: this.data.imageList
  269. };
  270. console.log('提交的数据:', submitData);
  271. wx.showLoading({
  272. title: '提交中...',
  273. });
  274. wx.request({
  275. url: app.globalData.interfaceUrls.repairRegistration,
  276. method: 'POST',
  277. header: {
  278. 'content-type': 'application/json', // 默认值
  279. 'token': app.globalData.userWxInfo.token,
  280. 'source': "wc",
  281. '!SAAS_LOGIN_TOKEN_!': app.globalData.currentAccountInfo.dsKey
  282. },
  283. data:submitData,
  284. success(res) {
  285. wx.hideLoading();
  286. if(res.data.code=='200'){
  287. wx.navigateTo({
  288. url: '/pages/baoxiuSuccess/baoxiuSuccess',
  289. });
  290. }
  291. },
  292. fail(error) {
  293. wx.hideLoading()
  294. utils.simleInfo('登记失败,请稍后再试')
  295. }
  296. })
  297. }
  298. });