tousujianyi.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. const app = getApp();
  2. Page({
  3. data: {
  4. contact: '',
  5. phone: '',
  6. description: '',
  7. imageList: [],
  8. showNotification: true,
  9. countDown: 3,
  10. isFormValid: false,
  11. category: '投诉',
  12. isPreviewMode: false,
  13. title: '',
  14. content: '',
  15. replyTime: '',
  16. replyContent: '',
  17. formSubmitted: false,
  18. isSubmitting: false, // 新增保存标志位
  19. lastSubmitTime: 0, // 上次提交时间(通过防抖(Debounce)技术,限制用户在一定时间内只能提交一次。)
  20. },
  21. onLoad: function (options) {
  22. this.startCountDown();
  23. // 检查是否是预览模式
  24. if (options.mode === 'preview') {
  25. const id = options.id;
  26. // 获取数据
  27. this.getDataById(id);
  28. // 设置为预览模式
  29. this.setData({
  30. isPreviewMode: true
  31. });
  32. } else {
  33. // 非预览模式才检查表单有效性
  34. this.checkFormValidity();
  35. }
  36. },
  37. onShow: function () {
  38. // 检查是否是从成功页面返回
  39. if (this.data.formSubmitted) {
  40. // 重置表单数据
  41. this.resetForm();
  42. // 重置提交状态标记
  43. this.setData({
  44. formSubmitted: false
  45. });
  46. }
  47. },
  48. startCountDown: function () {
  49. let that = this;
  50. let timer = setInterval(function () {
  51. if (that.data.countDown > 0) {
  52. that.setData({
  53. countDown: that.data.countDown - 1
  54. });
  55. } else {
  56. clearInterval(timer);
  57. }
  58. }, 1000);
  59. },
  60. goBack: function () {
  61. wx.navigateBack();
  62. },
  63. radioChange: function (e) {
  64. this.setData({
  65. category: e.detail.value
  66. });
  67. this.checkFormValidity();
  68. },
  69. inputContact: function (e) {
  70. this.setData({
  71. contact: e.detail.value
  72. });
  73. this.checkFormValidity();
  74. },
  75. inputPhone: function (e) {
  76. const value = e.detail.value;
  77. const phoneNumber = value.replace(/\D/g, '');
  78. this.setData({
  79. phone: phoneNumber
  80. });
  81. this.checkFormValidity();
  82. },
  83. validatePhone: function (phone) {
  84. const phoneReg = /^1[3-9]\d{9}$/;
  85. return phoneReg.test(phone);
  86. },
  87. inputDescription: function (e) {
  88. this.setData({
  89. description: e.detail.value
  90. });
  91. this.checkFormValidity();
  92. },
  93. chooseImage: function () {
  94. let that = this;
  95. if (that.data.imageList.length >= 10) {
  96. wx.showToast({
  97. title: '最多只能上传10张图片',
  98. icon: 'none'
  99. });
  100. return;
  101. }
  102. wx.chooseMedia({
  103. count: 10 - that.data.imageList.length,
  104. mediaType: ['image'],
  105. sourceType: ['album', 'camera'],
  106. sizeType: ['compressed'],
  107. success: function (res) {
  108. let tempFiles = res.tempFiles;
  109. let validFiles = [];
  110. for (let i = 0; i < tempFiles.length; i++) {
  111. const file = tempFiles[i];
  112. if (file.size <= 10 * 1024 * 1024) {
  113. validFiles.push(file);
  114. } else {
  115. wx.showToast({
  116. title: '图片大小不能超过10M',
  117. icon: 'none'
  118. });
  119. }
  120. }
  121. if (validFiles.length > 0) {
  122. let newImageList = that.data.imageList.concat(validFiles);
  123. that.setData({
  124. imageList: newImageList
  125. });
  126. }
  127. }
  128. });
  129. },
  130. previewImage: function (e) {
  131. let index = e.currentTarget.dataset.index;
  132. wx.previewImage({
  133. current: this.data.imageList[index],
  134. urls: this.data.imageList
  135. });
  136. },
  137. deleteImage: function (e) {
  138. let index = e.currentTarget.dataset.index;
  139. let imageList = this.data.imageList;
  140. imageList.splice(index, 1);
  141. this.setData({
  142. imageList: imageList
  143. });
  144. },
  145. checkFormValidity: function () {
  146. const {
  147. contact,
  148. phone,
  149. description,
  150. category
  151. } = this.data;
  152. // 只检查必填项:联系人、联系电话和内容说明
  153. const isValid = contact.trim() !== '' && this.validatePhone(phone) && description.trim() !== '';
  154. this.setData({
  155. isFormValid: isValid
  156. });
  157. return isValid;
  158. },
  159. onInputChange: function (e) {
  160. const {
  161. field
  162. } = e.currentTarget.dataset;
  163. const {
  164. value
  165. } = e.detail;
  166. this.setData({
  167. [field]: value
  168. });
  169. this.checkFormValidity();
  170. },
  171. bindPickerChange: function (e) {
  172. this.checkFormValidity();
  173. },
  174. // 添加submitForm方法
  175. submitForm: function () {
  176. // 如果正在提交中,直接返回
  177. if (this.data.isSubmitting) {
  178. return;
  179. }
  180. if (!this.checkFormValidity()) {
  181. let errorMsg = '';
  182. if (!this.data.contact.trim()) {
  183. errorMsg = '请填写联系人';
  184. } else if (!this.validatePhone(this.data.phone)) {
  185. errorMsg = '请输入正确的联系电话';
  186. } else if (!this.data.description.trim()) {
  187. errorMsg = '请填写内容说明';
  188. }
  189. wx.showToast({
  190. title: errorMsg,
  191. icon: 'none'
  192. });
  193. return;
  194. }
  195. const now = Date.now();
  196. const lastSubmitTime = this.data.lastSubmitTime;
  197. // 如果距离上次提交时间小于 2 秒,直接返回
  198. if (now - lastSubmitTime < 2000) {
  199. // wx.showToast({
  200. // title: '请勿重复提交',
  201. // icon: 'none',
  202. // });
  203. return;
  204. }
  205. // 更新上次提交时间
  206. this.setData({
  207. lastSubmitTime: now,
  208. });
  209. const fileManager = wx.getFileSystemManager();
  210. this.data.imageList.map(imgInfo => {
  211. const base64 = fileManager.readFileSync(imgInfo.tempFilePath, 'base64');
  212. imgInfo.base64 = base64;
  213. return imgInfo;
  214. })
  215. // 构建提交数据
  216. const submitData = {
  217. category: this.data.category,
  218. contact: this.data.contact,
  219. phone: this.data.phone,
  220. description: this.data.description,
  221. images: this.data.imageList
  222. };
  223. debugger
  224. // 设置正在提交中 防止重复点击提交按钮
  225. this.setData({
  226. isSubmitting: true,
  227. });
  228. console.log('提交的数据:', submitData);
  229. wx.showLoading({
  230. title: '提交中...',
  231. });
  232. wx.request({
  233. url: app.globalData.interfaceUrls.feedback,
  234. method: 'POST',
  235. header: {
  236. 'content-type': 'application/json', // 默认值
  237. 'token': app.globalData.userWxInfo.token,
  238. 'source': "wc",
  239. '!SAAS_LOGIN_TOKEN_!': app.globalData.currentAccountInfo.dsKey
  240. },
  241. data: submitData,
  242. success(res) {
  243. wx.hideLoading();
  244. if (res.data.code == '200') {
  245. wx.navigateTo({
  246. url: '/pages/tousujianyiSuccess/tousujianyiSuccess',
  247. });
  248. }
  249. },
  250. fail(error) {
  251. wx.hideLoading()
  252. utils.simleInfo('登记失败,请稍后再试')
  253. },
  254. complete: () => {
  255. this.setData({
  256. isSubmitting: false, // 提交完成,重置标志位,可继续提交
  257. formSubmitted: true // 在提交成功后设置标记,返回将重置表单
  258. });
  259. },
  260. })
  261. // 在提交成功后设置标记,返回将重置表单
  262. // this.setData({
  263. // formSubmitted: true
  264. // });
  265. },
  266. submitRepair: function () {
  267. this.submitForm();
  268. },
  269. // 根据ID获取数据
  270. getDataById: function (id) {
  271. // 这里应该是从服务器获取数据
  272. // 但为了演示,我们从本地数据中获取
  273. const pages = getCurrentPages();
  274. const prevPage = pages[pages.length - 2]; // 获取上一个页面
  275. if (prevPage && prevPage.data.noticeList) {
  276. const item = prevPage.data.noticeList.find(item => item.id == id);
  277. debugger
  278. if (item) {
  279. const formatTime = (timeString) => {
  280. if (!timeString) return ''; // 如果时间为空,返回空字符串
  281. const date = new Date(timeString);
  282. const year = date.getFullYear();
  283. const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份补零
  284. const day = String(date.getDate()).padStart(2, '0'); // 日期补零
  285. return `${year}-${month}-${day}`;
  286. };
  287. this.setData({
  288. category: item.category || '投诉',
  289. description: item.description || '',
  290. contact: item.feedbackperson || '',
  291. description: item.replynote || '',
  292. phone: item.contactnumber || '',
  293. imageList: item.attachments || [],
  294. replyTime: formatTime(item.replytime) || '',
  295. replyContent: item.replycontent || ''
  296. });
  297. }
  298. }
  299. },
  300. // 添加重置表单的方法
  301. resetForm: function () {
  302. this.setData({
  303. contact: '',
  304. phone: '',
  305. description: '',
  306. imageList: []
  307. });
  308. }
  309. });