12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- import uploadStore from '@/store/modules/process'
- import { ElMessage } from 'element-plus'
- import { uploadFileMany } from "@/api/chat/msg";
- import useUserStore from "@/store/modules/user";
- const useUserStoreId = useUserStore();
- export function addUploadFile(raw) {
- const upload = uploadStore()
- const uploadProgress = {
- progress: 0,
- file_id: '',
- file_name: raw.name,
- percent: [],
- speed: '0 MB',
- file_size: raw.size,
- upload_size: 0,
- upload_time: new Date()
- }
- // status上传状态 0 队列,1 上传中,2 上传成功 , 3 取消上传
- // failTryCount 失败上传次数, 没上传一次,自动减去已,当为0的时候,停止上传
- upload.multiFileList.push({file: raw, progress: uploadProgress, status: 0, failTryCount: 3})
- multiUpload()
- }
- export function multiUpload() {
- const upload = uploadStore()
- const readFileList = []
- upload.multiFileList.forEach(res => {
- if (res.status === 0) {
- readFileList.push(res)
- }
- })
- if (readFileList.length > 0) {
- multiRun(upload, readFileList.slice(0, upload.processNumber))
- }
- }
- function multiRun(upload, keyList, func) {
- const processNumber = upload.processNumber
- const promise = upload.promise
- for (let i = 0; i < processNumber - promise.length; i++) {
- promise.push(Promise.resolve())
- }
- let reduceNumber = promise.length - processNumber
- if (reduceNumber > 0) {
- upload.promise = promise.slice(0, reduceNumber)
- }
- for (let j = 0; j < keyList.length; j += processNumber) {
- for (let i = 0; i < processNumber; i++) {
- if (i + j < keyList.length) {
- // promise[(j + i) % processNumber] = promise[(j + i) % processNumber].then(() => func(keyList[i + j]))
- // .catch(({fileInfo,err}) => {
- // if (fileInfo?.status === 3) {
- // console.log(fileInfo.file.name, '取消上传')
- // } else {
- // fileInfo.status = 0
- // fileInfo.failTryCount -= 1
- // if (fileInfo.failTryCount < 1) {
- // ElMessage.error(`${fileInfo.file.name} 超过最大重试次数,停止上传`)
- // } else {
- // ElMessage.error(`${fileInfo.file.name} 上传失败,正在重试`)
- // console.log(fileInfo.file.name, err)
- // multiUpload()
- // }
- // }
- // })
- }
- }
- }
- }
- uploadFileMany({spaceId:useUserStoreId.spaceIdCommon,dirId:17,files:''}).then((res) => {
- progress.progress = 100
- progress.upload_size = progress.file_size
- progress.speed = '上传成功'
- fileInfo.status = 2
- fileInfo.upload_time = new Date()
- console.log('res===', res)
- multiUpload()
- resolve()
- }).catch((err) => {
- fileInfo.status = 0
- fileInfo.failTryCount -= 1
- if (fileInfo.failTryCount < 1) {
- ElMessage.error(`${fileInfo.file.name} 超过最大重试次数,停止上传`)
- } else {
- ElMessage.error(`${fileInfo.file.name} 上传失败,正在重试`)
- console.log(fileInfo.file.name, err)
- multiUpload()
- }
- reject({fileInfo, err})
- })
|