baoxiudj.js 9.0 KB

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