UserImportForm.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <template>
  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" name="SystemUserImportForm" setup>
  44. import * as UserApi from '@/api/system/user'
  45. import { getAccessToken, getTenantId } from '@/utils/auth'
  46. import download from '@/utils/download'
  47. const message = useMessage() // 消息弹窗
  48. const dialogVisible = 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. const open = () => {
  58. dialogVisible.value = true
  59. resetForm()
  60. }
  61. defineExpose({ open }) // 提供 open 方法,用于打开弹窗
  62. /** 提交表单 */
  63. const submitForm = async () => {
  64. if (fileList.value.length == 0) {
  65. message.error('请上传文件')
  66. return
  67. }
  68. // 提交请求
  69. uploadHeaders.value = {
  70. Authorization: 'Bearer ' + getAccessToken(),
  71. 'tenant-id': getTenantId()
  72. }
  73. formLoading.value = true
  74. uploadRef.value!.submit()
  75. }
  76. /** 文件上传成功 */
  77. const emits = defineEmits(['success'])
  78. const submitFormSuccess = (response: any) => {
  79. if (response.code !== 0) {
  80. message.error(response.msg)
  81. formLoading.value = false
  82. return
  83. }
  84. // 拼接提示语
  85. const data = response.data
  86. let text = '上传成功数量:' + data.createUsernames.length + ';'
  87. for (let username of data.createUsernames) {
  88. text += '< ' + username + ' >'
  89. }
  90. text += '更新成功数量:' + data.updateUsernames.length + ';'
  91. for (const username of data.updateUsernames) {
  92. text += '< ' + username + ' >'
  93. }
  94. text += '更新失败数量:' + Object.keys(data.failureUsernames).length + ';'
  95. for (const username in data.failureUsernames) {
  96. text += '< ' + username + ': ' + data.failureUsernames[username] + ' >'
  97. }
  98. message.alert(text)
  99. // 发送操作成功的事件
  100. emits('success')
  101. }
  102. /** 上传错误提示 */
  103. const submitFormError = (): void => {
  104. message.error('上传失败,请您重新上传!')
  105. formLoading.value = false
  106. }
  107. /** 重置表单 */
  108. const resetForm = () => {
  109. // 重置上传状态和文件
  110. formLoading.value = false
  111. uploadRef.value?.clearFiles()
  112. }
  113. /** 文件数超出提示 */
  114. const handleExceed = (): void => {
  115. message.error('最多只能上传一个文件!')
  116. }
  117. /** 下载模板操作 */
  118. const importTemplate = async () => {
  119. const res = await UserApi.importUserTemplate()
  120. download.excel(res, '用户导入模版.xls')
  121. }
  122. </script>