|
|
@@ -1,10 +1,8 @@
|
|
|
<script lang="ts" setup>
|
|
|
-import { useUserStore } from '@/stores/user'
|
|
|
-import { ACCESS_TOKEN } from '@/utils/constants'
|
|
|
-import { ElMessage } from 'element-plus'
|
|
|
-import { isAbsolutePath, ossUpload } from '@/utils/utils'
|
|
|
+import { isAbsolutePath } from '@/utils/utils'
|
|
|
import config from '@/config/defaultSetting'
|
|
|
-import type { UploadProps, UploadFile } from 'element-plus'
|
|
|
+import { useUpload } from '@/hooks/useUpload'
|
|
|
+import type { UploadFile } from 'element-plus'
|
|
|
import type { BasicFormItem } from '@/types/form'
|
|
|
|
|
|
interface Props {
|
|
|
@@ -13,32 +11,22 @@ interface Props {
|
|
|
size?: number | string
|
|
|
iconSize?: number | string
|
|
|
fileSize?: number | string
|
|
|
- uploadApi?: string
|
|
|
+ action?: string
|
|
|
oss?: boolean
|
|
|
limit?: number
|
|
|
}
|
|
|
const props = withDefaults(defineProps<Props>(), {
|
|
|
size: '148px',
|
|
|
iconSize: '28px',
|
|
|
- uploadApi: config.uploadApi,
|
|
|
+ action: config.uploadApi,
|
|
|
fileSize: 10,
|
|
|
oss: config.oss,
|
|
|
limit: 1
|
|
|
})
|
|
|
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 headers = reactive({
|
|
|
- [ACCESS_TOKEN]: user.token
|
|
|
-})
|
|
|
-
|
|
|
const limit = Number(props.limit) || 1
|
|
|
+const { modelValue, headers, baseApi, uploadList, action, uploadAttrs, beforeUpload } = useUpload(props, emits)
|
|
|
|
|
|
// 生成图片地址
|
|
|
const prefix = props.oss ? config.ossHost : baseApi
|
|
|
@@ -51,26 +39,6 @@ const genImageUrl = (url: string) => {
|
|
|
}
|
|
|
|
|
|
const uploadRef = ref()
|
|
|
-// 在上传之前对文件进行验证
|
|
|
-const fileSize = Number(props.fileSize) || 10
|
|
|
-const beforeUpload: UploadProps['beforeUpload'] = rawFile => {
|
|
|
- if (rawFile.size / 1024 / 1024 > fileSize) {
|
|
|
- ElMessage.error(`图片大小不能超过${fileSize}MB!`)
|
|
|
- return false
|
|
|
- }
|
|
|
- return true
|
|
|
-}
|
|
|
-
|
|
|
-// 图片上传成功
|
|
|
-const uploadList = ref([...props.modelValue])
|
|
|
-const handleUploadSuccess: UploadProps['onSuccess'] = (response, uploadFile: UploadFile, uploadFiles: UploadFile[]) => {
|
|
|
- if (response.success || response.code === 200) {
|
|
|
- // 附加oss路径
|
|
|
- uploadList.value = uploadFiles
|
|
|
- } else {
|
|
|
- ElMessage.error(response.msg)
|
|
|
- }
|
|
|
-}
|
|
|
// 删除图片
|
|
|
const handleRemove = (file: UploadFile) => {
|
|
|
uploadRef.value.handleRemove(file)
|
|
|
@@ -87,16 +55,6 @@ const handlePreview = (file: UploadFile) => {
|
|
|
const closeViewer = () => {
|
|
|
showViewer.value = false
|
|
|
}
|
|
|
-
|
|
|
-// 动态属性
|
|
|
-const attrs: any = props.oss ? { 'http-request': ossUpload } : {}
|
|
|
-if (props.item) {
|
|
|
- // 接口需要的属性
|
|
|
- props.item.extra = computed(() => uploadList.value.map((item: any) => item.response?.url || item.url))
|
|
|
- if (!props.item.props?.onSuccess) {
|
|
|
- attrs['on-success'] = handleUploadSuccess
|
|
|
- }
|
|
|
-}
|
|
|
</script>
|
|
|
|
|
|
<template>
|
|
|
@@ -104,13 +62,13 @@ if (props.item) {
|
|
|
ref="uploadRef"
|
|
|
v-model:file-list="modelValue"
|
|
|
:class="{ 'is-disabled': modelValue.length >= limit }"
|
|
|
- :action="isAbsolutePath(props.uploadApi) ? props.uploadApi : baseApi + props.uploadApi"
|
|
|
+ :action="action"
|
|
|
:headers="headers"
|
|
|
:before-upload="beforeUpload"
|
|
|
:limit="limit"
|
|
|
list-type="picture-card"
|
|
|
accept="image/*"
|
|
|
- v-bind="{ ...attrs, ...$attrs }"
|
|
|
+ v-bind="{ ...uploadAttrs, ...$attrs }"
|
|
|
>
|
|
|
<el-icon class="avatar-uploader-icon" :style="{ fontSize: iconSize }"><Plus /></el-icon>
|
|
|
<template #file="{ file }">
|