Excel.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <!-- excel 导入组件 -->
  2. <template>
  3. <el-dialog :title="prop.title" v-model="state.upload.open" :close-on-click-modal="false" draggable>
  4. <el-upload
  5. ref="uploadRef"
  6. :limit="1"
  7. accept=".xlsx, .xls"
  8. :headers="headers"
  9. :action="baseURL + other.adaptationUrl(url)"
  10. :disabled="state.upload.isUploading"
  11. :on-progress="handleFileUploadProgress"
  12. :on-success="handleFileSuccess"
  13. :on-error="handleFileError"
  14. :auto-upload="false"
  15. drag
  16. >
  17. <i class="el-icon-upload"></i>
  18. <div class="el-upload__text">
  19. {{ $t('excel.operationNotice') }}
  20. <em>{{ $t('excel.clickUpload') }}</em>
  21. </div>
  22. <template #tip>
  23. <div class="el-upload__tip text-center">
  24. <span>{{ $t('excel.fileFormat') }}</span>
  25. <el-link type="primary" :underline="false" style="font-size: 12px; vertical-align: baseline" @click="downExcelTemp" v-if="tempUrl"
  26. >{{ $t('excel.downloadTemplate') }}
  27. </el-link>
  28. </div>
  29. </template>
  30. </el-upload>
  31. <template #footer>
  32. <el-button type="primary" @click="submitFileForm">{{ $t('common.confirmButtonText') }}</el-button>
  33. <el-button @click="state.upload.open = false">{{ $t('common.cancelButtonText') }}</el-button>
  34. </template>
  35. </el-dialog>
  36. <!--校验失败错误数据-->
  37. <el-dialog :title="$t('excel.validationFailureData')" v-model="state.errorVisible">
  38. <el-table :data="state.errorData">
  39. <el-table-column property="lineNum" :label="$t('excel.lineNumbers')" width="100"></el-table-column>
  40. <el-table-column property="errors" :label="$t('excel.misDescription')" show-overflow-tooltip>
  41. <template v-slot="scope">
  42. <el-tag type="danger" v-for="error in scope.row.errors" :key="error">{{ error }}</el-tag>
  43. </template>
  44. </el-table-column>
  45. </el-table>
  46. </el-dialog>
  47. </template>
  48. <script setup lang="ts" name="upload-excel">
  49. import { useMessage } from '/@/hooks/message';
  50. import other from '/@/utils/other';
  51. import { Session } from '/@/utils/storage';
  52. const emit = defineEmits(['sizeChange', 'refreshDataList']);
  53. const prop = defineProps({
  54. url: {
  55. type: String,
  56. },
  57. title: {
  58. type: String,
  59. },
  60. tempUrl: {
  61. type: String,
  62. },
  63. });
  64. const uploadRef = ref();
  65. const state = reactive({
  66. errorVisible: false,
  67. errorData: [],
  68. dialog: {
  69. title: '',
  70. isShowDialog: false,
  71. },
  72. upload: {
  73. open: false,
  74. isUploading: false,
  75. },
  76. });
  77. /**
  78. * 下载模板文件
  79. */
  80. const downExcelTemp = () => {
  81. other.downBlobFile(other.adaptationUrl(prop.tempUrl), {}, 'temp.xlsx');
  82. };
  83. /**
  84. * 上传进度条变化事件
  85. */
  86. const handleFileUploadProgress = () => {
  87. state.upload.isUploading = true;
  88. };
  89. /**
  90. * 上传失败事件处理
  91. */
  92. const handleFileError = () => {
  93. useMessage().error('上传失败,数据格式不合法!');
  94. state.upload.open = false;
  95. };
  96. /**
  97. * 上传成功事件处理
  98. * @param {any} response - 上传成功的响应结果
  99. */
  100. const handleFileSuccess = (response: any) => {
  101. state.upload.isUploading = false;
  102. state.upload.open = false;
  103. uploadRef.value.clearFiles();
  104. // 校验失败
  105. if (response.code === 1) {
  106. useMessage().error('导入失败,以下数据不合法');
  107. state.errorVisible = true;
  108. state.errorData = response.data;
  109. uploadRef.value.clearFiles();
  110. // 刷新表格
  111. emit?.('refreshDataList');
  112. } else {
  113. useMessage().success(response.msg ? response.msg : '导入成功');
  114. // 刷新表格
  115. emit?.('refreshDataList');
  116. }
  117. };
  118. /**
  119. * 提交表单,触发上传
  120. */
  121. const submitFileForm = () => {
  122. uploadRef.value.submit();
  123. };
  124. /**
  125. * 显示上传文件对话框,并清除上传信息
  126. */
  127. const show = () => {
  128. state.upload.isUploading = false;
  129. state.upload.open = true;
  130. };
  131. /**
  132. * 计算请求头部信息
  133. */
  134. const headers = computed(() => {
  135. return {
  136. Authorization: 'Bearer ' + Session.getToken(),
  137. 'TENANT-ID': Session.getTenant(),
  138. };
  139. });
  140. // 暴露变量
  141. defineExpose({
  142. show,
  143. });
  144. </script>
  145. <style scoped></style>