|
|
@@ -27,19 +27,29 @@ const props = withDefaults(defineProps<Props>(), {
|
|
|
})
|
|
|
const emits = defineEmits(['update:modelValue'])
|
|
|
|
|
|
+const baseApi = import.meta.env.VITE_BASE_API
|
|
|
const modelValue = computed({
|
|
|
get: () => props.modelValue,
|
|
|
set: value => emits('update:modelValue', value)
|
|
|
})
|
|
|
|
|
|
const user = useUserStore()
|
|
|
-const baseApi = import.meta.env.VITE_BASE_API
|
|
|
const headers = reactive({
|
|
|
[ACCESS_TOKEN]: user.token
|
|
|
})
|
|
|
|
|
|
const limit = Number(props.limit) || 1
|
|
|
|
|
|
+// 生成图片地址
|
|
|
+const prefix = props.oss ? config.ossHost : baseApi
|
|
|
+const genImageUrl = (url: string) => {
|
|
|
+ if (url.startsWith('blob:') || isAbsolutePath(url)) {
|
|
|
+ return url
|
|
|
+ } else {
|
|
|
+ return prefix + url
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
const uploadRef = ref()
|
|
|
// 在上传之前对文件进行验证
|
|
|
const fileSize = Number(props.fileSize) || 10
|
|
|
@@ -52,14 +62,12 @@ const beforeUpload: UploadProps['beforeUpload'] = rawFile => {
|
|
|
}
|
|
|
|
|
|
// 图片上传成功
|
|
|
-const handleUploadSuccess: UploadProps['onSuccess'] = (response, uploadFile: UploadFile) => {
|
|
|
+const uploadList = ref([...props.modelValue])
|
|
|
+const handleUploadSuccess: UploadProps['onSuccess'] = (response, uploadFile: UploadFile, uploadFiles: UploadFile[]) => {
|
|
|
+ console.log('uploadFiles', uploadFiles)
|
|
|
if (response.success || response.code === 200) {
|
|
|
// 附加oss路径
|
|
|
- modelValue.value.forEach((item: any) => {
|
|
|
- if (item.uid === uploadFile.uid) {
|
|
|
- item.extra = props.oss ? response.url : config.uploadSuccessCb(response)
|
|
|
- }
|
|
|
- })
|
|
|
+ uploadList.value = uploadFiles
|
|
|
} else {
|
|
|
ElMessage.error(response.msg)
|
|
|
}
|
|
|
@@ -71,7 +79,7 @@ const handleRemove = (file: UploadFile) => {
|
|
|
// 图片预览
|
|
|
const showViewer = ref(false)
|
|
|
const previewIndex = ref(0)
|
|
|
-const previewList = computed(() => modelValue.value.map((item: any) => item.url))
|
|
|
+const previewList = computed(() => modelValue.value.map((item: any) => genImageUrl(item.url)))
|
|
|
const handlePreview = (file: UploadFile) => {
|
|
|
previewIndex.value = modelValue.value.findIndex((item: any) => item.url === file.url)
|
|
|
showViewer.value = true
|
|
|
@@ -81,7 +89,7 @@ const closeViewer = () => {
|
|
|
}
|
|
|
|
|
|
// 接口需要的属性
|
|
|
-props.item.extra = computed(() => modelValue.value.map((item: any) => item.extra))
|
|
|
+props.item.extra = computed(() => uploadList.value.map((item: any) => item.response?.url || item.url))
|
|
|
|
|
|
// 动态属性
|
|
|
const attrs: any = props.oss ? { 'http-request': ossUpload } : {}
|
|
|
@@ -105,7 +113,7 @@ if (!props.item.props?.onSuccess) {
|
|
|
>
|
|
|
<el-icon class="avatar-uploader-icon" :style="{ fontSize: iconSize }"><Plus /></el-icon>
|
|
|
<template #file="{ file }">
|
|
|
- <el-image class="el-upload-list__item-thumbnail" :src="file.url" fit="cover" />
|
|
|
+ <el-image class="el-upload-list__item-thumbnail" :src="genImageUrl(file.url)" fit="cover" />
|
|
|
<span class="el-upload-list__item-actions">
|
|
|
<span class="el-upload-list__item-preview" @click="handlePreview(file)">
|
|
|
<el-icon><zoom-in /></el-icon>
|