webview.vue 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <template>
  2. <cwg-page-wrapper class="webview-page" :pageTitle="pageTitle" type="webview">
  3. <view class="page-container">
  4. <!-- WebView 内容区(自动占满剩余空间,不遮挡导航栏) -->
  5. <view class="web-view-container">
  6. <web-view :src="fileUrl" class="web-view" :webview-styles="webviewStyles" />
  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. import getWebBase from '@/utils/webBase'
  16. const webBase = getWebBase()
  17. import { useI18n } from 'vue-i18n'
  18. const { t } = useI18n()
  19. const fileUrl = ref('')
  20. const title = ref('')
  21. const pageTitle = computed(() => t(title.value || ''))
  22. const fileType = ref('')
  23. import useGlobalStore from '@/stores/use-global-store'
  24. const globalStore = useGlobalStore()
  25. const statusBarHeight = computed(() => globalStore.statusBarHeight || 0)
  26. // webview 样式(修复顶部安全区)
  27. const webviewStyles = computed(() => ({
  28. progress: {
  29. color: '#ea002a'
  30. },
  31. top: statusBarHeight.value + 56 + 'px',
  32. bounce: 'none',
  33. scrollIndicator: 'none'
  34. }))
  35. // 获取文件后缀
  36. const getFileExt = (name) => {
  37. if (!name) return ''
  38. return name.split('.').pop()?.toUpperCase()
  39. }
  40. onLoad((options) => {
  41. console.log('webview 接收参数:', options)
  42. title.value = options.title || '文件预览'
  43. const realUrl = decodeURIComponent(options.url || '')
  44. fileType.value = getFileExt(realUrl)
  45. // ✅ 核心修复:PDF 只编码一次,不再二次编码
  46. if (fileType.value === 'PDF') {
  47. fileUrl.value = `${Config.Host80}${webBase}/iframe/pdf.html?pdf=${realUrl}&title=${title.value}`
  48. } else {
  49. fileUrl.value = realUrl
  50. }
  51. console.log('最终加载地址:', fileUrl.value)
  52. })
  53. </script>
  54. <style scoped lang="scss">
  55. @import "@/uni.scss";
  56. </style>