HcDragUpload.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <template>
  2. <el-upload class="hc-upload-border" drag :action="action" :headers="getTokenHeader()" :data="uploadData" :accept="accept" :file-list="fileListData" :disabled="uploadDisabled"
  3. :on-preview="uploadPreview" :on-success="uploadSuccess" :on-exceed="uploadExceed" :on-error="uploadError" :before-upload="beforeUpload" :on-change="onChangeToolFile"
  4. :on-progress="uploadprogress" ref="upload">
  5. <div class="hc-upload-loading" v-loading="uploadDisabled" :element-loading-text="loadingText">
  6. <HcIcon name="backup" ui="text-5xl mt-4"/>
  7. <div class="el-upload__text">拖动文件到这里 或 <em>点击这里选择文件</em> 并上传</div>
  8. </div>
  9. <template #tip>
  10. <div class="el-upload__tip" style="font-size: 14px;">允许格式:jpg/png/pdf/word, 文件大小 小于 60MB</div>
  11. </template>
  12. </el-upload>
  13. </template>
  14. <script setup>
  15. import {ref,watch,onMounted} from "vue";
  16. import {getTokenHeader} from '~src/api/request/header';
  17. import wbsApi from "~api/data-fill/wbs"
  18. import {isSize} from "vue-utils-plus"
  19. import { genFileId } from 'element-plus'
  20. const props = defineProps({
  21. fileList: {
  22. type: Array,
  23. default: () => ([])
  24. },
  25. datas: {
  26. type: Object,
  27. default: () => ({})
  28. },
  29. uploadData:{
  30. type: Array,
  31. default: () => ([])
  32. }
  33. })
  34. //变量
  35. const uploadData = ref(props.datas)
  36. const fileListData = ref(props.fileList);
  37. const action = '/api/blade-resource/oss/endpoint/put-file-attach';
  38. const accept = 'image/png,image/jpg,image/jpeg,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel,application/pdf,.doc,.docx,application/msword';
  39. const uploadDisabled = ref(false)
  40. //监听
  41. watch(() => [
  42. props.fileList,
  43. props.datas,
  44. ], ([fileList, datas]) => {
  45. uploadData.value = datas
  46. fileListData.value = fileList
  47. })
  48. //渲染完成
  49. onMounted(()=> {
  50. beforeFileNum.value = 0
  51. finishFileNum.value = 0
  52. errorFileNum.value = 0
  53. })
  54. //事件
  55. const emit = defineEmits(['change','progress','finished'])
  56. //上传前
  57. const beforeFileNum = ref(0)
  58. const beforeUpload = async (file) => {
  59. if (isSize(file?.size,60)) {
  60. beforeFileNum.value ++;
  61. return true;
  62. } else {
  63. window?.$message?.warning('文件大小, 不能过60M!');
  64. return false;
  65. }
  66. }
  67. const upload=ref('')
  68. //超出限制时
  69. const uploadExceed = (files) => {
  70. }
  71. //文件改变
  72. const onChangeToolFile = (files,fileList) => {
  73. fileListData.value = [files]
  74. fileList.splice(0, 1)
  75. }
  76. //上传中
  77. const loadingText = ref('上传中...')
  78. const uploadprogress = () => {
  79. loadingText.value = '上传中...'
  80. uploadDisabled.value = true
  81. }
  82. //上传完成
  83. const finishFileNum = ref(0)
  84. // const uploadSuccess = () => {
  85. // finishFileNum.value ++;
  86. // if (beforeFileNum.value === finishFileNum.value) {
  87. // uploadDisabled.value = false
  88. // emit('change', {type: 'success'})
  89. // }
  90. // }
  91. //上传完成
  92. const uploadSuccess = ({code, data}) => {
  93. uploadDisabled.value = false
  94. emit('progress', false)
  95. if (code === 200) {
  96. window?.$message?.success('上传成功');
  97. emit('finished', data)
  98. } else {
  99. window?.$message?.error('上传失败');
  100. }
  101. }
  102. //上传失败
  103. const errorFileNum = ref(0)
  104. const uploadError = () => {
  105. errorFileNum.value ++;
  106. window?.$message?.error('上传失败');
  107. const num = finishFileNum.value + errorFileNum.value;
  108. if (beforeFileNum.value === num) {
  109. uploadDisabled.value = false
  110. emit('change', {type: 'success'})
  111. }
  112. }
  113. //预览
  114. const uploadPreview = ({url}) => {
  115. if (url) {
  116. window.open(url, '_blank')
  117. }
  118. }
  119. //删除文件
  120. const delUploadData = async ({id}) => {
  121. loadingText.value = '删除中...'
  122. uploadDisabled.value = true
  123. const {error, code} = await wbsApi.removeBussFile({
  124. ids: id
  125. })
  126. uploadDisabled.value = false
  127. if (!error && code === 200) {
  128. window?.$message?.success('删除成功')
  129. return true
  130. } else {
  131. return false
  132. }
  133. }
  134. const uploadRemove = () => {
  135. if(fileListData.value.length <= 0) {
  136. emit('change', {type: 'del'})
  137. }
  138. }
  139. </script>