imgUploadAndDownLoad.js 7.3 KB

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