Skip to content

Commit cebe6f9

Browse files
committed
REVIEW 用户管理(导入用户)
1 parent 0a69041 commit cebe6f9

File tree

5 files changed

+136
-161
lines changed

5 files changed

+136
-161
lines changed

src/api/system/user/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export const exportUser = (params) => {
4848
}
4949

5050
// 下载用户导入模板
51-
export const importUserTemplateApi = () => {
51+
export const importUserTemplate = () => {
5252
return request.download({ url: '/system/user/get-import-template' })
5353
}
5454

src/views/bpm/model/ModelImportForm.vue

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

src/views/system/user/components/UserImportForm.vue

Lines changed: 0 additions & 154 deletions
This file was deleted.

src/views/system/user/index.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ import download from '@/utils/download'
194194
import { CommonStatusEnum } from '@/utils/constants'
195195
import * as UserApi from '@/api/system/user'
196196
import UserForm from './UserForm.vue'
197-
import UserImportForm from './components/UserImportForm.vue'
197+
import UserImportForm from './UserImportForm.vue'
198198
import UserAssignRoleForm from './UserAssignRoleForm.vue'
199199
import DeptTree from './DeptTree.vue'
200200
const message = useMessage() // 消息弹窗
@@ -250,10 +250,10 @@ const openForm = (type: string, id?: number) => {
250250
formRef.value.open(type, id)
251251
}
252252
253-
// 用户导入
253+
/** 用户导入 */
254254
const importFormRef = ref()
255255
const handleImport = () => {
256-
importFormRef.value?.openForm()
256+
importFormRef.value.open()
257257
}
258258
259259
/** 修改用户状态 */

0 commit comments

Comments
 (0)