chore: 优化上传组件

This commit is contained in:
JaguarJack 2023-05-16 10:39:39 +08:00
parent 2772c3322f
commit 960576e286
2 changed files with 36 additions and 4 deletions

View File

@ -1,4 +1,3 @@
<el-form-item label="{label}" prop="{prop}"> <el-form-item label="{label}" prop="{prop}">
<Upload action="upload/image" :show-file-list="false" name="image" :on-success="uploadSuccess"> <Upload v-model="{model-value}" />
</Upload>
</el-form-item> </el-form-item>

View File

@ -1,8 +1,21 @@
<template> <template>
<el-upload ref="upload" :action="actionApi" :auto-upload="auto" :headers="{ authorization: token, 'Request-from': 'Dashboard' }" v-bind="$attrs"> <el-upload
ref="upload"
:action="actionApi"
:show-file-list="false"
name="image"
:auto-upload="auto"
:headers="{ authorization: token, 'Request-from': 'Dashboard' }"
v-bind="$attrs"
:on-success="handleSuccess"
>
<template v-for="(index, name) in $slots" v-slot:[name]> <template v-for="(index, name) in $slots" v-slot:[name]>
<slot :name="name"></slot> <slot :name="name"></slot>
</template> </template>
<img :src="modelValue" v-if="modelValue" :class="imageClass" />
<div v-else class="w-24 h-24 border-blue-100 border-dashed border rounded flex justify-center pt-8">
<Icon name="plus" />
</div>
</el-upload> </el-upload>
</template> </template>
@ -10,18 +23,30 @@
import { ref } from 'vue' import { ref } from 'vue'
import { env } from '/admin/support/helper' import { env } from '/admin/support/helper'
import { getAuthToken } from '/admin/support/helper' import { getAuthToken } from '/admin/support/helper'
import { Code } from '/admin/enum/app'
import Message from '/admin/support/message'
const props = defineProps({ const props = defineProps({
action: { action: {
type: String, type: String,
default: 'upload', default: 'upload/image',
}, },
auto: { auto: {
type: Boolean, type: Boolean,
default: true, default: true,
}, },
modelValue: {
type: String,
default: '',
require: true,
},
imageClass: {
type: String,
default: '',
},
}) })
const emits = defineEmits(['update:modelValue'])
const baseURL = env('VITE_BASE_URL') const baseURL = env('VITE_BASE_URL')
const actionApi = ref<string>('') const actionApi = ref<string>('')
@ -30,4 +55,12 @@ actionApi.value = baseURL + props.action
const token = ref<string>() const token = ref<string>()
token.value = 'Bearer ' + getAuthToken() token.value = 'Bearer ' + getAuthToken()
const handleSuccess = (response: any, uploadFile) => {
if (response.code === Code.SUCCESS) {
emits('update:modelValue', response.data.path)
} else {
Message.error(response.message)
}
}
</script> </script>