baoxiudj.js 10 KB

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