DeptImportForm.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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"
  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. <span>仅允许导入 xls、xlsx 格式文件。</span>
  22. <el-link
  23. :underline="false"
  24. style="font-size: 12px; vertical-align: baseline"
  25. type="primary"
  26. @click="importTemplate"
  27. >
  28. 下载模板
  29. </el-link>
  30. </div>
  31. </template>
  32. </el-upload>
  33. <template #footer>
  34. <el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
  35. <el-button @click="dialogVisible = false">取 消</el-button>
  36. </template>
  37. </Dialog>
  38. </template>
  39. <script lang="ts" setup>
  40. import * as DeptApi from '@/api/system/dept'
  41. import { getAccessToken, getTenantId } from '@/utils/auth'
  42. import download from '@/utils/download'
  43. defineOptions({ name: 'SystemDeptImportForm' })
  44. const message = useMessage()
  45. const dialogVisible = ref(false)
  46. const formLoading = ref(false)
  47. const uploadRef = ref()
  48. const importUrl = import.meta.env.VITE_BASE_URL + import.meta.env.VITE_API_URL + '/system/dept/import'
  49. const uploadHeaders = ref()
  50. const fileList = ref([])
  51. /** 打开弹窗 */
  52. const open = () => {
  53. dialogVisible.value = true
  54. fileList.value = []
  55. resetForm()
  56. }
  57. defineExpose({ open })
  58. /** 提交表单 */
  59. const submitForm = async () => {
  60. if (fileList.value.length == 0) {
  61. message.error('请上传文件')
  62. return
  63. }
  64. uploadHeaders.value = {
  65. Authorization: 'Bearer ' + getAccessToken(),
  66. 'tenant-id': getTenantId()
  67. }
  68. formLoading.value = true
  69. uploadRef.value!.submit()
  70. }
  71. /** 文件上传成功 */
  72. const emits = defineEmits(['success'])
  73. const submitFormSuccess = (response: any) => {
  74. if (response.code !== 0) {
  75. message.error(response.msg)
  76. formLoading.value = false
  77. return
  78. }
  79. const data = response.data
  80. let text = '创建成功数量:' + data.createDeptNames.length + ';'
  81. for (const name of data.createDeptNames) {
  82. text += '< ' + name + ' >'
  83. }
  84. text += '导入失败数量:' + Object.keys(data.failureDeptNames).length + ';'
  85. for (const name in data.failureDeptNames) {
  86. text += '< ' + name + ': ' + data.failureDeptNames[name] + ' >'
  87. }
  88. message.alert(text)
  89. formLoading.value = false
  90. dialogVisible.value = false
  91. emits('success')
  92. }
  93. /** 上传错误提示 */
  94. const submitFormError = (): void => {
  95. message.error('上传失败,请您重新上传!')
  96. formLoading.value = false
  97. }
  98. /** 重置表单 */
  99. const resetForm = async (): Promise<void> => {
  100. formLoading.value = false
  101. await nextTick()
  102. uploadRef.value?.clearFiles()
  103. }
  104. /** 文件数超出提示 */
  105. const handleExceed = (): void => {
  106. message.error('最多只能上传一个文件!')
  107. }
  108. /** 下载模板操作 */
  109. const importTemplate = async () => {
  110. const res = await DeptApi.importDeptTemplate()
  111. download.excel(res, '部门导入模版.xls')
  112. }
  113. </script>