UserImportForm.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <template>
  2. <Dialog
  3. :title="upload.title"
  4. :modelValue="showDialog"
  5. width="400px"
  6. append-to-body
  7. @close="closeDialog"
  8. >
  9. <el-upload
  10. ref="uploadRef"
  11. accept=".xlsx, .xls"
  12. :limit="1"
  13. :headers="upload.headers"
  14. :action="upload.url + '?updateSupport=' + upload.updateSupport"
  15. :disabled="upload.isUploading"
  16. :on-progress="handleFileUploadProgress"
  17. :on-success="handleFileSuccess"
  18. :on-exceed="handleExceed"
  19. :on-error="excelUploadError"
  20. :auto-upload="false"
  21. drag
  22. >
  23. <Icon icon="ep:upload" />
  24. <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
  25. <template #tip>
  26. <div class="el-upload__tip text-center">
  27. <div class="el-upload__tip">
  28. <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据
  29. </div>
  30. <span>仅允许导入xls、xlsx格式文件。</span>
  31. <el-link
  32. type="primary"
  33. :underline="false"
  34. style="font-size: 12px; vertical-align: baseline"
  35. @click="importTemplate"
  36. >下载模板</el-link
  37. >
  38. </div>
  39. </template>
  40. </el-upload>
  41. <template #footer>
  42. <div class="dialog-footer">
  43. <el-button type="primary" @click="submitFileForm">确 定</el-button>
  44. <el-button @click="cancel">取 消</el-button>
  45. </div>
  46. </template>
  47. </Dialog>
  48. </template>
  49. <script lang="ts" setup>
  50. import { importUserTemplateApi } from '@/api/system/user'
  51. import { getAccessToken, getTenantId } from '@/utils/auth'
  52. import download from '@/utils/download'
  53. const emits = defineEmits(['success'])
  54. const message = useMessage() // 消息弹窗
  55. const showDialog = ref(false)
  56. const uploadRef = ref()
  57. // 用户导入参数
  58. const upload = reactive({
  59. // // 是否显示弹出层(用户导入)
  60. // open: false,
  61. // 弹出层标题(用户导入)
  62. title: '用户导入',
  63. // 是否禁用上传
  64. isUploading: false,
  65. // 是否更新已经存在的用户数据
  66. updateSupport: 0,
  67. // 设置上传的请求头部
  68. headers: {
  69. Authorization: 'Bearer ' + getAccessToken(),
  70. 'tenant-id': getTenantId()
  71. },
  72. // 上传的地址
  73. url: import.meta.env.VITE_BASE_URL + import.meta.env.VITE_API_URL + '/system/user/import'
  74. })
  75. // 文件上传中处理
  76. const handleFileUploadProgress = () => {
  77. upload.isUploading = true
  78. }
  79. // 文件上传成功处理
  80. const handleFileSuccess = (response: any) => {
  81. if (response.code !== 0) {
  82. message.error(response.msg)
  83. return
  84. }
  85. upload.isUploading = false
  86. uploadRef.value?.clearFiles()
  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. emits('success')
  103. closeDialog()
  104. }
  105. // 文件数超出提示
  106. const handleExceed = (): void => {
  107. message.error('最多只能上传一个文件!')
  108. }
  109. // 上传错误提示
  110. const excelUploadError = (e): void => {
  111. console.log(e)
  112. message.error('导入数据失败,请您重新上传!')
  113. }
  114. /** 下载模板操作 */
  115. const importTemplate = async () => {
  116. try {
  117. const res = await importUserTemplateApi()
  118. download.excel(res, '用户导入模版.xls')
  119. } catch (error) {
  120. console.error(error)
  121. }
  122. }
  123. /* 弹框按钮操作 */
  124. // 点击取消
  125. const cancel = () => {
  126. closeDialog()
  127. }
  128. // 关闭弹窗
  129. const closeDialog = () => {
  130. showDialog.value = false
  131. }
  132. // 提交上传文件
  133. const submitFileForm = () => {
  134. uploadRef.value?.submit()
  135. }
  136. const openForm = () => {
  137. uploadRef.value?.clearFiles()
  138. showDialog.value = true
  139. }
  140. defineExpose({
  141. openForm
  142. })
  143. </script>