CustomUploadComp.vue
2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<script setup name="CustomUploadComp" lang="ts">
import { PlusOutlined, CloseCircleOutlined } from '@ant-design/icons-vue';
import { Upload, Spin } from 'ant-design-vue';
import { ref, watchEffect } from 'vue';
import { useUpload } from './hooks/useUploadFile.hook';
import { logoUpload } from '/@/api/oem';
import { useI18n } from '/@/hooks/web/useI18n';
const { t } = useI18n();
const props = withDefaults(
defineProps<{
imgUrl: string;
}>(),
{
imgUrl: '',
}
);
const emit = defineEmits(['setImg']);
const { beforeUploadVerify } = useUpload();
const uploadLoading = ref(false);
const uploadImgUrl = ref('');
//上传图片接口
const customUpload = async ({ file }) => {
try {
if (beforeUploadVerify(file)) {
uploadImgUrl.value = '';
uploadLoading.value = true;
const formData = new FormData();
formData.append('file', file);
const response = await logoUpload(formData);
if (response.fileStaticUri) {
uploadImgUrl.value = response.fileStaticUri;
emit('setImg', uploadImgUrl.value);
}
}
} finally {
uploadLoading.value = false;
}
};
// 删除图片
const handleDelete = () => {
uploadImgUrl.value = '';
emit('setImg', uploadImgUrl.value);
};
watchEffect(() => {
init();
});
function init() {
if (!props.imgUrl) uploadImgUrl.value = '';
uploadImgUrl.value = props.imgUrl;
}
</script>
<template>
<div>
<Upload
name="avatar"
list-type="picture-card"
class="avatar-uploader"
:show-upload-list="false"
:customRequest="customUpload"
:before-upload="beforeUploadVerify"
>
<div v-if="uploadImgUrl" class="w-full h-full modal-div relative">
<img class="fill-img" :src="uploadImgUrl" alt="avatar" />
<CloseCircleOutlined class="absolute icon-delete" @click.stop="handleDelete" />
</div>
<div v-else>
<Spin v-if="uploadLoading" :tip="t('system.platform.uploadingText')" />
<PlusOutlined v-else />
<div class="ant-upload-text">
{{ t('system.platform.uploadText') }}
</div>
</div>
</Upload>
</div>
</template>
<style lang="less" scoped>
.fill-img {
width: 100%;
height: 100%;
}
.icon-delete {
top: -13px;
right: -13px;
color: rgb(146, 145, 145);
font-size: 1rem;
z-index: 9999;
}
</style>