ImportForm.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <template>
  2. <el-dialog
  3. :title="upload.title"
  4. :modelValue="modelValue"
  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. </el-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. interface Props {
  54. modelValue: boolean
  55. }
  56. // const props =
  57. withDefaults(defineProps<Props>(), {
  58. modelValue: false
  59. })
  60. const emits = defineEmits(['update:modelValue', 'success'])
  61. const message = useMessage() // 消息弹窗
  62. const uploadRef = ref()
  63. // 用户导入参数
  64. const upload = reactive({
  65. // // 是否显示弹出层(用户导入)
  66. // open: false,
  67. // 弹出层标题(用户导入)
  68. title: '用户导入',
  69. // 是否禁用上传
  70. isUploading: false,
  71. // 是否更新已经存在的用户数据
  72. updateSupport: 0,
  73. // 设置上传的请求头部
  74. headers: {
  75. Authorization: 'Bearer ' + getAccessToken(),
  76. 'tenant-id': getTenantId()
  77. },
  78. // 上传的地址
  79. url: import.meta.env.VITE_BASE_URL + import.meta.env.VITE_API_URL + '/system/user/import'
  80. })
  81. // 文件上传中处理
  82. const handleFileUploadProgress = () => {
  83. upload.isUploading = true
  84. }
  85. // 文件上传成功处理
  86. const handleFileSuccess = (response: any) => {
  87. if (response.code !== 0) {
  88. message.error(response.msg)
  89. return
  90. }
  91. upload.isUploading = false
  92. uploadRef.value?.clearFiles()
  93. // 拼接提示语
  94. const data = response.data
  95. let text = '上传成功数量:' + data.createUsernames.length + ';'
  96. for (let username of data.createUsernames) {
  97. text += '< ' + username + ' >'
  98. }
  99. text += '更新成功数量:' + data.updateUsernames.length + ';'
  100. for (const username of data.updateUsernames) {
  101. text += '< ' + username + ' >'
  102. }
  103. text += '更新失败数量:' + Object.keys(data.failureUsernames).length + ';'
  104. for (const username in data.failureUsernames) {
  105. text += '< ' + username + ': ' + data.failureUsernames[username] + ' >'
  106. }
  107. message.alert(text)
  108. emits('success')
  109. closeDialog()
  110. }
  111. // 文件数超出提示
  112. const handleExceed = (): void => {
  113. message.error('最多只能上传一个文件!')
  114. }
  115. // 上传错误提示
  116. const excelUploadError = (): void => {
  117. message.error('导入数据失败,请您重新上传!')
  118. }
  119. /** 下载模板操作 */
  120. const importTemplate = async () => {
  121. try {
  122. const res = await importUserTemplateApi()
  123. download.excel(res, '用户导入模版.xls')
  124. } catch (error) {
  125. console.error(error)
  126. }
  127. }
  128. /* 弹框按钮操作 */
  129. // 点击取消
  130. const cancel = () => {
  131. closeDialog()
  132. }
  133. // 关闭弹窗
  134. const closeDialog = () => {
  135. emits('update:modelValue', false)
  136. }
  137. // 提交上传文件
  138. const submitFileForm = () => {
  139. uploadRef.value?.submit()
  140. }
  141. </script>