REVIEW 用户管理(导入用户)

This commit is contained in:
YunaiV 2023-04-01 00:04:28 +08:00
parent 0a69041602
commit cebe6f93db
5 changed files with 136 additions and 161 deletions

View File

@ -48,7 +48,7 @@ export const exportUser = (params) => {
} }
// 下载用户导入模板 // 下载用户导入模板
export const importUserTemplateApi = () => { export const importUserTemplate = () => {
return request.download({ url: '/system/user/get-import-template' }) return request.download({ url: '/system/user/get-import-template' })
} }

View File

@ -8,7 +8,7 @@
:data="formData" :data="formData"
name="bpmnFile" name="bpmnFile"
v-model:file-list="fileList" v-model:file-list="fileList"
:drag="true" drag
:auto-upload="false" :auto-upload="false"
accept=".bpmn, .xml" accept=".bpmn, .xml"
:limit="1" :limit="1"
@ -77,7 +77,7 @@ const open = async () => {
} }
defineExpose({ open }) // open defineExpose({ open }) // open
/** 重置表单 */ /** 提交表单 */
const submitForm = async () => { const submitForm = async () => {
// //
if (!formRef) return if (!formRef) return
@ -98,7 +98,7 @@ const submitForm = async () => {
/** 文件上传成功 */ /** 文件上传成功 */
const emit = defineEmits(['success']) // success const emit = defineEmits(['success']) // success
const submitFormSuccess = async (response: any): Promise<void> => { const submitFormSuccess = async (response: any) => {
if (response.code !== 0) { if (response.code !== 0) {
message.error(response.msg) message.error(response.msg)
formLoading.value = false formLoading.value = false

View File

@ -0,0 +1,129 @@
<template>
<Dialog title="用户导入" v-model="modelVisible" width="400">
<el-upload
ref="uploadRef"
:action="importUrl + '?updateSupport=' + updateSupport"
:headers="uploadHeaders"
v-model:file-list="fileList"
drag
accept=".xlsx, .xls"
:limit="1"
:on-success="submitFormSuccess"
:on-exceed="handleExceed"
:on-error="submitFormError"
:auto-upload="false"
:disabled="formLoading"
>
<Icon icon="ep:upload" />
<div class="el-upload__text">将文件拖到此处<em>点击上传</em></div>
<template #tip>
<div class="el-upload__tip text-center">
<div class="el-upload__tip">
<el-checkbox v-model="updateSupport" /> 是否更新已经存在的用户数据
</div>
<span>仅允许导入 xlsxlsx 格式文件</span>
<el-link
type="primary"
:underline="false"
style="font-size: 12px; vertical-align: baseline"
@click="importTemplate"
>
下载模板
</el-link>
</div>
</template>
</el-upload>
<template #footer>
<el-button @click="submitForm" type="primary" :disabled="formLoading"> </el-button>
<el-button @click="modelVisible = false"> </el-button>
</template>
</Dialog>
</template>
<script lang="ts" setup>
import * as UserApi from '@/api/system/user'
import { getAccessToken, getTenantId } from '@/utils/auth'
import download from '@/utils/download'
const message = useMessage() //
const modelVisible = ref(false) //
const formLoading = ref(false) //
const uploadRef = ref()
const importUrl =
import.meta.env.VITE_BASE_URL + import.meta.env.VITE_API_URL + '/system/user/import'
const uploadHeaders = ref() // Header
const fileList = ref([]) //
const updateSupport = ref(0) //
/** 打开弹窗 */
const open = () => {
modelVisible.value = true
resetForm()
}
defineExpose({ open }) // open
/** 提交表单 */
const submitForm = async () => {
if (fileList.value.length == 0) {
message.error('请上传文件')
return
}
//
uploadHeaders.value = {
Authorization: 'Bearer ' + getAccessToken(),
'tenant-id': getTenantId()
}
formLoading.value = true
uploadRef.value!.submit()
}
/** 文件上传成功 */
const emits = defineEmits(['success'])
const submitFormSuccess = (response: any) => {
if (response.code !== 0) {
message.error(response.msg)
formLoading.value = false
return
}
//
const data = response.data
let text = '上传成功数量:' + data.createUsernames.length + ';'
for (let username of data.createUsernames) {
text += '< ' + username + ' >'
}
text += '更新成功数量:' + data.updateUsernames.length + ';'
for (const username of data.updateUsernames) {
text += '< ' + username + ' >'
}
text += '更新失败数量:' + Object.keys(data.failureUsernames).length + ';'
for (const username in data.failureUsernames) {
text += '< ' + username + ': ' + data.failureUsernames[username] + ' >'
}
message.alert(text)
//
emits('success')
}
/** 上传错误提示 */
const submitFormError = (): void => {
message.error('上传失败,请您重新上传!')
formLoading.value = false
}
/** 重置表单 */
const resetForm = () => {
//
formLoading.value = false
uploadRef.value?.clearFiles()
}
/** 文件数超出提示 */
const handleExceed = (): void => {
message.error('最多只能上传一个文件!')
}
/** 下载模板操作 */
const importTemplate = async () => {
const res = await UserApi.importUserTemplate()
download.excel(res, '用户导入模版.xls')
}
</script>

View File

@ -1,154 +0,0 @@
<template>
<Dialog
:title="upload.title"
:modelValue="showDialog"
width="400px"
append-to-body
@close="closeDialog"
>
<el-upload
ref="uploadRef"
accept=".xlsx, .xls"
:limit="1"
:headers="upload.headers"
:action="upload.url + '?updateSupport=' + upload.updateSupport"
:disabled="upload.isUploading"
:on-progress="handleFileUploadProgress"
:on-success="handleFileSuccess"
:on-exceed="handleExceed"
:on-error="excelUploadError"
:auto-upload="false"
drag
>
<Icon icon="ep:upload" />
<div class="el-upload__text">将文件拖到此处<em>点击上传</em></div>
<template #tip>
<div class="el-upload__tip text-center">
<div class="el-upload__tip">
<el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据
</div>
<span>仅允许导入xlsxlsx格式文件</span>
<el-link
type="primary"
:underline="false"
style="font-size: 12px; vertical-align: baseline"
@click="importTemplate"
>下载模板</el-link
>
</div>
</template>
</el-upload>
<template #footer>
<div class="dialog-footer">
<el-button type="primary" @click="submitFileForm"> </el-button>
<el-button @click="cancel"> </el-button>
</div>
</template>
</Dialog>
</template>
<script lang="ts" setup>
import { importUserTemplateApi } from '@/api/system/user'
import { getAccessToken, getTenantId } from '@/utils/auth'
import download from '@/utils/download'
const emits = defineEmits(['success'])
const message = useMessage() //
const showDialog = ref(false)
const uploadRef = ref()
//
const upload = reactive({
// //
// open: false,
//
title: '用户导入',
//
isUploading: false,
//
updateSupport: 0,
//
headers: {
Authorization: 'Bearer ' + getAccessToken(),
'tenant-id': getTenantId()
},
//
url: import.meta.env.VITE_BASE_URL + import.meta.env.VITE_API_URL + '/system/user/import'
})
//
const handleFileUploadProgress = () => {
upload.isUploading = true
}
//
const handleFileSuccess = (response: any) => {
if (response.code !== 0) {
message.error(response.msg)
return
}
upload.isUploading = false
uploadRef.value?.clearFiles()
//
const data = response.data
let text = '上传成功数量:' + data.createUsernames.length + ';'
for (let username of data.createUsernames) {
text += '< ' + username + ' >'
}
text += '更新成功数量:' + data.updateUsernames.length + ';'
for (const username of data.updateUsernames) {
text += '< ' + username + ' >'
}
text += '更新失败数量:' + Object.keys(data.failureUsernames).length + ';'
for (const username in data.failureUsernames) {
text += '< ' + username + ': ' + data.failureUsernames[username] + ' >'
}
message.alert(text)
emits('success')
closeDialog()
}
//
const handleExceed = (): void => {
message.error('最多只能上传一个文件!')
}
//
const excelUploadError = (e): void => {
console.log(e)
message.error('导入数据失败,请您重新上传!')
}
/** 下载模板操作 */
const importTemplate = async () => {
try {
const res = await importUserTemplateApi()
download.excel(res, '用户导入模版.xls')
} catch (error) {
console.error(error)
}
}
/* 弹框按钮操作 */
//
const cancel = () => {
closeDialog()
}
//
const closeDialog = () => {
showDialog.value = false
}
//
const submitFileForm = () => {
uploadRef.value?.submit()
}
const openForm = () => {
uploadRef.value?.clearFiles()
showDialog.value = true
}
defineExpose({
openForm
})
</script>

View File

@ -194,7 +194,7 @@ import download from '@/utils/download'
import { CommonStatusEnum } from '@/utils/constants' import { CommonStatusEnum } from '@/utils/constants'
import * as UserApi from '@/api/system/user' import * as UserApi from '@/api/system/user'
import UserForm from './UserForm.vue' import UserForm from './UserForm.vue'
import UserImportForm from './components/UserImportForm.vue' import UserImportForm from './UserImportForm.vue'
import UserAssignRoleForm from './UserAssignRoleForm.vue' import UserAssignRoleForm from './UserAssignRoleForm.vue'
import DeptTree from './DeptTree.vue' import DeptTree from './DeptTree.vue'
const message = useMessage() // const message = useMessage() //
@ -250,10 +250,10 @@ const openForm = (type: string, id?: number) => {
formRef.value.open(type, id) formRef.value.open(type, id)
} }
// /** 用户导入 */
const importFormRef = ref() const importFormRef = ref()
const handleImport = () => { const handleImport = () => {
importFormRef.value?.openForm() importFormRef.value.open()
} }
/** 修改用户状态 */ /** 修改用户状态 */