popupPreviewFile.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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="fileTypeBySuffix=='image'" class="img_width" @click="CloseDialog" :src="filePath" mode="aspectFit"></image>
  6. <video v-else-if="fileTypeBySuffix=='video'" id="fileVideo" :src="filePath" @error="videoErrorCallback"></video>
  7. <audio v-else-if="fileTypeBySuffix=='audio'" :src="filePath"></audio>
  8. <template v-else-if="['word','excel','pptx','pdf','txt'].includes(fileTypeBySuffix)">
  9. <template v-if="uniPlatform=='web'">
  10. <!-- 默认指H5端 此插件只支持h5 PDF、Word、Excel、PPTX、TXT预览-->
  11. <wg-h5-preview-file ref="webPreviewFile" :value="filePath" :name="fileName" :type="fileTypeBySuffix" @CloseDialog="CloseDialog" />
  12. </template>
  13. <template v-else-if="uniPlatform=='app'">
  14. <!-- App端 -->
  15. </template>
  16. <template v-else-if="uniPlatform=='mp-weixin'">
  17. <!-- 微信小程序端 -->
  18. </template>
  19. <template v-else>
  20. <view class="other_file"><text>不支持预览,请点击下载: </text><text style="color: #2e64fa;font-size: 28rpx;" @click="ToDownloadFile">{{fileName}}</text></view>
  21. </template>
  22. </template>
  23. <template v-else>
  24. <view class="other_file"><text>不支持预览,请点击下载: </text><text style="color: #2e64fa;font-size: 28rpx;" @click="ToDownloadFile">{{fileName}}</text></view>
  25. </template>
  26. </uni-popup>
  27. </template>
  28. <script setup>
  29. import uniPopup from '@/uni_modules/uni-popup/components/uni-popup/uni-popup.vue';
  30. import wgH5PreviewFile from '@/components/wg-h5-preview-file/wg-h5-preview-file.vue';
  31. import { getFileTypeBySuffix } from '@/common/common.js';
  32. import {computed, reactive,ref,nextTick} from 'vue';
  33. const props = defineProps({
  34. isMaskClick:{//蒙版点击是否关闭弹窗
  35. type: Boolean,
  36. default: true
  37. },
  38. fileType:{//文件后缀
  39. type: String,
  40. default: ''
  41. },
  42. filePath:{//文件路径
  43. type: String,
  44. default: ''
  45. },
  46. fileName:{//文件名称
  47. type: String,
  48. default: ''
  49. },
  50. })
  51. const emit = defineEmits(['ToDownloadFile']);//下载
  52. const fileTypeBySuffix = computed(()=>{//获取文件类型
  53. return getFileTypeBySuffix(props.fileType)
  54. });
  55. const uniPlatform = uni.getSystemInfoSync().uniPlatform;// 运行平台
  56. const state = reactive({});
  57. const popupDialog = ref(null);
  58. const webPreviewFile = ref(null);//web端文件预览
  59. //打开弹窗
  60. const OpenDialog = () => {
  61. popupDialog.value?.open();
  62. if(['word','excel','pptx','pdf','txt'].includes(fileTypeBySuffix.value)){
  63. if(uniPlatform=='web'){
  64. nextTick(()=>{
  65. webPreviewFile.value && webPreviewFile.value.preview({
  66. url: props.filePath,
  67. type: fileTypeBySuffix.value
  68. });
  69. })
  70. }
  71. }
  72. }
  73. //下载
  74. const ToDownloadFile = () => {
  75. emit('ToDownloadFile',{
  76. fileType:props.fileType,
  77. filePath:props.filePath,
  78. fileName:props.fileName
  79. })
  80. };
  81. //关闭弹框
  82. const CloseDialog = () => {
  83. console.log(121212)
  84. popupDialog.value?.close();
  85. }
  86. defineExpose({
  87. OpenDialog
  88. })
  89. </script>
  90. <style scoped lang="scss">
  91. :deep(.maskClass){
  92. background-color: rgba(0, 0, 0, 0.8) !important;
  93. }
  94. .img_width{
  95. width: 750rpx;
  96. height: 80vh;
  97. }
  98. .other_file{
  99. margin: 0 auto;
  100. background: #ffffff;
  101. min-height: 160rpx;
  102. height: auto;
  103. display: flex;
  104. width: 670rpx;
  105. box-sizing: border-box;
  106. padding: 20rpx;
  107. border-radius: 20rpx;
  108. flex-wrap: wrap;
  109. align-items: center;
  110. justify-content: center;
  111. text-align: center;
  112. font-size: 28rpx;
  113. color: #333;
  114. }
  115. .colse{
  116. position: fixed;
  117. box-sizing: border-box;
  118. top: 0px;
  119. right: 0px;
  120. width: 60px;
  121. height: 44px;
  122. padding: 6px;
  123. line-height: 32px;
  124. font-size: 26px;
  125. color: #fff !important;
  126. text-align: center;
  127. z-index:99;
  128. }
  129. </style>