HcUpload.vue 3.5 KB

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