|
@@ -1,24 +1,27 @@
|
|
<template>
|
|
<template>
|
|
- <el-upload
|
|
|
|
- ref="uploadRef"
|
|
|
|
- :multiple="limit > 1"
|
|
|
|
- name="file"
|
|
|
|
- list-type="picture-card"
|
|
|
|
- v-model:file-list="fileList"
|
|
|
|
- :show-file-list="true"
|
|
|
|
- :action="updateUrl"
|
|
|
|
- :headers="uploadHeaders"
|
|
|
|
- :limit="limit"
|
|
|
|
- :before-upload="beforeUpload"
|
|
|
|
- :on-exceed="handleExceed"
|
|
|
|
- :on-success="handleFileSuccess"
|
|
|
|
- :on-error="excelUploadError"
|
|
|
|
- :on-remove="handleRemove"
|
|
|
|
- :on-preview="handlePictureCardPreview"
|
|
|
|
- :class="{ hide: fileList.length >= limit }"
|
|
|
|
- >
|
|
|
|
- <Icon icon="ep:upload-filled" />
|
|
|
|
- </el-upload>
|
|
|
|
|
|
+ <div class="component-upload-image">
|
|
|
|
+ <el-upload
|
|
|
|
+ ref="uploadRef"
|
|
|
|
+ :multiple="props.limit > 1"
|
|
|
|
+ name="file"
|
|
|
|
+ v-model="valueRef"
|
|
|
|
+ list-type="picture-card"
|
|
|
|
+ :file-list="fileList"
|
|
|
|
+ :show-file-list="true"
|
|
|
|
+ :action="updateUrl"
|
|
|
|
+ :headers="uploadHeaders"
|
|
|
|
+ :limit="props.limit"
|
|
|
|
+ :before-upload="beforeUpload"
|
|
|
|
+ :on-exceed="handleExceed"
|
|
|
|
+ :on-success="handleFileSuccess"
|
|
|
|
+ :on-error="excelUploadError"
|
|
|
|
+ :on-remove="handleRemove"
|
|
|
|
+ :on-preview="handlePictureCardPreview"
|
|
|
|
+ :class="{ hide: fileList.length >= props.limit }"
|
|
|
|
+ >
|
|
|
|
+ <Icon icon="ep:upload-filled" />
|
|
|
|
+ </el-upload>
|
|
|
|
+ </div>
|
|
<!-- 文件列表 -->
|
|
<!-- 文件列表 -->
|
|
<Dialog v-model="dialogVisible" title="预览" width="800" append-to-body>
|
|
<Dialog v-model="dialogVisible" title="预览" width="800" append-to-body>
|
|
<img :src="dialogImageUrl" style="display: block; max-width: 100%; margin: 0 auto" />
|
|
<img :src="dialogImageUrl" style="display: block; max-width: 100%; margin: 0 auto" />
|
|
@@ -32,10 +35,10 @@ import { getAccessToken, getTenantId } from '@/utils/auth'
|
|
import { ElUpload, UploadInstance, UploadProps, UploadRawFile, UploadUserFile } from 'element-plus'
|
|
import { ElUpload, UploadInstance, UploadProps, UploadRawFile, UploadUserFile } from 'element-plus'
|
|
|
|
|
|
const message = useMessage() // 消息弹窗
|
|
const message = useMessage() // 消息弹窗
|
|
-const emit = defineEmits(['input'])
|
|
|
|
|
|
+const emit = defineEmits(['update:modelValue'])
|
|
|
|
|
|
const props = defineProps({
|
|
const props = defineProps({
|
|
- imgs: propTypes.oneOfType([String, Object, Array]),
|
|
|
|
|
|
+ modelValue: propTypes.oneOfType([String, Object, Array]),
|
|
title: propTypes.string.def('图片上传'),
|
|
title: propTypes.string.def('图片上传'),
|
|
updateUrl: propTypes.string.def(import.meta.env.VITE_UPLOAD_URL),
|
|
updateUrl: propTypes.string.def(import.meta.env.VITE_UPLOAD_URL),
|
|
fileType: propTypes.array.def(['jpg', 'png', 'gif', 'jpeg']), // 文件类型, 例如['png', 'jpg', 'jpeg']
|
|
fileType: propTypes.array.def(['jpg', 'png', 'gif', 'jpeg']), // 文件类型, 例如['png', 'jpg', 'jpeg']
|
|
@@ -44,6 +47,7 @@ const props = defineProps({
|
|
isShowTip: propTypes.bool.def(false) // 是否显示提示
|
|
isShowTip: propTypes.bool.def(false) // 是否显示提示
|
|
})
|
|
})
|
|
// ========== 上传相关 ==========
|
|
// ========== 上传相关 ==========
|
|
|
|
+const valueRef = ref(props.modelValue)
|
|
const uploadRef = ref<UploadInstance>()
|
|
const uploadRef = ref<UploadInstance>()
|
|
const uploadList = ref<UploadUserFile[]>([])
|
|
const uploadList = ref<UploadUserFile[]>([])
|
|
const fileList = ref<UploadUserFile[]>([])
|
|
const fileList = ref<UploadUserFile[]>([])
|
|
@@ -55,15 +59,15 @@ const uploadHeaders = ref({
|
|
'tenant-id': getTenantId()
|
|
'tenant-id': getTenantId()
|
|
})
|
|
})
|
|
watch(
|
|
watch(
|
|
- () => props.imgs,
|
|
|
|
|
|
+ () => props.modelValue,
|
|
(val) => {
|
|
(val) => {
|
|
if (val) {
|
|
if (val) {
|
|
// 首先将值转为数组, 当只穿了一个图片时,会报map方法错误
|
|
// 首先将值转为数组, 当只穿了一个图片时,会报map方法错误
|
|
- const list = Array.isArray(props.imgs)
|
|
|
|
- ? props.imgs
|
|
|
|
- : Array.isArray(props.imgs?.split(','))
|
|
|
|
- ? props.imgs?.split(',')
|
|
|
|
- : Array.of(props.imgs)
|
|
|
|
|
|
+ const list = Array.isArray(props.modelValue)
|
|
|
|
+ ? props.modelValue
|
|
|
|
+ : Array.isArray(props.modelValue?.split(','))
|
|
|
|
+ ? props.modelValue?.split(',')
|
|
|
|
+ : Array.of(props.modelValue)
|
|
// 然后将数组转为对象数组
|
|
// 然后将数组转为对象数组
|
|
fileList.value = list.map((item) => {
|
|
fileList.value = list.map((item) => {
|
|
if (typeof item === 'string') {
|
|
if (typeof item === 'string') {
|
|
@@ -84,6 +88,10 @@ watch(
|
|
)
|
|
)
|
|
// 文件上传之前判断
|
|
// 文件上传之前判断
|
|
const beforeUpload: UploadProps['beforeUpload'] = (file: UploadRawFile) => {
|
|
const beforeUpload: UploadProps['beforeUpload'] = (file: UploadRawFile) => {
|
|
|
|
+ if (fileList.value.length >= props.limit) {
|
|
|
|
+ message.error(`上传文件数量不能超过${props.limit}个!`)
|
|
|
|
+ return false
|
|
|
|
+ }
|
|
let fileExtension = ''
|
|
let fileExtension = ''
|
|
if (file.name.lastIndexOf('.') > -1) {
|
|
if (file.name.lastIndexOf('.') > -1) {
|
|
fileExtension = file.name.slice(file.name.lastIndexOf('.') + 1)
|
|
fileExtension = file.name.slice(file.name.lastIndexOf('.') + 1)
|
|
@@ -111,14 +119,12 @@ const beforeUpload: UploadProps['beforeUpload'] = (file: UploadRawFile) => {
|
|
// 文件上传成功
|
|
// 文件上传成功
|
|
const handleFileSuccess: UploadProps['onSuccess'] = (res: any): void => {
|
|
const handleFileSuccess: UploadProps['onSuccess'] = (res: any): void => {
|
|
message.success('上传成功')
|
|
message.success('上传成功')
|
|
- console.info(uploadList.value)
|
|
|
|
- console.info(fileList.value)
|
|
|
|
uploadList.value.push({ name: res.data, url: res.data })
|
|
uploadList.value.push({ name: res.data, url: res.data })
|
|
if (uploadList.value.length == uploadNumber.value) {
|
|
if (uploadList.value.length == uploadNumber.value) {
|
|
fileList.value = fileList.value.concat(uploadList.value)
|
|
fileList.value = fileList.value.concat(uploadList.value)
|
|
uploadList.value = []
|
|
uploadList.value = []
|
|
uploadNumber.value = 0
|
|
uploadNumber.value = 0
|
|
- emit('input', listToString(fileList.value))
|
|
|
|
|
|
+ emit('update:modelValue', listToString(fileList.value))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
// 文件数超出提示
|
|
// 文件数超出提示
|
|
@@ -134,7 +140,7 @@ const handleRemove = (file) => {
|
|
const findex = fileList.value.map((f) => f.name).indexOf(file.name)
|
|
const findex = fileList.value.map((f) => f.name).indexOf(file.name)
|
|
if (findex > -1) {
|
|
if (findex > -1) {
|
|
fileList.value.splice(findex, 1)
|
|
fileList.value.splice(findex, 1)
|
|
- emit('input', listToString(fileList.value))
|
|
|
|
|
|
+ emit('update:modelValue', listToString(fileList.value))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
// 对象转成指定字符串分隔
|
|
// 对象转成指定字符串分隔
|