index.vue
2.93 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
99
100
101
102
103
104
105
<template>
<div>
<a-upload-dragger
v-model:fileList="fileList.list"
name="file"
:disabled="disabled"
:multiple="false"
@change="handleChange($event)"
:before-upload="() => false"
>
<p class="ant-upload-drag-icon">
<InboxOutlined />
</p>
<p class="ant-upload-text">{{ t('rule.dataflow.index.uploadTooltip') }}</p>
<p class="ant-upload-hint">
{{ t('rule.dataflow.index.uploadType') }}{{ supportFileType.join(' ') }}
<br />
{{ t('rule.dataflow.index.uploadSize') }}{{ fileLimitSize }}M
</p>
</a-upload-dragger>
<Image
class="mt-2"
style="width: 6.25rem; height: 6.25rem"
v-if="url ? url : imgUrl"
:src="url ? url : imgUrl"
/>
</div>
</template>
<script lang="ts" setup name="uploadfile">
import { ref, reactive } from 'vue';
import { InboxOutlined } from '@ant-design/icons-vue';
import { Image } from 'ant-design-vue';
import { useMessage } from '/@/hooks/web/useMessage';
import { uploadApi } from '/@/api/personal/index';
import { useI18n } from '/@/hooks/web/useI18n';
const { t } = useI18n(); // 加载国际化
defineProps({
url: {
type: String,
default: '',
},
disabled: {
type: Boolean,
default: false,
},
});
const emit = defineEmits(['fileUrlEmit']);
const { createMessage } = useMessage();
const imgUrl = ref('');
const fileList = reactive<any>({
list: [],
});
const supportFileType = ['image/png', 'image/png', 'image/jpeg', 'image/gif'];
const fileLimitSize = 5;
//验证图片类型和大小
const beforeUploadVerify = (type, size) => {
let status = false;
const limitImgSize = (size as number) / 1024 / 1024 < fileLimitSize;
if (!supportFileType.includes(type)) {
status = false;
const errorType = `只能上传其中${supportFileType.join(' ')}中的类型!`;
createMessage.error(errorType);
throw Error(errorType);
}
status = true;
if (!limitImgSize) {
status = false;
const errorType = `图片大小不能超过${fileLimitSize}MB!`;
createMessage.error(errorType);
throw Error(errorType);
}
status = true;
return status;
};
const handleChange = async ({ file }) => {
const fileStatus = file.status;
const fileSize = file.size;
const fileType = file.type;
if (fileStatus === 'removed') {
fileList.list = [];
imgUrl.value = '';
} else {
if (!beforeUploadVerify(fileType, fileSize)) return;
fileList.list = [file];
const formData = new FormData();
formData.append('file', file);
const { fileStaticUri } = await uploadApi(formData);
if (!fileStaticUri) return;
imgUrl.value = fileStaticUri;
emit('fileUrlEmit', fileStaticUri);
}
};
</script>
<style lang="less" scoped></style>