tousujianyi.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. const app= getApp();
  2. Page({
  3. data: {
  4. address: '安平镇安坪村安平小区12栋305室',
  5. contact: '',
  6. phone: '',
  7. repairType: '',
  8. description: '',
  9. imageList: [],
  10. showNotification: true,
  11. countDown: 3,
  12. isFormValid: false,
  13. category: '投诉',
  14. isPreviewMode: false,
  15. title: '',
  16. content: '',
  17. replyTime: '',
  18. replyContent: '',
  19. showReplyInfo: false
  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. startCountDown: function() {
  38. let that = this;
  39. let timer = setInterval(function() {
  40. if (that.data.countDown > 0) {
  41. that.setData({
  42. countDown: that.data.countDown - 1
  43. });
  44. } else {
  45. clearInterval(timer);
  46. }
  47. }, 1000);
  48. },
  49. goBack: function() {
  50. wx.navigateBack();
  51. },
  52. radioChange: function(e) {
  53. this.setData({
  54. category: e.detail.value
  55. });
  56. this.checkFormValidity();
  57. },
  58. inputContact: function(e) {
  59. this.setData({
  60. contact: e.detail.value
  61. });
  62. this.checkFormValidity();
  63. },
  64. inputPhone: function(e) {
  65. const value = e.detail.value;
  66. const phoneNumber = value.replace(/\D/g, '');
  67. this.setData({
  68. phone: phoneNumber
  69. });
  70. this.checkFormValidity();
  71. },
  72. validatePhone: function(phone) {
  73. const phoneReg = /^1[3-9]\d{9}$/;
  74. return phoneReg.test(phone);
  75. },
  76. inputDescription: function(e) {
  77. this.setData({
  78. description: e.detail.value
  79. });
  80. this.checkFormValidity();
  81. },
  82. chooseImage: function() {
  83. let that = this;
  84. if (that.data.imageList.length >= 10) {
  85. wx.showToast({
  86. title: '最多只能上传10张图片',
  87. icon: 'none'
  88. });
  89. return;
  90. }
  91. wx.chooseMedia({
  92. count: 10 - that.data.imageList.length,
  93. mediaType: ['image'],
  94. sourceType: ['album', 'camera'],
  95. sizeType: ['compressed'],
  96. success: function(res) {
  97. let tempFiles = res.tempFiles;
  98. let validFiles = [];
  99. for (let i = 0; i < tempFiles.length; i++) {
  100. const file = tempFiles[i];
  101. if (file.size <= 10 * 1024 * 1024) {
  102. validFiles.push(file);
  103. } else {
  104. wx.showToast({
  105. title: '图片大小不能超过10M',
  106. icon: 'none'
  107. });
  108. }
  109. }
  110. if (validFiles.length > 0) {
  111. let newImageList = that.data.imageList.concat(validFiles);
  112. that.setData({
  113. imageList: newImageList
  114. });
  115. }
  116. }
  117. });
  118. },
  119. previewImage: function(e) {
  120. let index = e.currentTarget.dataset.index;
  121. wx.previewImage({
  122. current: this.data.imageList[index],
  123. urls: this.data.imageList
  124. });
  125. },
  126. deleteImage: function(e) {
  127. let index = e.currentTarget.dataset.index;
  128. let imageList = this.data.imageList;
  129. imageList.splice(index, 1);
  130. this.setData({
  131. imageList: imageList
  132. });
  133. },
  134. checkFormValidity: function() {
  135. const { contact, phone, description, category } = this.data;
  136. // 只检查必填项:联系人、联系电话和内容说明
  137. const isValid = contact.trim() !== '' && this.validatePhone(phone) && description.trim() !== '';
  138. this.setData({
  139. isFormValid: isValid
  140. });
  141. return isValid;
  142. },
  143. onInputChange: function(e) {
  144. const { field } = e.currentTarget.dataset;
  145. const { value } = e.detail;
  146. this.setData({
  147. [field]: value
  148. });
  149. this.checkFormValidity();
  150. },
  151. bindPickerChange: function(e) {
  152. this.checkFormValidity();
  153. },
  154. // 添加submitForm方法
  155. submitForm: function() {
  156. if (!this.checkFormValidity()) {
  157. let errorMsg = '';
  158. if (!this.data.contact.trim()) {
  159. errorMsg = '请填写联系人';
  160. } else if (!this.validatePhone(this.data.phone)) {
  161. errorMsg = '请输入正确的联系电话';
  162. } else if (!this.data.description.trim()) {
  163. errorMsg = '请填写内容说明';
  164. }
  165. wx.showToast({
  166. title: errorMsg,
  167. icon: 'none'
  168. });
  169. return;
  170. }
  171. debugger;
  172. const fileManager = wx.getFileSystemManager();
  173. this.data.imageList.map(imgInfo=>{
  174. const base64 = fileManager.readFileSync(imgInfo.tempFilePath, 'base64');
  175. imgInfo.base64=base64;
  176. return imgInfo;
  177. })
  178. // 构建提交数据
  179. const submitData = {
  180. category: this.data.category,
  181. contact: this.data.contact,
  182. phone: this.data.phone,
  183. description: this.data.description,
  184. images: this.data.imageList
  185. };
  186. console.log('提交的数据:', submitData);
  187. wx.showLoading({
  188. title: '提交中...',
  189. });
  190. wx.request({
  191. url: app.globalData.interfaceUrls.feedback,
  192. method: 'POST',
  193. header: {
  194. 'content-type': 'application/json', // 默认值
  195. 'token': app.globalData.userWxInfo.token,
  196. 'source': "wc",
  197. '!SAAS_LOGIN_TOKEN_!': app.globalData.currentAccountInfo.dsKey
  198. },
  199. data:submitData,
  200. success(res) {
  201. wx.hideLoading();
  202. if(res.data.code=='200'){
  203. wx.navigateTo({
  204. url: '/pages/tousujianyiSuccess/tousujianyiSuccess',
  205. });
  206. }
  207. },
  208. fail(error) {
  209. wx.hideLoading()
  210. utils.simleInfo('登记失败,请稍后再试')
  211. }
  212. })
  213. },
  214. submitRepair: function() {
  215. this.submitForm();
  216. },
  217. // 根据ID获取数据
  218. getDataById: function(id) {
  219. // 这里应该是从服务器获取数据
  220. // 但为了演示,我们从本地数据中获取
  221. const pages = getCurrentPages();
  222. const prevPage = pages[pages.length - 2]; // 获取上一个页面
  223. if (prevPage && prevPage.data.noticeList) {
  224. const item = prevPage.data.noticeList.find(item => item.id == id);
  225. if (item) {
  226. this.setData({
  227. category: item.title.includes('投诉') ? '投诉' : '建议',
  228. description: item.content,
  229. content: item.content,
  230. contact: item.contact,
  231. phone: item.phone,
  232. imageList: item.attachments || [],
  233. replyTime: item.replyTime,
  234. replyContent: item.replyContent
  235. });
  236. }
  237. }
  238. }
  239. });