index.vue 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <template>
  2. <web-view :update-title="title" :webview-styles="webStyle" :style="webStyle" :src="pdfSrc" v-if="pdfSrc"/>
  3. </template>
  4. <script setup>
  5. import {ref, watch, onMounted} from "vue";
  6. //初始变量
  7. const props = defineProps({
  8. ui: {
  9. type: Object,
  10. default: () => ({})
  11. },
  12. title: {
  13. type: Boolean,
  14. default: true,
  15. },
  16. src: String,
  17. });
  18. const windowInfo = uni.getWindowInfo()
  19. const pdfSrc = ref('')
  20. const webStyle = ref({})
  21. //渲染完成
  22. onMounted(() => {
  23. setPdfSrc(props.src)
  24. setWebStyle(props.ui)
  25. // #ifdef APP-PLUS
  26. //ios 禁用缓存,测试生效!!
  27. let cache1 = plus.ios.newObject('NSURLCache');
  28. let cache = plus.ios.invoke(cache1, 'sharedURLCache');
  29. plus.ios.invoke(cache, 'removeAllCachedResponses');
  30. plus.ios.invoke(cache, 'setDiskCapacity:', 0);
  31. plus.ios.invoke(cache, 'setMemoryCapacity:', 0);
  32. //安卓端缓存清理。
  33. plus.cache.clear();
  34. // #endif
  35. })
  36. //监听变化
  37. watch(() => [
  38. props.src
  39. ], ([src]) => {
  40. setPdfSrc(src)
  41. })
  42. //监听样式变化
  43. watch(() => [
  44. props.ui
  45. ], ([ui]) => {
  46. setWebStyle(ui)
  47. }, {deep: true})
  48. //设置pdf的url
  49. const setPdfSrc = (src) => {
  50. const url = '/hybrid/html/pdf/index.html?src='
  51. if (src) {
  52. pdfSrc.value = url + src
  53. } else {
  54. pdfSrc.value = ''
  55. }
  56. }
  57. //设置webview的样式
  58. const setWebStyle = (data) => {
  59. const {windowHeight} = windowInfo
  60. webStyle.value = {
  61. ...data,
  62. height: data.height ?? windowHeight + 'px'
  63. }
  64. }
  65. </script>