progress.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import uploadStore from '@/store/modules/process'
  2. import { ElMessage } from 'element-plus'
  3. import { uploadFileMany } from "@/api/chat/msg";
  4. import useUserStore from "@/store/modules/user";
  5. const useUserStoreId = useUserStore();
  6. export function addUploadFile(raw) {
  7. const upload = uploadStore()
  8. const uploadProgress = {
  9. progress: 0,
  10. file_id: '',
  11. file_name: raw.name,
  12. percent: [],
  13. speed: '0 MB',
  14. file_size: raw.size,
  15. upload_size: 0,
  16. upload_time: new Date()
  17. }
  18. // status上传状态 0 队列,1 上传中,2 上传成功 , 3 取消上传
  19. // failTryCount 失败上传次数, 没上传一次,自动减去已,当为0的时候,停止上传
  20. upload.multiFileList.push({file: raw, progress: uploadProgress, status: 0, failTryCount: 3})
  21. multiUpload()
  22. }
  23. export function multiUpload() {
  24. const upload = uploadStore()
  25. const readFileList = []
  26. upload.multiFileList.forEach(res => {
  27. if (res.status === 0) {
  28. readFileList.push(res)
  29. }
  30. })
  31. if (readFileList.length > 0) {
  32. multiRun(upload, readFileList.slice(0, upload.processNumber))
  33. }
  34. }
  35. function multiRun(upload, keyList, func) {
  36. const processNumber = upload.processNumber
  37. const promise = upload.promise
  38. for (let i = 0; i < processNumber - promise.length; i++) {
  39. promise.push(Promise.resolve())
  40. }
  41. let reduceNumber = promise.length - processNumber
  42. if (reduceNumber > 0) {
  43. upload.promise = promise.slice(0, reduceNumber)
  44. }
  45. for (let j = 0; j < keyList.length; j += processNumber) {
  46. for (let i = 0; i < processNumber; i++) {
  47. if (i + j < keyList.length) {
  48. // promise[(j + i) % processNumber] = promise[(j + i) % processNumber].then(() => func(keyList[i + j]))
  49. // .catch(({fileInfo,err}) => {
  50. // if (fileInfo?.status === 3) {
  51. // console.log(fileInfo.file.name, '取消上传')
  52. // } else {
  53. // fileInfo.status = 0
  54. // fileInfo.failTryCount -= 1
  55. // if (fileInfo.failTryCount < 1) {
  56. // ElMessage.error(`${fileInfo.file.name} 超过最大重试次数,停止上传`)
  57. // } else {
  58. // ElMessage.error(`${fileInfo.file.name} 上传失败,正在重试`)
  59. // console.log(fileInfo.file.name, err)
  60. // multiUpload()
  61. // }
  62. // }
  63. // })
  64. }
  65. }
  66. }
  67. }
  68. uploadFileMany({spaceId:useUserStoreId.spaceIdCommon,dirId:17,files:''}).then((res) => {
  69. progress.progress = 100
  70. progress.upload_size = progress.file_size
  71. progress.speed = '上传成功'
  72. fileInfo.status = 2
  73. fileInfo.upload_time = new Date()
  74. console.log('res===', res)
  75. multiUpload()
  76. resolve()
  77. }).catch((err) => {
  78. fileInfo.status = 0
  79. fileInfo.failTryCount -= 1
  80. if (fileInfo.failTryCount < 1) {
  81. ElMessage.error(`${fileInfo.file.name} 超过最大重试次数,停止上传`)
  82. } else {
  83. ElMessage.error(`${fileInfo.file.name} 上传失败,正在重试`)
  84. console.log(fileInfo.file.name, err)
  85. multiUpload()
  86. }
  87. reject({fileInfo, err})
  88. })