tousujianyi.js 7.3 KB

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