HcDragUpload.vue 4.3 KB

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