提交 | 用户 | 时间
|
820397
|
1 |
<template> |
H |
2 |
<Dialog v-model="dialogVisible" title="上传文件"> |
|
3 |
<el-upload |
|
4 |
ref="uploadRef" |
|
5 |
v-model:file-list="fileList" |
|
6 |
:action="uploadUrl" |
|
7 |
:auto-upload="false" |
|
8 |
:data="data" |
|
9 |
:disabled="formLoading" |
|
10 |
:limit="1" |
|
11 |
:on-change="handleFileChange" |
|
12 |
:on-error="submitFormError" |
|
13 |
:on-exceed="handleExceed" |
|
14 |
:on-success="submitFormSuccess" |
|
15 |
:http-request="httpRequest" |
|
16 |
accept=".jpg, .png, .gif" |
|
17 |
drag |
|
18 |
> |
|
19 |
<i class="el-icon-upload"></i> |
|
20 |
<div class="el-upload__text"> 将文件拖到此处,或 <em>点击上传</em></div> |
|
21 |
<template #tip> |
|
22 |
<div class="el-upload__tip" style="color: red"> |
|
23 |
提示:仅允许导入 jpg、png、gif 格式文件! |
|
24 |
</div> |
|
25 |
</template> |
|
26 |
</el-upload> |
|
27 |
<template #footer> |
|
28 |
<el-button :disabled="formLoading" type="primary" @click="submitFileForm">确 定</el-button> |
|
29 |
<el-button @click="dialogVisible = false">取 消</el-button> |
|
30 |
</template> |
|
31 |
</Dialog> |
|
32 |
</template> |
|
33 |
<script lang="ts" setup> |
|
34 |
import { useUpload } from '@/components/UploadFile/src/useUpload' |
|
35 |
|
|
36 |
defineOptions({ name: 'InfraFileForm' }) |
|
37 |
|
|
38 |
const { t } = useI18n() // 国际化 |
|
39 |
const message = useMessage() // 消息弹窗 |
|
40 |
|
|
41 |
const dialogVisible = ref(false) // 弹窗的是否展示 |
|
42 |
const formLoading = ref(false) // 表单的加载中 |
|
43 |
const fileList = ref([]) // 文件列表 |
|
44 |
const data = ref({ path: '' }) |
|
45 |
const uploadRef = ref() |
|
46 |
|
|
47 |
const { uploadUrl, httpRequest } = useUpload() |
|
48 |
|
|
49 |
/** 打开弹窗 */ |
|
50 |
const open = async () => { |
|
51 |
dialogVisible.value = true |
|
52 |
resetForm() |
|
53 |
} |
|
54 |
defineExpose({ open }) // 提供 open 方法,用于打开弹窗 |
|
55 |
|
|
56 |
/** 处理上传的文件发生变化 */ |
|
57 |
const handleFileChange = (file) => { |
|
58 |
data.value.path = file.name |
|
59 |
} |
|
60 |
|
|
61 |
/** 提交表单 */ |
|
62 |
const submitFileForm = () => { |
|
63 |
if (fileList.value.length == 0) { |
|
64 |
message.error('请上传文件') |
|
65 |
return |
|
66 |
} |
|
67 |
unref(uploadRef)?.submit() |
|
68 |
} |
|
69 |
|
|
70 |
/** 文件上传成功处理 */ |
|
71 |
const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调 |
|
72 |
const submitFormSuccess = () => { |
|
73 |
// 清理 |
|
74 |
dialogVisible.value = false |
|
75 |
formLoading.value = false |
|
76 |
unref(uploadRef)?.clearFiles() |
|
77 |
// 提示成功,并刷新 |
|
78 |
message.success(t('common.createSuccess')) |
|
79 |
emit('success') |
|
80 |
} |
|
81 |
|
|
82 |
/** 上传错误提示 */ |
|
83 |
const submitFormError = (): void => { |
|
84 |
message.error('上传失败,请您重新上传!') |
|
85 |
formLoading.value = false |
|
86 |
} |
|
87 |
|
|
88 |
/** 重置表单 */ |
|
89 |
const resetForm = () => { |
|
90 |
// 重置上传状态和文件 |
|
91 |
formLoading.value = false |
|
92 |
uploadRef.value?.clearFiles() |
|
93 |
} |
|
94 |
|
|
95 |
/** 文件数超出提示 */ |
|
96 |
const handleExceed = (): void => { |
|
97 |
message.error('最多只能上传一个文件!') |
|
98 |
} |
|
99 |
</script> |