123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- 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), uploadAsync)
- }
- }
- 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()
- // }
- // }
- // })
- }
- }
- }
- }
- function uploadAsync(fileInfo) {
- const progress = fileInfo.progress
- const file = fileInfo.file
- return new Promise((resolve, reject) => {
- progress.file_name = file.name
- progress.file_size = file.size
- if (fileInfo.status === 0) {
- fileInfo.status = 1
- } else {
- return resolve()
- }
- progress.progress = 10
- // getUploadSid().then(async res => {
- // progress.speed = '文件读取中'
- // let hash = await PreHash(file, progress)
- // let fileHashInfo = {
- // sid: res.data.sid,
- // file_name: progress.file_name,
- // file_size: progress.file_size,
- // pre_hash: hash
- // }
- // progress.progress = 20
- // checkPreHash(fileHashInfo).then(async pRes => {
- // if (pRes.data.check_status === true) {
- // progress.progress = 30
- // const md5Code = pRes.data.md5_token
- // progress.speed = '文件校验中'
- // let hash = await ContentHash(file, md5Code, progress)
- // fileHashInfo.proof_code = hash.proofCode
- // fileHashInfo.content_hash = hash.conHash
- // checkContentHash(fileHashInfo).then(async cRes => {
- // if (cRes.data.check_status === true) {
- // progress.progress = 100
- // progress.upload_size = progress.file_size
- // progress.speed = '秒传成功'
- // fileInfo.status = 2
- // fileInfo.upload_time = new Date()
- // multiUpload()
- // resolve()
- // } else {
- // return await ChunkedUpload(fileInfo, fileHashInfo, cRes.data.upload_extra, cRes.data.part_info_list, () => {
- // fileInfo.status = 2
- // fileInfo.upload_time = new Date()
- // multiUpload()
- // resolve()
- // }, (err) => {
- // reject({fileInfo, err})
- // })
- // }
- // }).catch((err) => {
- // reject({fileInfo, err})
- // })
- // } else {
- // return await ChunkedUpload(fileInfo, fileHashInfo, pRes.data.upload_extra, pRes.data.part_info_list, () => {
- // fileInfo.status = 2
- // fileInfo.upload_time = new Date()
- // multiUpload()
- // resolve()
- // }, (err) => {
- // reject({fileInfo, err})
- // })
- // }
- // }).catch((err) => {
- // reject({fileInfo, err})
- // })
- // }).catch((err) => {
- // reject({fileInfo, err})
- // })
- // Update this part of the code based on your project's API for uploading files
- console.log('file=========', file,)
- 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})
- })
- })
- }
|