popupPreviewFile.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <template>
  2. <uni-popup ref="popupDialog" :animation="false" :is-mask-click="isMaskClick">
  3. <uni-icons @click="CloseDialog" class="colse" type="closeempty" size="24"></uni-icons>
  4. <!-- 图片 -->
  5. <image v-if="fileType=='image'" class="img_width" @click="CloseDialog" :src="filePath" mode="aspectFit"></image>
  6. <video v-else-if="fileType=='video'" ref="fileVideo" id="fileVideo" :src="filePath" style="width: 100%; object-fit: contain;"></video>
  7. <audio v-else-if="fileType=='audio'" :src="filePath" style="width: 100%;"></audio>
  8. <template v-else>
  9. <view class="other_file"><text>不支持预览,请点击下载: </text><text style="color: #2e64fa;font-size: 28rpx;" @click="ToDownloadFile">{{fileName}}</text></view>
  10. </template>
  11. </uni-popup>
  12. </template>
  13. <script setup>
  14. import uniPopup from '@/uni_modules/uni-popup/components/uni-popup/uni-popup.vue';
  15. import { getFileTypeBySuffix } from '@/common/common.js';
  16. import {computed, reactive,ref,nextTick,getCurrentInstance} from 'vue';
  17. import {onLoad} from '@dcloudio/uni-app';
  18. const props = defineProps({
  19. isMaskClick:{//蒙版点击是否关闭弹窗
  20. type: Boolean,
  21. default: true
  22. },
  23. fileType:{//文件后缀
  24. type: String,
  25. default: ''
  26. },
  27. filePath:{//文件路径
  28. type: String,
  29. default: ''
  30. },
  31. fileName:{//文件名称
  32. type: String,
  33. default: ''
  34. },
  35. })
  36. const videoCtx = ref(null);//播放器
  37. const emit = defineEmits(['ToDownloadFile']);//下载
  38. const state = reactive({});
  39. const popupDialog = ref(null);
  40. onLoad(() => {
  41. console.log(props.fileType,999)
  42. if(props.fileType=='video'){
  43. const instance = getCurrentInstance();
  44. videoCtx.value = uni.createVideoContext('fileVideo', instance.proxy);
  45. }
  46. })
  47. //打开弹窗
  48. const OpenDialog = () => {
  49. popupDialog.value?.open();
  50. }
  51. //下载
  52. const ToDownloadFile = () => {
  53. emit('ToDownloadFile',{
  54. fileType:props.fileType,
  55. filePath:props.filePath,
  56. fileName:props.fileName
  57. })
  58. popupDialog.value?.close();
  59. };
  60. //关闭弹框
  61. const CloseDialog = () => {
  62. videoCtx?.value?.pause();//暂停播放
  63. popupDialog.value?.close();
  64. }
  65. defineExpose({
  66. OpenDialog
  67. })
  68. </script>
  69. <style scoped lang="scss">
  70. :deep(.maskClass){
  71. background-color: rgba(0, 0, 0, 0.8) !important;
  72. }
  73. :deep(.uni-popup__wrapper){
  74. width: 100%;
  75. }
  76. .img_width{
  77. width: 100%;
  78. height: 80vh;
  79. }
  80. .other_file{
  81. margin: 0 auto;
  82. background: #ffffff;
  83. min-height: 160rpx;
  84. height: auto;
  85. display: flex;
  86. width: 670rpx;
  87. box-sizing: border-box;
  88. padding: 20rpx;
  89. border-radius: 20rpx;
  90. flex-wrap: wrap;
  91. align-items: center;
  92. justify-content: center;
  93. text-align: center;
  94. font-size: 28rpx;
  95. color: #333;
  96. word-break: break-all;
  97. }
  98. .colse{
  99. position: fixed;
  100. box-sizing: border-box;
  101. top: 0px;
  102. right: 0px;
  103. width: 60px;
  104. height: 44px;
  105. padding: 6px;
  106. line-height: 32px;
  107. font-size: 26px;
  108. color: #fff !important;
  109. text-align: center;
  110. z-index:99;
  111. }
  112. </style>