tousujianyi.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. categoryValue: '1',
  13. isPreviewMode: false,
  14. title: '',
  15. content: '',
  16. replyTime: '',
  17. replyContent: '',
  18. formSubmitted: false,
  19. isSubmitting: false,
  20. lastSubmitTime: 0,
  21. categoryMap: [
  22. {name: '投诉', value: '1'},
  23. {name: '建议', value: '2'}
  24. ],
  25. },
  26. onLoad: function (options) {
  27. this.startCountDown();
  28. // 检查是否是预览模式
  29. if (options.mode === 'preview') {
  30. const id = options.id;
  31. // 获取数据
  32. this.getDataById(id);
  33. // 设置为预览模式
  34. this.setData({
  35. isPreviewMode: true
  36. });
  37. } else {
  38. // 非预览模式才检查表单有效性
  39. this.checkFormValidity();
  40. }
  41. },
  42. onShow: function () {
  43. // 检查是否是从成功页面返回
  44. if (this.data.formSubmitted) {
  45. // 重置表单数据
  46. this.resetForm();
  47. // 重置提交状态标记
  48. this.setData({
  49. formSubmitted: false
  50. });
  51. }
  52. },
  53. startCountDown: function () {
  54. let that = this;
  55. let timer = setInterval(function () {
  56. if (that.data.countDown > 0) {
  57. that.setData({
  58. countDown: that.data.countDown - 1
  59. });
  60. } else {
  61. clearInterval(timer);
  62. }
  63. }, 1000);
  64. },
  65. goBack: function () {
  66. wx.navigateBack();
  67. },
  68. radioChange: function (e) {
  69. const value = e.detail.value;
  70. const selectedCategory = this.data.categoryMap.find(item => item.value === value || item.name === value);
  71. this.setData({
  72. category: selectedCategory.name,
  73. categoryValue: selectedCategory.value
  74. });
  75. this.checkFormValidity();
  76. },
  77. inputContact: function (e) {
  78. this.setData({
  79. contact: e.detail.value
  80. });
  81. this.checkFormValidity();
  82. },
  83. inputPhone: function (e) {
  84. const value = e.detail.value;
  85. const phoneNumber = value.replace(/\D/g, '');
  86. this.setData({
  87. phone: phoneNumber
  88. });
  89. this.checkFormValidity();
  90. },
  91. validatePhone: function (phone) {
  92. const phoneReg = /^1[3-9]\d{9}$/;
  93. return phoneReg.test(phone);
  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. description,
  158. category
  159. } = this.data;
  160. // 只检查必填项:联系人、联系电话和内容说明
  161. const isValid = contact.trim() !== '' && this.validatePhone(phone) && description.trim() !== '';
  162. this.setData({
  163. isFormValid: isValid
  164. });
  165. return isValid;
  166. },
  167. onInputChange: function (e) {
  168. const {
  169. field
  170. } = e.currentTarget.dataset;
  171. const {
  172. value
  173. } = e.detail;
  174. this.setData({
  175. [field]: value
  176. });
  177. this.checkFormValidity();
  178. },
  179. bindPickerChange: function (e) {
  180. this.checkFormValidity();
  181. },
  182. // 添加submitForm方法
  183. submitForm: function () {
  184. // 如果正在提交中,直接返回
  185. if (this.data.isSubmitting) {
  186. return;
  187. }
  188. if (!this.checkFormValidity()) {
  189. let errorMsg = '';
  190. if (!this.data.contact.trim()) {
  191. errorMsg = '请填写联系人';
  192. } else if (!this.validatePhone(this.data.phone)) {
  193. errorMsg = '请输入正确的联系电话';
  194. } else if (!this.data.description.trim()) {
  195. errorMsg = '请填写内容说明';
  196. }
  197. wx.showToast({
  198. title: errorMsg,
  199. icon: 'none'
  200. });
  201. return;
  202. }
  203. const now = Date.now();
  204. const lastSubmitTime = this.data.lastSubmitTime;
  205. // 如果距离上次提交时间小于 2 秒,直接返回
  206. if (now - lastSubmitTime < 2000) {
  207. // wx.showToast({
  208. // title: '请勿重复提交',
  209. // icon: 'none',
  210. // });
  211. return;
  212. }
  213. // 更新上次提交时间
  214. this.setData({
  215. lastSubmitTime: now,
  216. });
  217. const fileManager = wx.getFileSystemManager();
  218. this.data.imageList.map(imgInfo => {
  219. const base64 = fileManager.readFileSync(imgInfo.tempFilePath, 'base64');
  220. imgInfo.base64 = base64;
  221. return imgInfo;
  222. })
  223. const submitData = {
  224. category: this.data.categoryValue,
  225. contact: this.data.contact,
  226. phone: this.data.phone,
  227. description: this.data.description,
  228. images: this.data.imageList
  229. };
  230. debugger
  231. // 设置正在提交中 防止重复点击提交按钮
  232. this.setData({
  233. isSubmitting: true,
  234. });
  235. console.log('提交的数据:', submitData);
  236. wx.showLoading({
  237. title: '提交中...',
  238. });
  239. wx.request({
  240. url: app.globalData.interfaceUrls.feedback,
  241. method: 'POST',
  242. header: {
  243. 'content-type': 'application/json', // 默认值
  244. 'token': app.globalData.userWxInfo.token,
  245. 'source': "wc",
  246. '!SAAS_LOGIN_TOKEN_!': app.globalData.currentAccountInfo.dsKey
  247. },
  248. data: submitData,
  249. success(res) {
  250. wx.hideLoading();
  251. if (res.data.code == '200') {
  252. wx.navigateTo({
  253. url: '/pages/tousujianyiSuccess/tousujianyiSuccess',
  254. });
  255. }
  256. },
  257. fail(error) {
  258. wx.hideLoading()
  259. utils.simleInfo('登记失败,请稍后再试')
  260. },
  261. complete: () => {
  262. this.setData({
  263. isSubmitting: false, // 提交完成,重置标志位,可继续提交
  264. formSubmitted: true // 在提交成功后设置标记,返回将重置表单
  265. });
  266. },
  267. })
  268. // 在提交成功后设置标记,返回将重置表单
  269. // this.setData({
  270. // formSubmitted: true
  271. // });
  272. },
  273. submitRepair: function () {
  274. this.submitForm();
  275. },
  276. // 根据ID获取数据
  277. getDataById: function (id) {
  278. // 这里应该是从服务器获取数据
  279. // 但为了演示,我们从本地数据中获取
  280. const pages = getCurrentPages();
  281. const prevPage = pages[pages.length - 2]; // 获取上一个页面
  282. if (prevPage && prevPage.data.noticeList) {
  283. const item = prevPage.data.noticeList.find(item => item.id == id);
  284. if (item) {
  285. const formatTime = (timeString) => {
  286. if (!timeString) return ''; // 如果时间为空,返回空字符串
  287. const date = new Date(timeString);
  288. const year = date.getFullYear();
  289. const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份补零
  290. const day = String(date.getDate()).padStart(2, '0'); // 日期补零
  291. return `${year}-${month}-${day}`;
  292. };
  293. let categoryName = '投诉';
  294. const categoryItem = this.data.categoryMap.find(cat => cat.value === item.category);
  295. if (categoryItem) {
  296. categoryName = categoryItem.name;
  297. }
  298. this.setData({
  299. category: categoryName,
  300. categoryValue: item.category || '1',
  301. contact: item.feedbackperson || '',
  302. description: item.replynote || '',
  303. phone: item.contactnumber || '',
  304. imageList: item.attachments || [],
  305. replyTime: formatTime(item.replytime) || '',
  306. replyContent: item.replycontent || ''
  307. });
  308. }
  309. }
  310. },
  311. resetForm: function () {
  312. this.setData({
  313. contact: '',
  314. phone: '',
  315. description: '',
  316. imageList: [],
  317. category: '投诉',
  318. categoryValue: '1'
  319. });
  320. }
  321. });