UserImportForm.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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" setup>
  44. import * as UserApi from '@/api/system/user'
  45. import { getAccessToken, getTenantId } from '@/utils/auth'
  46. import download from '@/utils/download'
  47. defineOptions({ name: 'SystemUserImportForm' })
  48. const message = useMessage() // 消息弹窗
  49. const dialogVisible = ref(false) // 弹窗的是否展示
  50. const formLoading = ref(false) // 表单的加载中
  51. const uploadRef = ref()
  52. const importUrl =
  53. import.meta.env.VITE_BASE_URL + import.meta.env.VITE_API_URL + '/system/user/import'
  54. const uploadHeaders = ref() // 上传 Header 头
  55. const fileList = ref([]) // 文件列表
  56. const updateSupport = ref(0) // 是否更新已经存在的用户数据
  57. /** 打开弹窗 */
  58. const open = () => {
  59. dialogVisible.value = true
  60. updateSupport.value = 0
  61. fileList.value = []
  62. resetForm()
  63. }
  64. defineExpose({ open }) // 提供 open 方法,用于打开弹窗
  65. /** 提交表单 */
  66. const submitForm = async () => {
  67. if (fileList.value.length == 0) {
  68. message.error('请上传文件')
  69. return
  70. }
  71. // 提交请求
  72. uploadHeaders.value = {
  73. Authorization: 'Bearer ' + getAccessToken(),
  74. 'tenant-id': getTenantId()
  75. }
  76. formLoading.value = true
  77. uploadRef.value!.submit()
  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. const submitFormError = (): void => {
  107. message.error('上传失败,请您重新上传!')
  108. formLoading.value = false
  109. }
  110. /** 重置表单 */
  111. const resetForm = async (): Promise<void> => {
  112. // 重置上传状态和文件
  113. formLoading.value = false
  114. await nextTick()
  115. uploadRef.value?.clearFiles()
  116. }
  117. /** 文件数超出提示 */
  118. const handleExceed = (): void => {
  119. message.error('最多只能上传一个文件!')
  120. }
  121. /** 下载模板操作 */
  122. const importTemplate = async () => {
  123. const res = await UserApi.importUserTemplate()
  124. download.excel(res, '用户导入模版.xls')
  125. }
  126. </script>