webview.vue 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <template>
  2. <cwg-page-wrapper class="webview-page" :pageTitle="pageTitle">
  3. <view class="page-container">
  4. <!-- WebView 内容区(自动占满剩余空间,不遮挡导航栏) -->
  5. <view class="web-view-container">
  6. <web-view :src="fileUrl" class="web-view" />
  7. </view>
  8. </view>
  9. </cwg-page-wrapper>
  10. </template>
  11. <script setup>
  12. import { ref, computed } from 'vue'
  13. import { onLoad } from '@dcloudio/uni-app'
  14. import Config from '@/config/index'
  15. const { Host80 } = Config
  16. import getWebBase from '@/utils/webBase'
  17. const webBase = getWebBase()
  18. import { useI18n } from 'vue-i18n'
  19. const { t } = useI18n()
  20. const fileUrl = ref('')
  21. const title = ref('')
  22. const pageTitle = computed(() => t(title.value))
  23. const fileType = ref('')
  24. // 获取文件后缀
  25. const getFileExt = (name) => {
  26. if (!name) return ''
  27. return name.split('.').pop()?.toUpperCase()
  28. }
  29. onLoad((options) => {
  30. title.value = options.title || '文件预览'
  31. fileType.value = getFileExt(options.url)
  32. // PDF 跳转到你的预览页
  33. if (fileType.value === 'PDF') {
  34. fileUrl.value = `${Host80}${webBase}iframe/pdf.html?pdf=${encodeURIComponent(options.url)}`
  35. } else {
  36. fileUrl.value = options.url || ''
  37. }
  38. })
  39. </script>
  40. <style scoped lang="scss">
  41. @import "@/uni.scss";
  42. .webview-page {
  43. :deep(.content-wrapper) {
  44. padding: 0 !important;
  45. }
  46. :deep(.page-content) {
  47. height: calc(100vh - 112px);
  48. }
  49. :deep(.fixed) {
  50. height: 112px;
  51. }
  52. }
  53. /* 页面根容器:弹性布局,完美分配导航栏和web-view高度 */
  54. .page-container {
  55. display: flex;
  56. flex-direction: column;
  57. width: 100%;
  58. height: 100vh;
  59. }
  60. /* 顶部导航栏:正确适配安全区,高度计算正确 */
  61. .web-view-container {
  62. flex: 1;
  63. width: 100%;
  64. overflow: hidden;
  65. }
  66. .web-view {
  67. width: 100%;
  68. height: 100%;
  69. }
  70. </style>