tousujianyi.js 6.4 KB

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