提交 | 用户 | 时间
|
820397
|
1 |
<template> |
H |
2 |
<Dialog v-model="dialogVisible" title="用户导入" width="400"> |
|
3 |
<el-upload |
|
4 |
ref="uploadRef" |
|
5 |
v-model:file-list="fileList" |
|
6 |
:action="importUrl + '?updateSupport=' + updateSupport" |
|
7 |
:auto-upload="false" |
|
8 |
:disabled="formLoading" |
|
9 |
:headers="uploadHeaders" |
|
10 |
:limit="1" |
|
11 |
:on-error="submitFormError" |
|
12 |
:on-exceed="handleExceed" |
|
13 |
:on-success="submitFormSuccess" |
|
14 |
accept=".xlsx, .xls" |
|
15 |
drag |
|
16 |
> |
|
17 |
<Icon icon="ep:upload" /> |
|
18 |
<div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div> |
|
19 |
<template #tip> |
|
20 |
<div class="el-upload__tip text-center"> |
|
21 |
<div class="el-upload__tip"> |
|
22 |
<el-checkbox v-model="updateSupport" /> |
|
23 |
是否更新已经存在的用户数据 |
|
24 |
</div> |
|
25 |
<span>仅允许导入 xls、xlsx 格式文件。</span> |
|
26 |
<el-link |
|
27 |
:underline="false" |
|
28 |
style="font-size: 12px; vertical-align: baseline" |
|
29 |
type="primary" |
|
30 |
@click="importTemplate" |
|
31 |
> |
|
32 |
下载模板 |
|
33 |
</el-link> |
|
34 |
</div> |
|
35 |
</template> |
|
36 |
</el-upload> |
|
37 |
<template #footer> |
|
38 |
<el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button> |
|
39 |
<el-button @click="dialogVisible = false">取 消</el-button> |
|
40 |
</template> |
|
41 |
</Dialog> |
|
42 |
</template> |
|
43 |
<script lang="ts" setup> |
|
44 |
import * as UserApi from '@/api/system/user' |
|
45 |
import { getAccessToken, getTenantId } from '@/utils/auth' |
|
46 |
import download from '@/utils/download' |
|
47 |
|
|
48 |
defineOptions({ name: 'SystemUserImportForm' }) |
|
49 |
|
|
50 |
const message = useMessage() // 消息弹窗 |
|
51 |
|
|
52 |
const dialogVisible = ref(false) // 弹窗的是否展示 |
|
53 |
const formLoading = ref(false) // 表单的加载中 |
|
54 |
const uploadRef = ref() |
|
55 |
const importUrl = |
|
56 |
import.meta.env.VITE_BASE_URL + import.meta.env.VITE_API_URL + '/system/user/import' |
|
57 |
const uploadHeaders = ref() // 上传 Header 头 |
|
58 |
const fileList = ref([]) // 文件列表 |
|
59 |
const updateSupport = ref(0) // 是否更新已经存在的用户数据 |
|
60 |
|
|
61 |
/** 打开弹窗 */ |
|
62 |
const open = () => { |
|
63 |
dialogVisible.value = true |
|
64 |
updateSupport.value = 0 |
|
65 |
fileList.value = [] |
|
66 |
resetForm() |
|
67 |
} |
|
68 |
defineExpose({ open }) // 提供 open 方法,用于打开弹窗 |
|
69 |
|
|
70 |
/** 提交表单 */ |
|
71 |
const submitForm = async () => { |
|
72 |
if (fileList.value.length == 0) { |
|
73 |
message.error('请上传文件') |
|
74 |
return |
|
75 |
} |
|
76 |
// 提交请求 |
|
77 |
uploadHeaders.value = { |
|
78 |
Authorization: 'Bearer ' + getAccessToken(), |
|
79 |
'tenant-id': getTenantId() |
|
80 |
} |
|
81 |
formLoading.value = true |
|
82 |
uploadRef.value!.submit() |
|
83 |
} |
|
84 |
|
|
85 |
/** 文件上传成功 */ |
|
86 |
const emits = defineEmits(['success']) |
|
87 |
const submitFormSuccess = (response: any) => { |
|
88 |
if (response.code !== 0) { |
|
89 |
message.error(response.msg) |
|
90 |
formLoading.value = false |
|
91 |
return |
|
92 |
} |
|
93 |
// 拼接提示语 |
|
94 |
const data = response.data |
|
95 |
let text = '上传成功数量:' + data.createUsernames.length + ';' |
|
96 |
for (let username of data.createUsernames) { |
|
97 |
text += '< ' + username + ' >' |
|
98 |
} |
|
99 |
text += '更新成功数量:' + data.updateUsernames.length + ';' |
|
100 |
for (const username of data.updateUsernames) { |
|
101 |
text += '< ' + username + ' >' |
|
102 |
} |
|
103 |
text += '更新失败数量:' + Object.keys(data.failureUsernames).length + ';' |
|
104 |
for (const username in data.failureUsernames) { |
|
105 |
text += '< ' + username + ': ' + data.failureUsernames[username] + ' >' |
|
106 |
} |
|
107 |
message.alert(text) |
|
108 |
formLoading.value = false |
|
109 |
dialogVisible.value = false |
|
110 |
// 发送操作成功的事件 |
|
111 |
emits('success') |
|
112 |
} |
|
113 |
|
|
114 |
/** 上传错误提示 */ |
|
115 |
const submitFormError = (): void => { |
|
116 |
message.error('上传失败,请您重新上传!') |
|
117 |
formLoading.value = false |
|
118 |
} |
|
119 |
|
|
120 |
/** 重置表单 */ |
|
121 |
const resetForm = async (): Promise<void> => { |
|
122 |
// 重置上传状态和文件 |
|
123 |
formLoading.value = false |
|
124 |
await nextTick() |
|
125 |
uploadRef.value?.clearFiles() |
|
126 |
} |
|
127 |
|
|
128 |
/** 文件数超出提示 */ |
|
129 |
const handleExceed = (): void => { |
|
130 |
message.error('最多只能上传一个文件!') |
|
131 |
} |
|
132 |
|
|
133 |
/** 下载模板操作 */ |
|
134 |
const importTemplate = async () => { |
|
135 |
const res = await UserApi.importUserTemplate() |
|
136 |
download.excel(res, '用户导入模版.xls') |
|
137 |
} |
|
138 |
</script> |