HcDragUpload.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <template>
  2. <el-upload ref="uploadRef" class="hc-upload-border approach" drag :action="action" :headers="getTokenHeader()" :data="uploadData" :accept="accept" :disabled="uploadDisabled" :limit="1" :show-file-list="false"
  3. :before-upload="beforeUpload" :on-exceed="uploadExceed" :on-progress="uploadprogress" :on-success="uploadSuccess" :on-error="uploadError" :on-change="uploadChange" :auto-upload="false">
  4. <div class="hc-upload-loading upload-file-info" v-loading="uploadDisabled" element-loading-text="上传中...">
  5. <template v-if="uploadFileInfo?.name">
  6. <HcIcon name="file-text" class="upload-file-icon"/>
  7. <div class="upload-file-name">{{uploadFileInfo?.name}}</div>
  8. </template>
  9. <template v-else>
  10. <HcIcon name="upload-cloud" class="upload-icon"/>
  11. <div class="el-upload__text">拖动文件到这里 或 <em>点击这里选择文件</em> 并上传</div>
  12. </template>
  13. </div>
  14. <template #tip>
  15. <div class="el-upload__tip">允许格式:excel, 文件大小 小于 60MB</div>
  16. </template>
  17. </el-upload>
  18. </template>
  19. <script setup>
  20. import {ref,watch,onMounted} from "vue";
  21. import {getTokenHeader} from '~src/api/request/header';
  22. import {getObjValue, isSize} from "vue-utils-plus"
  23. import {genFileId} from "element-plus";
  24. const props = defineProps({
  25. datas: {
  26. type: Object,
  27. default: () => ({})
  28. },
  29. })
  30. //变量
  31. const uploadRef = ref(null)
  32. const uploadData = ref(props.datas)
  33. const uploadFileInfo = ref({})
  34. const uploadDisabled = ref(false)
  35. const action = '/api/blade-manager/exceltab/add-buss-file';
  36. const accept = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel';
  37. //监听
  38. watch(() => [
  39. props.datas,
  40. ], ([datas]) => {
  41. uploadData.value = datas
  42. })
  43. //事件
  44. const emit = defineEmits(['progress', 'finished', 'change'])
  45. //上传前
  46. const beforeUpload = async (file) => {
  47. if (isSize(file?.size,60)) {
  48. return true;
  49. } else {
  50. window?.$message?.warning('文件大小, 不能过60M!');
  51. return false;
  52. }
  53. }
  54. //超出限制时
  55. const uploadExceed = (files) => {
  56. uploadRef.value?.clearFiles()
  57. const file = files[0]
  58. file.uid = genFileId()
  59. uploadRef.value?.handleStart(file)
  60. }
  61. //上传中
  62. const uploadprogress = () => {
  63. uploadDisabled.value = true
  64. emit('progress', true)
  65. }
  66. //上传完成
  67. const uploadSuccess = (res) => {
  68. uploadDisabled.value = false
  69. emit('progress', false)
  70. emit('finished', {
  71. type: 'success',
  72. data: getObjValue(res?.data)
  73. })
  74. }
  75. //上传失败
  76. const uploadError = (res) => {
  77. uploadDisabled.value = false
  78. emit('progress', false)
  79. emit('finished', {
  80. type: 'error',
  81. data: getObjValue(res?.data)
  82. })
  83. window?.$message?.error('导入失败');
  84. }
  85. //文件改变时
  86. const uploadChange = (file) => {
  87. console.log(file)
  88. uploadFileInfo.value = file
  89. emit('change', file)
  90. }
  91. const submit = () => {
  92. uploadRef.value?.submit()
  93. }
  94. const clearFiles = () => {
  95. uploadRef.value?.clearFiles()
  96. }
  97. // 暴露出去
  98. defineExpose({
  99. submit,
  100. clearFiles
  101. })
  102. </script>
  103. <style lang="scss">
  104. .hc-upload-border.approach {
  105. .el-upload-dragger {
  106. padding: 24px;
  107. }
  108. .hc-upload-loading.upload-file-info {
  109. .hc-icon-i {
  110. font-size: 40px;
  111. }
  112. .upload-icon {
  113. color: var(--el-text-color-placeholder);
  114. }
  115. .upload-file-icon {
  116. color: var(--el-color-primary-light-5);
  117. }
  118. .el-upload__text {
  119. margin-top: 10px;
  120. }
  121. .upload-file-name {
  122. margin-top: 10px;
  123. font-size: 14px;
  124. text-align: center;
  125. color: var(--el-color-primary);
  126. }
  127. }
  128. .el-upload__tip {
  129. font-size: 14px;
  130. margin-top: 16px;
  131. color: var(--el-text-color-placeholder);
  132. }
  133. }
  134. </style>