与牧同行-小程序用户端
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.

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