与牧同行-小程序用户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

329 lines
7.8 KiB

  1. import http from '../../../utils/api'
  2. const baseUrl = require('../../../utils/baseUrl')
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. formData: {
  9. animalType: '',
  10. animalAge: '',
  11. animalGender: '',
  12. description: '',
  13. images: [] // 这里应该存储服务器返回的文件名数组
  14. },
  15. symptomsLength: 0,
  16. isSubmitting: false,
  17. isFormValid: false,
  18. tempImages: [], // 新增:临时存储本地图片路径
  19. isUploading: false, // 新增:防止重复上传
  20. showLoadingMask: false, // 新增:显示加载遮罩层
  21. loadingText: '提交中...' // 新增:加载提示文字
  22. },
  23. /**
  24. * 生命周期函数--监听页面加载
  25. */
  26. onLoad(options) {
  27. // 初始化表单验证
  28. this.checkFormValidity();
  29. },
  30. // 宠物类型输入
  31. onPetTypeInput(e) {
  32. const value = e.detail.value;
  33. this.setData({
  34. 'formData.animalType': value
  35. }, () => {
  36. this.checkFormValidity();
  37. });
  38. },
  39. // 宠物年龄输入
  40. onPetAgeInput(e) {
  41. const value = e.detail.value;
  42. this.setData({
  43. 'formData.animalAge': value
  44. }, () => {
  45. this.checkFormValidity();
  46. });
  47. },
  48. // 选择性别
  49. selectGender(e) {
  50. const value = e.currentTarget.dataset.value;
  51. this.setData({
  52. 'formData.animalGender': value
  53. }, () => {
  54. this.checkFormValidity();
  55. });
  56. },
  57. // 症状描述输入
  58. onSymptomsInput(e) {
  59. const value = e.detail.value;
  60. this.setData({
  61. 'formData.description': value,
  62. symptomsLength: value.length
  63. }, () => {
  64. this.checkFormValidity();
  65. });
  66. },
  67. // 选择图片并上传
  68. chooseImage() {
  69. if (this.data.isUploading) {
  70. wx.showToast({
  71. title: '正在上传中,请稍候',
  72. icon: 'none'
  73. });
  74. return;
  75. }
  76. wx.chooseMedia({
  77. mediaType: ['image'],
  78. sourceType: ['album', 'camera'],
  79. success: (res) => {
  80. if (res.tempFiles && res.tempFiles.length > 0) {
  81. this.setData({
  82. isUploading: true
  83. });
  84. // 显示加载提示
  85. wx.showLoading({
  86. title: '上传图片中...',
  87. mask: true
  88. });
  89. // 逐个上传图片
  90. this.uploadImages(res.tempFiles);
  91. }
  92. }
  93. });
  94. },
  95. // 上传图片到服务器
  96. uploadImages(files) {
  97. console.log(1111,files);
  98. const uploadPromises = files.map(file => {
  99. return new Promise((resolve, reject) => {
  100. wx.uploadFile({
  101. url: baseUrl + '/common/upload',
  102. header: {
  103. 'Authorization': 'Bearer ' + wx.getStorageSync('token')
  104. },
  105. filePath: file.tempFilePath,
  106. name: 'file',
  107. success: (uploadRes) => {
  108. console.log(2222,uploadRes);
  109. try {
  110. const result = JSON.parse(uploadRes.data);
  111. if (result.code === 200 || result.fileName) {
  112. resolve({
  113. tempPath: file.tempFilePath,
  114. serverPath: result.fileName
  115. });
  116. } else {
  117. reject(new Error('上传失败: ' + (result.msg || '服务器错误')));
  118. }
  119. } catch (error) {
  120. reject(new Error('解析服务器响应失败'));
  121. }
  122. },
  123. fail: (error) => {
  124. reject(new Error('网络请求失败'));
  125. }
  126. });
  127. });
  128. });
  129. // 等待所有图片上传完成
  130. Promise.all(uploadPromises)
  131. .then(results => {
  132. // 更新临时图片列表(用于预览)
  133. const newTempPaths = results.map(item => item.tempPath);
  134. const newTempImages = [...this.data.tempImages, ...newTempPaths];
  135. // 更新服务器返回的图片路径
  136. const newServerPaths = results.map(item => item.serverPath);
  137. const newFormImages = [...this.data.formData.images, ...newServerPaths];
  138. console.log(555,this.data.formData.images);
  139. console.log(666,newServerPaths);
  140. this.setData({
  141. tempImages: newTempImages,
  142. 'formData.images': newFormImages,
  143. isUploading: false
  144. });
  145. wx.hideLoading();
  146. wx.showToast({
  147. title: '上传成功',
  148. icon: 'success'
  149. });
  150. })
  151. .catch(error => {
  152. wx.hideLoading();
  153. this.setData({
  154. isUploading: false
  155. });
  156. wx.showToast({
  157. title: error.message || '上传失败',
  158. icon: 'none'
  159. });
  160. });
  161. },
  162. // 移除图片
  163. removeImage(e) {
  164. const index = e.currentTarget.dataset.index;
  165. // 移除临时图片
  166. const tempImages = [...this.data.tempImages];
  167. tempImages.splice(index, 1);
  168. // 移除服务器图片路径
  169. const images = [...this.data.formData.images];
  170. images.splice(index, 1);
  171. this.setData({
  172. tempImages,
  173. 'formData.images': images
  174. });
  175. },
  176. // 预览图片
  177. previewImage(e) {
  178. const index = e.currentTarget.dataset.index;
  179. const urls = this.data.tempImages;
  180. if (urls && urls.length > 0 && urls[index]) {
  181. wx.previewImage({
  182. current: urls[index],
  183. urls: urls
  184. });
  185. }
  186. },
  187. // 检查表单有效性
  188. checkFormValidity() {
  189. const {
  190. animalType,
  191. animalAge,
  192. animalGender,
  193. description
  194. } = this.data.formData;
  195. const isValid = animalType.trim() && animalAge && animalGender && description.trim();
  196. this.setData({
  197. isFormValid: !!isValid
  198. });
  199. },
  200. // 表单提交
  201. submitForm(e) {
  202. if (this.data.isSubmitting || !this.data.isFormValid) {
  203. return;
  204. }
  205. // 检查是否还有图片正在上传
  206. if (this.data.isUploading) {
  207. wx.showToast({
  208. title: '图片正在上传中,请稍后提交',
  209. icon: 'none'
  210. });
  211. return;
  212. }
  213. // 显示加载遮罩层
  214. this.setData({
  215. isSubmitting: true,
  216. showLoadingMask: true,
  217. loadingText: '提交中...'
  218. });
  219. // 准备提交数据
  220. const submitData = {
  221. ...this.data.formData,
  222. images: this.data.formData.images.join(',') // 将数组转为字符串,如果需要的话
  223. };
  224. http.wzdAdd({
  225. data: submitData,
  226. success: (res) => {
  227. console.log('提交成功', res);
  228. if(res.code==200){
  229. // 提交成功后的处理
  230. this.setData({
  231. loadingText: '提交成功'
  232. });
  233. setTimeout(() => {
  234. this.setData({
  235. isSubmitting: false,
  236. showLoadingMask: false
  237. });
  238. wx.showToast({
  239. title: '提交成功',
  240. icon: 'success',
  241. duration: 1500,
  242. success: () => {
  243. // 返回上一页
  244. wx.navigateBack();
  245. }
  246. });
  247. }, 1000);
  248. } else {
  249. this.setData({
  250. loadingText: '提交失败'
  251. });
  252. setTimeout(() => {
  253. this.setData({
  254. isSubmitting: false,
  255. showLoadingMask: false
  256. });
  257. wx.showToast({
  258. title: res.msg || '提交失败,请重试',
  259. icon: 'none',
  260. duration: 2000
  261. });
  262. }, 1000);
  263. }
  264. },
  265. fail: (err) => {
  266. this.setData({
  267. loadingText: '网络错误'
  268. });
  269. setTimeout(() => {
  270. this.setData({
  271. isSubmitting: false,
  272. showLoadingMask: false
  273. });
  274. wx.showToast({
  275. title: '网络异常,请检查网络后重试',
  276. icon: 'none',
  277. duration: 2000
  278. });
  279. }, 1000);
  280. }
  281. });
  282. },
  283. /**
  284. * 生命周期函数--监听页面卸载
  285. */
  286. onUnload() {
  287. // 页面卸载时重置状态
  288. this.setData({
  289. isSubmitting: false,
  290. showLoadingMask: false
  291. });
  292. }
  293. });