|
|
|
|
@ -56,6 +56,23 @@ const props = defineProps({
|
|
|
|
|
const emit = defineEmits(["update:files"]); |
|
|
|
|
|
|
|
|
|
const files = ref(props.files); |
|
|
|
|
const allowArchiveFallback = ref(false); |
|
|
|
|
const fallbackArchiveExts = ['zip', 'rar', '7z']; |
|
|
|
|
const archiveExts = [...fallbackArchiveExts, 'tar', 'gz', 'bz2', 'xz', 'iso', 'dmg']; |
|
|
|
|
|
|
|
|
|
function getFileExt(file) { |
|
|
|
|
const name = (file?.name || file?.raw?.name || '').toLowerCase(); |
|
|
|
|
return name.includes('.') ? name.split('.').pop() : ''; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function enableArchiveFallback(file) { |
|
|
|
|
if (getFileExt(file) !== 'pdf') { |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
allowArchiveFallback.value = true; |
|
|
|
|
feedback.msgError('PDF上传失败,已临时允许上传一个 ZIP、RAR 或 7Z 压缩包'); |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
watch( |
|
|
|
|
() => props.files, |
|
|
|
|
@ -73,13 +90,10 @@ watch(files, () => {
|
|
|
|
|
},{immediate:true}); |
|
|
|
|
|
|
|
|
|
function beforeUpload(file) { |
|
|
|
|
const name = (file?.name || '').toLowerCase() |
|
|
|
|
const ext = name.includes('.') ? name.split('.').pop() : '' |
|
|
|
|
const ext = getFileExt(file) |
|
|
|
|
|
|
|
|
|
// 定义图片扩展名 |
|
|
|
|
const imageExts = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp', 'svg', 'tiff', 'tif', 'ico', 'heic', 'heif'] |
|
|
|
|
// 定义压缩包扩展名 |
|
|
|
|
const archiveExts = ['zip', 'rar', '7z', 'tar', 'gz', 'bz2', 'xz', 'iso', 'dmg'] |
|
|
|
|
|
|
|
|
|
// 图片类型:提示转为 PDF |
|
|
|
|
if (imageExts.includes(ext)) { |
|
|
|
|
@ -87,17 +101,28 @@ function beforeUpload(file) {
|
|
|
|
|
return false |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 压缩包类型:禁止上传 |
|
|
|
|
if (archiveExts.includes(ext)) { |
|
|
|
|
feedback.msgWarning('暂不支持压缩包上传') |
|
|
|
|
if (!allowArchiveFallback.value) { |
|
|
|
|
feedback.msgWarning('压缩包仅在 PDF 上传失败后允许上传') |
|
|
|
|
return false |
|
|
|
|
} |
|
|
|
|
if (!fallbackArchiveExts.includes(ext)) { |
|
|
|
|
feedback.msgWarning('PDF 上传失败后仅支持 ZIP、RAR 或 7Z 压缩包') |
|
|
|
|
return false |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 文件大小限制 100MB |
|
|
|
|
if (file.size >= 104857600) { |
|
|
|
|
feedback.msgError('文件大小不能超过100MB'); |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
if (fallbackArchiveExts.includes(ext)) { |
|
|
|
|
allowArchiveFallback.value = false; |
|
|
|
|
} |
|
|
|
|
if (ext === 'pdf') { |
|
|
|
|
allowArchiveFallback.value = false; |
|
|
|
|
} |
|
|
|
|
files.value.push({ |
|
|
|
|
uid: file.uid, |
|
|
|
|
percent: 0, |
|
|
|
|
@ -109,29 +134,48 @@ function uploadProgress(progressEvent, file) {
|
|
|
|
|
|
|
|
|
|
const filterFiles = files.value.filter((item) => file.uid === item.uid); |
|
|
|
|
if (filterFiles.length) { |
|
|
|
|
|
|
|
|
|
filterFiles[0].percent = parseInt(progressEvent.percent); |
|
|
|
|
filterFiles[0].percent = Math.min(99, parseInt(progressEvent.percent)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function handleSuccess(data, file) { |
|
|
|
|
const filterFiles = files.value.filter((item) => file.uid === item.uid); |
|
|
|
|
if (data.code !== 200) { |
|
|
|
|
if (!enableArchiveFallback(file)) { |
|
|
|
|
feedback.msgError(data.message); |
|
|
|
|
} |
|
|
|
|
if (filterFiles.length) { |
|
|
|
|
files.value.splice(files.value.indexOf(filterFiles[0]), 1); |
|
|
|
|
emit("update:files", files.value); |
|
|
|
|
} |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
if (getFileExt(file) === 'pdf') { |
|
|
|
|
allowArchiveFallback.value = false; |
|
|
|
|
} |
|
|
|
|
if (filterFiles.length) { |
|
|
|
|
filterFiles[0].percent = 100; |
|
|
|
|
filterFiles[0].fileName = data.data.fileName; |
|
|
|
|
filterFiles[0].filePath = data.data.filePath; |
|
|
|
|
window.setTimeout(() => { |
|
|
|
|
filterFiles[0].loading = false; |
|
|
|
|
emit("update:files", files.value); |
|
|
|
|
}, 300); |
|
|
|
|
} |
|
|
|
|
console.log("file", files.value); |
|
|
|
|
emit("update:files", files.value); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function handleError(e, file) { |
|
|
|
|
console.log(e, file); |
|
|
|
|
feedback.msgError("上传失败!"); |
|
|
|
|
const fileIndex = files.value.findIndex((item) => file.uid === item.uid); |
|
|
|
|
if (fileIndex > -1) { |
|
|
|
|
files.value.splice(fileIndex, 1); |
|
|
|
|
emit("update:files", files.value); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (enableArchiveFallback(file)) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
feedback.msgError("上传失败,请重试!"); |
|
|
|
|
} |
|
|
|
|
</script> |
|
|
|
|
|