imgUploadAndDownLoad.js 8.2 KB

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