HcUpload.vue 3.9 KB

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