index.vue
1.77 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
<script setup lang="ts">
import { ref, reactive } from "vue";
import { ElMessage } from "element-plus";
// import * as store from "@/pinia/index";
import { useLoginStore } from '@/pinia/login';
const upload = ref();
/**
* @description 组件属性
* @prop {string | null} url 上传地址
* @prop { string | null } method 上传请求方法
*
*/
interface Props {
url: string;
method: string;
}
const props = withDefaults(defineProps<Props>(), {});
const emit = defineEmits<{
(e: "success"): void;
}>();
/**
* @description 上传配置
*/
const setting = reactive({
url: `${import.meta.env.VITE_BASE_URL}${props.url}`,
headers: {
// "X-Token": store.family.loginStore().token,
"X-Token": useLoginStore()?.getToken,
},
loading: false,
});
/**
* 上传文件成功回调
* @param { ElUploadProgressEvent } 回调返回对象
*/
const handleSuccess = (res: ElUploadProgressEvent) => {
setting.loading = false;
if (res.errorCode == 0) {
ElMessage.success("操作成功");
emit("success");
} else {
ElMessage.error(res.errorMessage ?? "操作失败");
}
};
/**
* 上传文件失败回调
* @param { ElUploadProgressEvent } 回调返回对象
*/
const handlError = (err: ElUploadProgressEvent) => {
console.log(err);
};
/**
* 文件上传中回调
*
*/
const handleProgress = () => {
setting.loading = true;
};
</script>
<template>
<el-upload
ref="upload"
:action="setting.url"
:headers="setting.headers"
:method="props.method"
accept=".xlsx,.xls"
name="multipartFile"
:with-credentials="true"
:show-file-list="false"
:on-success="handleSuccess"
:on-error="handlError"
:on-progress="handleProgress"
>
<el-button type="primary" :loading="setting.loading">导入</el-button>
</el-upload>
</template>