progress.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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), uploadAsync)
  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. function uploadAsync(fileInfo) {
  69. const progress = fileInfo.progress
  70. const file = fileInfo.file
  71. return new Promise((resolve, reject) => {
  72. progress.file_name = file.name
  73. progress.file_size = file.size
  74. if (fileInfo.status === 0) {
  75. fileInfo.status = 1
  76. } else {
  77. return resolve()
  78. }
  79. progress.progress = 10
  80. // getUploadSid().then(async res => {
  81. // progress.speed = '文件读取中'
  82. // let hash = await PreHash(file, progress)
  83. // let fileHashInfo = {
  84. // sid: res.data.sid,
  85. // file_name: progress.file_name,
  86. // file_size: progress.file_size,
  87. // pre_hash: hash
  88. // }
  89. // progress.progress = 20
  90. // checkPreHash(fileHashInfo).then(async pRes => {
  91. // if (pRes.data.check_status === true) {
  92. // progress.progress = 30
  93. // const md5Code = pRes.data.md5_token
  94. // progress.speed = '文件校验中'
  95. // let hash = await ContentHash(file, md5Code, progress)
  96. // fileHashInfo.proof_code = hash.proofCode
  97. // fileHashInfo.content_hash = hash.conHash
  98. // checkContentHash(fileHashInfo).then(async cRes => {
  99. // if (cRes.data.check_status === true) {
  100. // progress.progress = 100
  101. // progress.upload_size = progress.file_size
  102. // progress.speed = '秒传成功'
  103. // fileInfo.status = 2
  104. // fileInfo.upload_time = new Date()
  105. // multiUpload()
  106. // resolve()
  107. // } else {
  108. // return await ChunkedUpload(fileInfo, fileHashInfo, cRes.data.upload_extra, cRes.data.part_info_list, () => {
  109. // fileInfo.status = 2
  110. // fileInfo.upload_time = new Date()
  111. // multiUpload()
  112. // resolve()
  113. // }, (err) => {
  114. // reject({fileInfo, err})
  115. // })
  116. // }
  117. // }).catch((err) => {
  118. // reject({fileInfo, err})
  119. // })
  120. // } else {
  121. // return await ChunkedUpload(fileInfo, fileHashInfo, pRes.data.upload_extra, pRes.data.part_info_list, () => {
  122. // fileInfo.status = 2
  123. // fileInfo.upload_time = new Date()
  124. // multiUpload()
  125. // resolve()
  126. // }, (err) => {
  127. // reject({fileInfo, err})
  128. // })
  129. // }
  130. // }).catch((err) => {
  131. // reject({fileInfo, err})
  132. // })
  133. // }).catch((err) => {
  134. // reject({fileInfo, err})
  135. // })
  136. // Update this part of the code based on your project's API for uploading files
  137. console.log('file=========', file,)
  138. uploadFileMany({spaceId:useUserStoreId.spaceIdCommon,dirId:17,files:''}).then((res) => {
  139. progress.progress = 100
  140. progress.upload_size = progress.file_size
  141. progress.speed = '上传成功'
  142. fileInfo.status = 2
  143. fileInfo.upload_time = new Date()
  144. console.log('res===', res)
  145. multiUpload()
  146. resolve()
  147. }).catch((err) => {
  148. fileInfo.status = 0
  149. fileInfo.failTryCount -= 1
  150. if (fileInfo.failTryCount < 1) {
  151. ElMessage.error(`${fileInfo.file.name} 超过最大重试次数,停止上传`)
  152. } else {
  153. ElMessage.error(`${fileInfo.file.name} 上传失败,正在重试`)
  154. console.log(fileInfo.file.name, err)
  155. multiUpload()
  156. }
  157. reject({fileInfo, err})
  158. })
  159. })
  160. }