index.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <template>
  2. <div class="hc-global-upload-file-box-hide" v-if="isBody">
  3. <Teleport to="#app">
  4. <UploadFile ref="uploadFileRef"
  5. :global="customGlobal"
  6. :params="customParams"
  7. :options="customOptions"
  8. @fileAdded="uploadFileAdded"
  9. @fileProgress="uploadFileProgress"
  10. @fileSuccess="uploadFileSuccess"
  11. @filesChange="uploadFileChange"
  12. @fileError="uploadFileError"
  13. />
  14. </Teleport>
  15. </div>
  16. </template>
  17. <script setup>
  18. import {nextTick, onMounted, ref, watch} from 'vue'
  19. import UploadFile from './file.vue'
  20. const props = defineProps({
  21. global: {
  22. type: Boolean,
  23. default: true
  24. },
  25. // 发送给服务器的额外参数
  26. params: {
  27. type: Object,
  28. default: () => ({})
  29. },
  30. options: {
  31. type: Object,
  32. default: () => ({})
  33. }
  34. })
  35. //初始变量
  36. const isBody = ref(false)
  37. const uploadFileRef = ref(null)
  38. const customGlobal = ref(props.global)
  39. const customParams = ref(props.params)
  40. const customOptions = ref(props.options)
  41. //事件
  42. const emit = defineEmits(['fileAdded', 'fileSuccess', 'fileError', 'filesChange', 'fileProgress'])
  43. //监听
  44. watch(() => [
  45. props.params,
  46. props.options,
  47. props.global
  48. ], ([params, options, global]) => {
  49. customGlobal.value = global
  50. if (params) {
  51. customParams.value = params
  52. }
  53. if (options) {
  54. customOptions.value = options
  55. }
  56. })
  57. onMounted(() => {
  58. //页面渲染完成后,再让 vue3 的 Teleport,把弹出框挂载到外部节点上。
  59. nextTick(() => {
  60. isBody.value = true
  61. })
  62. })
  63. //选择了文件
  64. const uploadFileAdded = () => {
  65. emit('fileAdded')
  66. }
  67. // 文件上传进度
  68. const uploadFileProgress = (res) => {
  69. emit('fileProgress', res)
  70. }
  71. // 文件上传成功的回调
  72. const uploadFileSuccess = (res) => {
  73. emit('fileSuccess', res)
  74. }
  75. //文件上传失败
  76. const uploadFileError = () => {
  77. emit('fileError')
  78. }
  79. // 文件全部上传完成
  80. const uploadFileChange = () => {
  81. emit('filesChange')
  82. }
  83. //关闭
  84. const close = () => {
  85. remove()
  86. isShow(false)
  87. }
  88. //清除
  89. const remove = () => {
  90. uploadFileRef.value?.remove()
  91. }
  92. const cancel = () => {
  93. uploadFileRef.value?.cancel()
  94. }
  95. //是否显示
  96. const isShow = (res) => {
  97. uploadFileRef.value?.isShow(res)
  98. }
  99. //点击上传按钮
  100. const btnUpload = () => {
  101. uploadFileRef.value?.btnUpload()
  102. }
  103. // 暴露出去
  104. defineExpose({
  105. close,
  106. remove,
  107. cancel,
  108. isShow,
  109. btnUpload
  110. })
  111. </script>
  112. <style lang="scss">
  113. .hc-global-upload-file-box-hide {
  114. display: none;
  115. }
  116. </style>