HcUpload.vue 4.2 KB

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