baoxiudj.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. const app = getApp();
  2. Page({
  3. data: {
  4. address: app.globalData.currentAccountInfo.address,
  5. contact: '',
  6. phone: '',
  7. repairType: '',
  8. repairTypeValue: '',
  9. repairTypeMap: [
  10. {name: '换表', value: '1'},
  11. {name: '换前阀', value: '2'},
  12. {name: '换后阀', value: '3'},
  13. {name: '换前后阀', value: '4'},
  14. {name: '其他', value: '5'}
  15. ],
  16. description: '',
  17. imageList: [],
  18. showNotification: true,
  19. countDown: 3,
  20. isFormValid: false,
  21. isPreviewMode: false,
  22. replyTime: '',
  23. replyContent: '',
  24. id: '',
  25. mode: '',
  26. isReplied: false,
  27. formSubmitted: false,
  28. isSubmitting: false, // 新增保存标志位
  29. lastSubmitTime: 0, // 上次提交时间(通过防抖(Debounce)技术,限制用户在一定时间内只能提交一次。)
  30. },
  31. onLoad: function (options) {
  32. const isReplied = options.isReplied === 'true';
  33. if (options.mode === 'preview') {
  34. this.setData({
  35. isPreviewMode: true,
  36. showNotification: false,
  37. id: options.id,
  38. mode: options.mode,
  39. isReplied: isReplied
  40. });
  41. this.loadPreviewData(options.id);
  42. } else {
  43. this.startCountDown();
  44. this.setData({
  45. id: options.id || '',
  46. mode: options.mode || '',
  47. isReplied: isReplied
  48. });
  49. }
  50. },
  51. startCountDown: function () {
  52. let that = this;
  53. let timer = setInterval(function () {
  54. if (that.data.countDown > 0) {
  55. that.setData({
  56. countDown: that.data.countDown - 1
  57. });
  58. } else {
  59. clearInterval(timer);
  60. }
  61. }, 1000);
  62. },
  63. closeNotification: function () {
  64. if (this.data.countDown <= 0) {
  65. this.setData({
  66. showNotification: false
  67. });
  68. }
  69. },
  70. goBack: function () {
  71. wx.navigateBack();
  72. },
  73. inputContact: function (e) {
  74. this.setData({
  75. contact: e.detail.value
  76. });
  77. this.checkFormValidity();
  78. },
  79. inputPhone: function (e) {
  80. const value = e.detail.value;
  81. const phoneNumber = value.replace(/\D/g, '');
  82. this.setData({
  83. phone: phoneNumber
  84. });
  85. this.checkFormValidity();
  86. },
  87. validatePhone: function (phone) {
  88. const phoneReg = /^1[3-9]\d{9}$/;
  89. return phoneReg.test(phone);
  90. },
  91. showRepairTypeSelector: function () {
  92. let that = this;
  93. // 只显示名称列表
  94. const typeNames = this.data.repairTypeMap.map(item => item.name);
  95. wx.showActionSheet({
  96. itemList: typeNames,
  97. success: function (res) {
  98. const selectedType = that.data.repairTypeMap[res.tapIndex];
  99. that.setData({
  100. repairType: selectedType.name,
  101. repairTypeValue: selectedType.value
  102. });
  103. that.checkFormValidity();
  104. }
  105. });
  106. },
  107. inputDescription: function (e) {
  108. this.setData({
  109. description: e.detail.value
  110. });
  111. this.checkFormValidity();
  112. },
  113. chooseImage: function () {
  114. let that = this;
  115. if (that.data.imageList.length >= 10) {
  116. wx.showToast({
  117. title: '最多只能上传10张图片',
  118. icon: 'none'
  119. });
  120. return;
  121. }
  122. wx.chooseMedia({
  123. count: 10 - that.data.imageList.length,
  124. mediaType: ['image'],
  125. sourceType: ['album', 'camera'],
  126. sizeType: ['compressed'],
  127. success: function (res) {
  128. let tempFiles = res.tempFiles;
  129. let validFiles = [];
  130. for (let i = 0; i < tempFiles.length; i++) {
  131. const file = tempFiles[i];
  132. if (file.size <= 10 * 1024 * 1024) {
  133. validFiles.push(file);
  134. } else {
  135. wx.showToast({
  136. title: '图片大小不能超过10M',
  137. icon: 'none'
  138. });
  139. }
  140. }
  141. if (validFiles.length > 0) {
  142. let newImageList = that.data.imageList.concat(validFiles);
  143. that.setData({
  144. imageList: newImageList
  145. });
  146. }
  147. }
  148. });
  149. },
  150. previewImage: function (e) {
  151. let index = e.currentTarget.dataset.index;
  152. wx.previewImage({
  153. current: this.data.imageList[index],
  154. urls: this.data.imageList
  155. });
  156. },
  157. deleteImage: function (e) {
  158. let index = e.currentTarget.dataset.index;
  159. let imageList = this.data.imageList;
  160. imageList.splice(index, 1);
  161. this.setData({
  162. imageList: imageList
  163. });
  164. },
  165. checkFormValidity: function () {
  166. const {
  167. contact,
  168. phone,
  169. address,
  170. repairType,
  171. repairTypeValue,
  172. description
  173. } = this.data;
  174. const hasAddress = address && address.trim() !== '';
  175. const hasContact = contact && contact.trim() !== '';
  176. const hasPhone = phone && phone.trim() !== '';
  177. const hasValidPhone = this.validatePhone(phone);
  178. const hasRepairType = repairType && repairType.trim() !== '';
  179. const hasRepairTypeValue = repairTypeValue && repairTypeValue.trim() !== '';
  180. const hasDescription = description && description.trim() !== '';
  181. const isValid = hasAddress && hasContact && hasPhone && hasValidPhone && hasRepairType && hasRepairTypeValue && hasDescription;
  182. this.setData({
  183. isFormValid: isValid
  184. });
  185. return isValid;
  186. },
  187. onInputChange: function (e) {
  188. const {
  189. field
  190. } = e.currentTarget.dataset;
  191. const {
  192. value
  193. } = e.detail;
  194. this.setData({
  195. [field]: value
  196. });
  197. this.checkFormValidity();
  198. },
  199. bindPickerChange: function (e) {
  200. this.checkFormValidity();
  201. },
  202. submitRepair: function () {
  203. if (!this.checkFormValidity()) {
  204. wx.showToast({
  205. title: '请填写完整信息',
  206. icon: 'none'
  207. });
  208. return;
  209. }
  210. if (!this.validatePhone(this.data.phone)) {
  211. wx.showToast({
  212. title: '请输入正确的手机号',
  213. icon: 'none'
  214. });
  215. return;
  216. }
  217. const submitData = {
  218. address: this.data.address,
  219. contact: this.data.contact,
  220. phone: this.data.phone,
  221. repairType: this.data.repairType,
  222. repairTypeValue: this.data.repairTypeValue,
  223. description: this.data.description,
  224. images: this.data.imageList
  225. };
  226. console.log('提交的数据:', submitData);
  227. wx.showLoading({
  228. title: '提交中...',
  229. });
  230. setTimeout(() => {
  231. wx.hideLoading();
  232. wx.showToast({
  233. icon: 'success',
  234. duration: 2000,
  235. success: function () {
  236. setTimeout(() => {
  237. wx.navigateTo({
  238. url: '/pages/baoxiuSuccess/baoxiuSuccess',
  239. });
  240. }, 2000);
  241. }
  242. });
  243. }, 1500);
  244. },
  245. loadPreviewData: function (id) {
  246. wx.showLoading({
  247. title: '加载中...',
  248. });
  249. // 从上一个页面获取数据
  250. const pages = getCurrentPages();
  251. const prevPage = pages[pages.length - 2]; // 获取上一个页面
  252. if (prevPage && prevPage.data && prevPage.data.noticeList) {
  253. // 根据id查找对应的报修项
  254. const item = prevPage.data.noticeList.find(item => item.id == id);
  255. debugger
  256. if (item) {
  257. // 找到对应的报修类型名称
  258. let repairTypeName = '';
  259. const repairTypeValue = item.repairtype || '';
  260. const repairTypeItem = this.data.repairTypeMap.find(type => type.name === repairTypeValue);
  261. if (repairTypeItem) {
  262. repairTypeName = repairTypeItem.name;
  263. }
  264. // 格式化时间
  265. const formatTime = (timeString) => {
  266. if (!timeString) return ''; // 如果时间为空,返回空字符串
  267. const date = new Date(timeString);
  268. const year = date.getFullYear();
  269. const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份补零
  270. const day = String(date.getDate()).padStart(2, '0'); // 日期补零
  271. return `${year}-${month}-${day}`;
  272. };
  273. this.setData({
  274. address: item.address || '',
  275. contact: item.contact || '',
  276. phone: item.contactnumber || '',
  277. repairType: repairTypeName,
  278. repairTypeValue: repairTypeValue,
  279. description: item.faultdescription || '',
  280. imageList: item.attachments || [],
  281. replyTime: item.isReplied ? formatTime(item.repairtime) : '',
  282. replyContent: item.isReplied ? item.remark : ''
  283. });
  284. }
  285. }
  286. wx.hideLoading();
  287. },
  288. submitForm: function () {
  289. // 如果正在提交中,直接返回
  290. if (this.data.isSubmitting) {
  291. return;
  292. }
  293. if (!this.data.address || this.data.address.trim() === '') {
  294. wx.showToast({
  295. title: '请填写地址',
  296. icon: 'none'
  297. });
  298. this.setData({
  299. isSubmitting: false
  300. });
  301. return;
  302. }
  303. if (!this.data.contact || this.data.contact.trim() === '') {
  304. wx.showToast({
  305. title: '请填写联系人',
  306. icon: 'none'
  307. });
  308. this.setData({
  309. isSubmitting: false
  310. });
  311. return;
  312. }
  313. if (!this.data.phone || this.data.phone.trim() === '') {
  314. wx.showToast({
  315. title: '请填写联系电话',
  316. icon: 'none'
  317. });
  318. this.setData({
  319. isSubmitting: false
  320. });
  321. return;
  322. }
  323. if (!this.validatePhone(this.data.phone)) {
  324. wx.showToast({
  325. title: '请输入正确的手机号',
  326. icon: 'none'
  327. });
  328. this.setData({
  329. isSubmitting: false
  330. });
  331. return;
  332. }
  333. if (!this.data.repairType || this.data.repairType.trim() === '') {
  334. wx.showToast({
  335. title: '请选择报修类型',
  336. icon: 'none'
  337. });
  338. this.setData({
  339. isSubmitting: false
  340. });
  341. return;
  342. }
  343. if (!this.data.repairTypeValue || this.data.repairTypeValue.trim() === '') {
  344. wx.showToast({
  345. title: '请选择报修类型',
  346. icon: 'none'
  347. });
  348. this.setData({
  349. isSubmitting: false
  350. });
  351. return;
  352. }
  353. if (!this.data.description || this.data.description.trim() === '') {
  354. wx.showToast({
  355. title: '请填写故障说明',
  356. icon: 'none'
  357. });
  358. this.setData({
  359. isSubmitting: false
  360. });
  361. return;
  362. }
  363. const now = Date.now();
  364. const lastSubmitTime = this.data.lastSubmitTime;
  365. // 如果距离上次提交时间小于 2 秒,直接返回
  366. if (now - lastSubmitTime < 2000) {
  367. // wx.showToast({
  368. // title: '请勿重复提交',
  369. // icon: 'none',
  370. // });
  371. return;
  372. }
  373. // 更新上次提交时间
  374. this.setData({
  375. lastSubmitTime: now,
  376. });
  377. const fileManager = wx.getFileSystemManager();
  378. this.data.imageList.map(imgInfo => {
  379. const base64 = fileManager.readFileSync(imgInfo.tempFilePath, 'base64');
  380. imgInfo.base64 = base64;
  381. return imgInfo;
  382. })
  383. const submitData = {
  384. address: this.data.address,
  385. contact: this.data.contact,
  386. phone: this.data.phone,
  387. repairType: this.data.repairTypeValue,
  388. description: this.data.description,
  389. images: this.data.imageList,
  390. userName: app.globalData.currentAccountInfo.username,
  391. userNum: app.globalData.currentAccountInfo.usernumber
  392. };
  393. // 设置正在提交中 防止重复点击提交按钮
  394. this.setData({
  395. isSubmitting: true,
  396. });
  397. console.log('提交的数据:', submitData);
  398. wx.showLoading({
  399. title: '提交中...',
  400. mask: true
  401. });
  402. const that = this;
  403. wx.request({
  404. url: app.globalData.interfaceUrls.repairRegistration,
  405. method: 'POST',
  406. header: {
  407. 'content-type': 'application/json', // 默认值
  408. 'token': app.globalData.userWxInfo.token,
  409. 'source': "wc",
  410. '!SAAS_LOGIN_TOKEN_!': app.globalData.currentAccountInfo.dsKey
  411. },
  412. data: submitData,
  413. success(res) {
  414. wx.hideLoading();
  415. if (res.data.code == '200') {
  416. wx.navigateTo({
  417. url: '/pages/baoxiuSuccess/baoxiuSuccess',
  418. });
  419. }
  420. that.setData({
  421. isSubmitting: false
  422. });
  423. },
  424. fail(error) {
  425. wx.hideLoading();
  426. wx.showToast({
  427. title: '登记失败,请稍后再试',
  428. icon: 'none'
  429. });
  430. that.setData({
  431. isSubmitting: false
  432. });
  433. },
  434. complete: () => {
  435. this.setData({
  436. isSubmitting: false, // 提交完成,重置标志位,可继续提交
  437. formSubmitted: true // 在提交成功后设置标记,返回将重置表单
  438. });
  439. },
  440. })
  441. },
  442. inputAddress: function (e) {
  443. this.setData({
  444. address: e.detail.value
  445. });
  446. },
  447. onShow: function () {
  448. // 检查是否是从成功页面返回
  449. if (this.data.formSubmitted) {
  450. // 重置表单数据
  451. this.resetForm();
  452. // 重置提交状态标记
  453. this.setData({
  454. formSubmitted: false
  455. });
  456. }
  457. },
  458. // 添加重置表单的方法
  459. resetForm: function () {
  460. this.setData({
  461. contact: '',
  462. phone: '',
  463. repairType: '',
  464. repairTypeValue: '',
  465. description: '',
  466. imageList: []
  467. });
  468. },
  469. });