imgUploadAndDownLoad.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. export default {
  2. methods: {
  3. //删除图片
  4. deleteImage(item) {
  5. uni.showModal({
  6. title: '提示',
  7. content: '确定要删除这张图片吗?',
  8. success: async (res) => {
  9. if (res.confirm) {
  10. // 调接口删除
  11. console.log('现在要删除的文件文件是:', item.id)
  12. try {
  13. await uni.$u.api.deleteClueFile([item.id])
  14. uni.showToast({
  15. title: '删除成功',
  16. icon: 'success',
  17. duration: 2000
  18. })
  19. } catch (error) {
  20. uni.showToast({
  21. title: '删除失败',
  22. icon: 'error',
  23. duration: 2000
  24. })
  25. }
  26. // 删除成功后刷新列表
  27. this.getList('2', '1');
  28. this.getList('2', '2');
  29. this.getList('2', '3');
  30. }
  31. }
  32. })
  33. },
  34. //获取文件列表
  35. async getList(type, orderFileType) {
  36. console.log('获取文件列表', type, orderFileType)
  37. try {
  38. const params = {
  39. clueId: this.orderDetail.clueId,
  40. sourceId: this.orderId,
  41. type,
  42. orderFileType,
  43. isDuplicate: '1',//先传1
  44. pageNum: 1,
  45. pageSize: 1000 // 设置一个较大的值以获取所有数据
  46. }
  47. const response = await uni.$u.api.selectClueFileByDto(params)
  48. if (orderFileType == '1') {
  49. this.trueUploadList = response.rows || []
  50. } else if (orderFileType == '2') {
  51. this.chatRecordsUploadList = response.rows || []
  52. } else if (orderFileType == '3') {
  53. this.detailImages = response.rows || []
  54. }
  55. } catch (error) {
  56. uni.$u.toast(`获取列表失败:${error.message}`)
  57. this.trueUploadList = []
  58. this.chatRecordsUploadList = []
  59. }
  60. },
  61. // 选择图片
  62. uploadImage(type) {
  63. uni.chooseImage({
  64. count: 9, // 最多选择9张
  65. sizeType: ['compressed'], // 压缩图片
  66. sourceType: ['album', 'camera'], // 从相册选择或拍照
  67. success: (res) => {
  68. const tempFilePaths = res.tempFilePaths
  69. console.log('上传的图片路径:11', tempFilePaths)
  70. this.uploadToServer(tempFilePaths, type)
  71. },
  72. fail: (err) => {
  73. console.error('选择图片失败:', err)
  74. }
  75. })
  76. },
  77. // 上传到服务器
  78. async uploadToServer(filePaths, type) {
  79. // 实际的上传逻辑
  80. try {
  81. //实物图的话就是1,聊天记录的话就是2,高清细节图的话就是3
  82. let p = type == 'truePic' ? '1' : type == 'chatRecords' ? '2' : type == 'detailImages' ? '3' : ''
  83. console.log('当前上传的图片类型是', p)
  84. const res = await Promise.all(filePaths.map(filePath => this.uploadFile(filePath, p)));
  85. this.bindOrder(p);
  86. this.getList('2', '1');
  87. this.getList('2', '2');
  88. this.getList('2', '3');
  89. } catch (error) {
  90. console.error('上传失败:', error);
  91. uni.$u.toast('上传失败');
  92. }
  93. },
  94. // 上传文件
  95. async uploadFile(fileUrl, orderFileType) {
  96. try {
  97. uni.showLoading({
  98. title: '上传中...',
  99. mask: true
  100. });
  101. // 调用统一的上传接口
  102. const { data } = await uni.$u.api.uploadFile(fileUrl);
  103. const fileObj = {
  104. fileSize: data.fileSize,
  105. fileSuffix: data.fileSuffix,
  106. fileName: data.name,
  107. fileUrl: data.url,
  108. orderFileType: orderFileType
  109. };
  110. this.bindList.push(fileObj);
  111. } catch (error) {
  112. uni.hideLoading();
  113. console.error('文件上传失败:', error);
  114. uni.$u.toast('上传失败,请重试');
  115. }
  116. },
  117. //绑定订单
  118. async bindOrder(orderFileType) {
  119. const res = await uni.$u.api.saveClueFile({
  120. clueId: this.orderDetail.clueId,//线索id
  121. list: this.bindList,
  122. sourceId: this.orderId,//订单id,sendformid
  123. type: '2',
  124. orderFileType: orderFileType
  125. });
  126. uni.$u.toast("上传成功");
  127. uni.hideLoading();
  128. // 清空待绑定的图片列表
  129. this.bindList = [];
  130. },
  131. //选择图图片》上传》返回路径,适用于page2选项里面的上传图片
  132. getImageUrl() {
  133. uni.chooseImage({
  134. count: 9, // 最多选择1张
  135. sizeType: ['compressed'], // 压缩图片
  136. sourceType: ['album', 'camera'], // 从相册选择或拍照
  137. success: async (res) => {
  138. const tempFilePath = res.tempFilePaths
  139. console.log('上传的图片路径:', tempFilePath)
  140. // 把blob数组上传到服务器,然后返回路径
  141. try {
  142. const res = await Promise.all(tempFilePath.map(filePath => uni.$u.api.uploadFile(filePath)));
  143. console.log('上传的图片路径数组是:', res.map(item => item.data.fileUrl))
  144. // 把数组返回给调用者
  145. return res.map(item => item.data.fileUrl)
  146. } catch (error) {
  147. console.error('上传失败:', error);
  148. uni.$u.toast('上传失败');
  149. }
  150. },
  151. fail: (err) => {
  152. console.error('选择图片失败:', err)
  153. }
  154. })
  155. }
  156. }
  157. }