|
|
|
@ -84,7 +84,7 @@ const emit = defineEmits(['update:modelValue'])
|
|
|
|
const props = defineProps({
|
|
|
|
const props = defineProps({
|
|
|
|
modelValue: propTypes.oneOfType<string | string[]>([String, Array<String>]).isRequired,
|
|
|
|
modelValue: propTypes.oneOfType<string | string[]>([String, Array<String>]).isRequired,
|
|
|
|
fileType: propTypes.array.def(['png', 'jpg', 'jpeg', 'doc', 'xls', 'ppt', 'txt', 'pdf']), // 文件类型, 例如['png', 'jpg', 'jpeg']
|
|
|
|
fileType: propTypes.array.def(['png', 'jpg', 'jpeg', 'doc', 'xls', 'ppt', 'txt', 'pdf']), // 文件类型, 例如['png', 'jpg', 'jpeg']
|
|
|
|
fileSize: propTypes.number.def(5), // 大小限制(MB)
|
|
|
|
fileSize: propTypes.number.def(100), // 大小限制(MB)
|
|
|
|
limit: propTypes.number.def(5), // 数量限制
|
|
|
|
limit: propTypes.number.def(5), // 数量限制
|
|
|
|
autoUpload: propTypes.bool.def(true), // 自动上传
|
|
|
|
autoUpload: propTypes.bool.def(true), // 自动上传
|
|
|
|
drag: propTypes.bool.def(false), // 拖拽上传
|
|
|
|
drag: propTypes.bool.def(false), // 拖拽上传
|
|
|
|
@ -97,6 +97,7 @@ const uploadRef = ref<UploadInstance>()
|
|
|
|
const uploadList = ref<UploadUserFile[]>([])
|
|
|
|
const uploadList = ref<UploadUserFile[]>([])
|
|
|
|
const fileList = ref<UploadUserFile[]>([])
|
|
|
|
const fileList = ref<UploadUserFile[]>([])
|
|
|
|
const uploadNumber = ref<number>(0)
|
|
|
|
const uploadNumber = ref<number>(0)
|
|
|
|
|
|
|
|
const nameMap = ref<Record<string, string>>({})
|
|
|
|
|
|
|
|
|
|
|
|
const { uploadUrl, httpRequest } = useUpload()
|
|
|
|
const { uploadUrl, httpRequest } = useUpload()
|
|
|
|
|
|
|
|
|
|
|
|
@ -130,14 +131,41 @@ const beforeUpload: UploadProps['beforeUpload'] = (file: UploadRawFile) => {
|
|
|
|
// const handleFileChange = (uploadFile: UploadFile): void => {
|
|
|
|
// const handleFileChange = (uploadFile: UploadFile): void => {
|
|
|
|
// uploadRef.value.data.path = uploadFile.name
|
|
|
|
// uploadRef.value.data.path = uploadFile.name
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// 文件上传成功
|
|
|
|
const getUrlFromRes = (res: any): string => {
|
|
|
|
const handleFileSuccess: UploadProps['onSuccess'] = (res: any): void => {
|
|
|
|
if (!res) return ''
|
|
|
|
|
|
|
|
if (typeof res === 'string') return res
|
|
|
|
|
|
|
|
if (typeof res.data === 'string') return res.data
|
|
|
|
|
|
|
|
const data = res.data || res
|
|
|
|
|
|
|
|
if (typeof data === 'string') return data
|
|
|
|
|
|
|
|
if (data.fileUrl) return data.fileUrl
|
|
|
|
|
|
|
|
return ''
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const getNameFromRes = (res: any, url: string): string => {
|
|
|
|
|
|
|
|
const data = res && res.data ? res.data : res
|
|
|
|
|
|
|
|
if (data && typeof data === 'object' && data.fileName) return data.fileName
|
|
|
|
|
|
|
|
if (!url) return ''
|
|
|
|
|
|
|
|
const idx = url.lastIndexOf('/')
|
|
|
|
|
|
|
|
return idx !== -1 ? url.substring(idx + 1) : url
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const handleFileSuccess: UploadProps['onSuccess'] = (res: any, uploadFile): void => {
|
|
|
|
|
|
|
|
const url = getUrlFromRes(res)
|
|
|
|
|
|
|
|
if (!url) {
|
|
|
|
|
|
|
|
message.error('上传返回数据异常')
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const name = getNameFromRes(res, url)
|
|
|
|
|
|
|
|
if (url && name) {
|
|
|
|
|
|
|
|
nameMap.value[url] = name
|
|
|
|
|
|
|
|
}
|
|
|
|
message.success('上传成功')
|
|
|
|
message.success('上传成功')
|
|
|
|
// 删除自身
|
|
|
|
const index = fileList.value.findIndex((item) => item.uid === uploadFile.uid)
|
|
|
|
const index = fileList.value.findIndex((item) => item.response?.data === res.data)
|
|
|
|
if (index > -1) {
|
|
|
|
fileList.value.splice(index, 1)
|
|
|
|
fileList.value.splice(index, 1)
|
|
|
|
uploadList.value.push({ name: res.data, url: res.data })
|
|
|
|
}
|
|
|
|
if (uploadList.value.length == uploadNumber.value) {
|
|
|
|
uploadList.value.push({ name, url })
|
|
|
|
|
|
|
|
if (uploadList.value.length === uploadNumber.value) {
|
|
|
|
fileList.value.push(...uploadList.value)
|
|
|
|
fileList.value.push(...uploadList.value)
|
|
|
|
uploadList.value = []
|
|
|
|
uploadList.value = []
|
|
|
|
uploadNumber.value = 0
|
|
|
|
uploadNumber.value = 0
|
|
|
|
@ -156,7 +184,10 @@ const excelUploadError: UploadProps['onError'] = (): void => {
|
|
|
|
const handleRemove = (file: UploadFile) => {
|
|
|
|
const handleRemove = (file: UploadFile) => {
|
|
|
|
const index = fileList.value.map((f) => f.name).indexOf(file.name)
|
|
|
|
const index = fileList.value.map((f) => f.name).indexOf(file.name)
|
|
|
|
if (index > -1) {
|
|
|
|
if (index > -1) {
|
|
|
|
fileList.value.splice(index, 1)
|
|
|
|
const removed = fileList.value.splice(index, 1)[0]
|
|
|
|
|
|
|
|
if (removed && removed.url && nameMap.value[removed.url]) {
|
|
|
|
|
|
|
|
delete nameMap.value[removed.url]
|
|
|
|
|
|
|
|
}
|
|
|
|
emitUpdateModelValue()
|
|
|
|
emitUpdateModelValue()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -164,6 +195,13 @@ const handlePreview: UploadProps['onPreview'] = (uploadFile) => {
|
|
|
|
console.log(uploadFile)
|
|
|
|
console.log(uploadFile)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const getDisplayNameByUrl = (url: string): string => {
|
|
|
|
|
|
|
|
const cached = nameMap.value[url]
|
|
|
|
|
|
|
|
if (cached) return cached
|
|
|
|
|
|
|
|
const idx = url.lastIndexOf('/')
|
|
|
|
|
|
|
|
return idx !== -1 ? url.substring(idx + 1) : url
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 监听模型绑定值变动
|
|
|
|
// 监听模型绑定值变动
|
|
|
|
watch(
|
|
|
|
watch(
|
|
|
|
() => props.modelValue,
|
|
|
|
() => props.modelValue,
|
|
|
|
@ -176,14 +214,13 @@ watch(
|
|
|
|
fileList.value = [] // 保障数据为空
|
|
|
|
fileList.value = [] // 保障数据为空
|
|
|
|
// 情况1:字符串
|
|
|
|
// 情况1:字符串
|
|
|
|
if (isString(val)) {
|
|
|
|
if (isString(val)) {
|
|
|
|
fileList.value.push(
|
|
|
|
const urls = val.split(',').filter((u) => !!u)
|
|
|
|
...val.split(',').map((url) => ({ name: url.substring(url.lastIndexOf('/') + 1), url }))
|
|
|
|
fileList.value.push(...urls.map((url) => ({ name: getDisplayNameByUrl(url), url })))
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 情况2:数组
|
|
|
|
// 情况2:数组
|
|
|
|
fileList.value.push(
|
|
|
|
fileList.value.push(
|
|
|
|
...(val as string[]).map((url) => ({ name: url.substring(url.lastIndexOf('/') + 1), url }))
|
|
|
|
...(val as string[]).map((url) => ({ name: getDisplayNameByUrl(url), url }))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{ immediate: true, deep: true }
|
|
|
|
{ immediate: true, deep: true }
|
|
|
|
|