Files
fe.ems.vue3/src/components/UploadModal/index.vue
2023-11-08 16:17:29 +08:00

113 lines
2.9 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
import { message } from 'ant-design-vue/lib';
import { FileType } from 'ant-design-vue/lib/upload/interface';
import { UploadRequestOption } from 'ant-design-vue/lib/vc-upload/interface';
import useI18n from '@/hooks/useI18n';
const { t } = useI18n();
const emit = defineEmits(['upload', 'close', 'update:visible']);
const props = defineProps({
/**窗口标题 */
title: {
type: String,
default: '标题',
},
/**是否上传中 */
loading: {
type: Boolean,
default: false,
},
/**是否弹出显示,必传 */
visible: {
type: Boolean,
required: true,
},
/**允许上传的文件拓展类型,默认任意,可指定['.xls', '.xlsx'] */
ext: {
type: Array<string>,
default: [],
},
/**上传文件大小单位MB默认不限制可指定10 */
size: {
type: Number,
default: 0,
},
});
/**弹框关闭事件 */
function fnModalClose() {
if(props.loading) return
emit('close');
}
/**上传前检查或转换压缩 */
function fnBeforeUpload(file: FileType) {
if (props.loading) return false;
// 检查文件大小
if (props.size > 0) {
const fileSize = file.size;
const isLtM = fileSize / 1024 / 1024 < props.size;
if (!isLtM) {
message.error(`${t('components.UploadModal.allowFilter')} ${props.size}MB`, 3);
return false;
}
}
// 检查后缀
if (props.ext.length > 0) {
const fileName = file.name;
const isAllowType = props.ext.some(v => fileName.endsWith(v));
if (!isAllowType) {
message.error(`${t('components.UploadModal.onlyAllow')} ${props.ext.join('、')}`, 3);
return false;
}
}
return true;
}
/**上传请求发出 */
function fnUpload(up: UploadRequestOption) {
emit('upload', up.file)
}
</script>
<template>
<a-modal
width="500px"
:title="props.title"
:visible="props.visible"
:keyboard="false"
:mask-closable="false"
:confirm-loading="props.loading"
:footer="null"
@cancel="fnModalClose"
>
<a-space :size="8" direction="vertical" style="width: 100%">
<a-upload-dragger
:disabled="props.loading"
:accept="props.ext.join(',')"
name="file"
:max-count="1"
:show-upload-list="false"
:before-upload="fnBeforeUpload"
:custom-request="fnUpload"
>
<p class="ant-upload-drag-icon">
<inbox-outlined></inbox-outlined>
</p>
<p class="ant-upload-text">{{t('components.UploadModal.uploadTip')}}</p>
<p class="ant-upload-hint">
<div v-if="props.size > 0">
{{t('components.UploadModal.allowSize')}} {{ props.size }} MB
</div>
<div v-if="props.ext.length > 0">
{{t('components.UploadModal.allowFormat')}} {{ props.ext.join('') }}
</div>
</p>
</a-upload-dragger>
<slot></slot>
</a-space>
</a-modal>
</template>
<style lang="less" scoped></style>