|
|
@ -69,24 +69,18 @@ |
|
|
|
props: { |
|
|
|
props: { |
|
|
|
...basicProps, |
|
|
|
...basicProps, |
|
|
|
previewFileList: { |
|
|
|
previewFileList: { |
|
|
|
type: Array as PropType<FileItem[]>, |
|
|
|
type: Array as PropType<string[]>, |
|
|
|
default: () => [], |
|
|
|
default: () => [], |
|
|
|
}, |
|
|
|
}, |
|
|
|
}, |
|
|
|
}, |
|
|
|
emits: ['change', 'register', 'delete', 'success'], |
|
|
|
emits: ['change', 'register', 'delete', 'success'], |
|
|
|
setup(props, { emit }) { |
|
|
|
setup(props, { emit }) { |
|
|
|
const state = reactive<{ fileList: FileItem[] }>({ |
|
|
|
const { t } = useI18n(); |
|
|
|
fileList: [], |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 是否正在上传 |
|
|
|
|
|
|
|
const isUploadingRef = ref(false); |
|
|
|
const isUploadingRef = ref(false); |
|
|
|
const fileListRef = ref<FileItem[]>([]); |
|
|
|
const fileListRef = ref<FileItem[]>([]); |
|
|
|
const { accept, helpText, maxNumber, maxSize } = toRefs(props); |
|
|
|
const { accept, helpText, maxNumber, maxSize } = toRefs(props); |
|
|
|
|
|
|
|
|
|
|
|
const { t } = useI18n(); |
|
|
|
|
|
|
|
const [register, { closeModal }] = useModalInner(); |
|
|
|
const [register, { closeModal }] = useModalInner(); |
|
|
|
|
|
|
|
const { createMessage } = useMessage(); |
|
|
|
const { getStringAccept, getHelpText } = useUploadType({ |
|
|
|
const { getStringAccept, getHelpText } = useUploadType({ |
|
|
|
acceptRef: accept, |
|
|
|
acceptRef: accept, |
|
|
|
helpTextRef: helpText, |
|
|
|
helpTextRef: helpText, |
|
|
@ -94,28 +88,16 @@ |
|
|
|
maxSizeRef: maxSize, |
|
|
|
maxSizeRef: maxSize, |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
const { createMessage } = useMessage(); |
|
|
|
const getIsSelectFile = computed(() => |
|
|
|
|
|
|
|
(fileListRef.value.length > 0 && !fileListRef.value.every((item) => item.status === UploadResultStatus.SUCCESS))); |
|
|
|
const getIsSelectFile = computed(() => { |
|
|
|
|
|
|
|
return ( |
|
|
|
|
|
|
|
fileListRef.value.length > 0 && |
|
|
|
|
|
|
|
!fileListRef.value.every((item) => item.status === UploadResultStatus.SUCCESS) |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const getOkButtonProps = computed(() => { |
|
|
|
const getOkButtonProps = computed(() => { |
|
|
|
const someSuccess = fileListRef.value.some( |
|
|
|
const someSuccess = fileListRef.value.some(item => item.status === UploadResultStatus.SUCCESS); |
|
|
|
(item) => item.status === UploadResultStatus.SUCCESS, |
|
|
|
return { disabled: isUploadingRef.value || fileListRef.value.length === 0 || !someSuccess }; |
|
|
|
); |
|
|
|
|
|
|
|
return { |
|
|
|
|
|
|
|
disabled: isUploadingRef.value || fileListRef.value.length === 0 || !someSuccess, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
const getUploadBtnText = computed(() => { |
|
|
|
const getUploadBtnText = computed(() => { |
|
|
|
const someError = fileListRef.value.some( |
|
|
|
const someError = fileListRef.value.some(item => item.status === UploadResultStatus.ERROR); |
|
|
|
(item) => item.status === UploadResultStatus.ERROR, |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
return isUploadingRef.value |
|
|
|
return isUploadingRef.value |
|
|
|
? t('component.upload.uploading') |
|
|
|
? t('component.upload.uploading') |
|
|
|
: someError |
|
|
|
: someError |
|
|
@ -147,10 +129,7 @@ |
|
|
|
getBase64WithFile(file).then(({ result: thumbUrl }) => { |
|
|
|
getBase64WithFile(file).then(({ result: thumbUrl }) => { |
|
|
|
fileListRef.value = [ |
|
|
|
fileListRef.value = [ |
|
|
|
...unref(fileListRef), |
|
|
|
...unref(fileListRef), |
|
|
|
{ |
|
|
|
{ thumbUrl, ...commonItem } |
|
|
|
thumbUrl, |
|
|
|
|
|
|
|
...commonItem, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
]; |
|
|
|
]; |
|
|
|
}); |
|
|
|
}); |
|
|
|
} else { |
|
|
|
} else { |
|
|
@ -168,16 +147,12 @@ |
|
|
|
|
|
|
|
|
|
|
|
async function uploadApiByItem(item: FileItem) { |
|
|
|
async function uploadApiByItem(item: FileItem) { |
|
|
|
const { api } = props; |
|
|
|
const { api } = props; |
|
|
|
if (!api || !isFunction(api)) { |
|
|
|
if (!api || !isFunction(api)) |
|
|
|
return warn('upload api must exist and be a function'); |
|
|
|
return warn('upload api must exist and be a function'); |
|
|
|
} |
|
|
|
|
|
|
|
try { |
|
|
|
try { |
|
|
|
item.status = UploadResultStatus.UPLOADING; |
|
|
|
item.status = UploadResultStatus.UPLOADING; |
|
|
|
const { data } = await props.api?.( |
|
|
|
const { data } = await props.api?.({ |
|
|
|
{ |
|
|
|
data: {...(props.uploadParams || {})}, |
|
|
|
data: { |
|
|
|
|
|
|
|
...(props.uploadParams || {}), |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
file: item.file, |
|
|
|
file: item.file, |
|
|
|
name: props.name, |
|
|
|
name: props.name, |
|
|
|
filename: props.filename, |
|
|
|
filename: props.filename, |
|
|
@ -194,7 +169,6 @@ |
|
|
|
error: null, |
|
|
|
error: null, |
|
|
|
}; |
|
|
|
}; |
|
|
|
} catch (e) { |
|
|
|
} catch (e) { |
|
|
|
console.log(e); |
|
|
|
|
|
|
|
item.status = UploadResultStatus.ERROR; |
|
|
|
item.status = UploadResultStatus.ERROR; |
|
|
|
return { |
|
|
|
return { |
|
|
|
success: false, |
|
|
|
success: false, |
|
|
@ -212,20 +186,13 @@ |
|
|
|
try { |
|
|
|
try { |
|
|
|
isUploadingRef.value = true; |
|
|
|
isUploadingRef.value = true; |
|
|
|
// 只上传不是成功状态的 |
|
|
|
// 只上传不是成功状态的 |
|
|
|
const uploadFileList = |
|
|
|
const uploadFileList = fileListRef.value.filter((item) => item.status !== UploadResultStatus.SUCCESS) || []; |
|
|
|
fileListRef.value.filter((item) => item.status !== UploadResultStatus.SUCCESS) || []; |
|
|
|
const result = await Promise.all(uploadFileList.map((item) => uploadApiByItem(item))); |
|
|
|
const data = await Promise.all( |
|
|
|
|
|
|
|
uploadFileList.map((item) => { |
|
|
|
|
|
|
|
return uploadApiByItem(item); |
|
|
|
|
|
|
|
}), |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
isUploadingRef.value = false; |
|
|
|
isUploadingRef.value = false; |
|
|
|
// 生产环境:抛出错误 |
|
|
|
const errorList = result.filter((item: any) => !item.success); |
|
|
|
const errorList = data.filter((item: any) => !item.success); |
|
|
|
|
|
|
|
if (errorList.length > 0) throw errorList; |
|
|
|
if (errorList.length > 0) throw errorList; |
|
|
|
useTimeoutFn(() => { |
|
|
|
// 当关闭上传保存按钮,默认选择开始上传按钮充当保存按钮 |
|
|
|
emit('success'); |
|
|
|
!props.showUploadSaveBtn && handleOk(); |
|
|
|
}, 300); |
|
|
|
|
|
|
|
} catch (e) { |
|
|
|
} catch (e) { |
|
|
|
isUploadingRef.value = false; |
|
|
|
isUploadingRef.value = false; |
|
|
|
throw e; |
|
|
|
throw e; |
|
|
@ -280,7 +247,6 @@ |
|
|
|
getOkButtonProps, |
|
|
|
getOkButtonProps, |
|
|
|
beforeUpload, |
|
|
|
beforeUpload, |
|
|
|
fileListRef, |
|
|
|
fileListRef, |
|
|
|
state, |
|
|
|
|
|
|
|
isUploadingRef, |
|
|
|
isUploadingRef, |
|
|
|
handleStartUpload, |
|
|
|
handleStartUpload, |
|
|
|
handleOk, |
|
|
|
handleOk, |
|
|
|