|
| 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> |
0 commit comments