preview.vue 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <template>
  2. <web-view :src="webSrc"/>
  3. </template>
  4. <script setup>
  5. import {ref, onMounted, getCurrentInstance} from "vue";
  6. import {onLoad} from '@dcloudio/uni-app'
  7. //初始变量
  8. let wv;
  9. const instance = getCurrentInstance().proxy
  10. const webSrc = ref('');
  11. onLoad((option) => {
  12. if (option.url) {
  13. webSrc.value = '/hybrid/html/pdf/web/viewer.html?file= ' + decodeURIComponent(option.url);
  14. }
  15. })
  16. //渲染完成
  17. onMounted(() => {
  18. setWebViewStyle()
  19. })
  20. //设置样式
  21. const setWebViewStyle = async () => {
  22. // #ifdef APP-PLUS
  23. await initWebview()
  24. // #endif
  25. }
  26. //初始化webview
  27. const initWebview = async () => {
  28. return new Promise((resolve) => {
  29. let currentWebview = instance.$scope.$getAppWebview()
  30. //如果是页面初始化调用时,需要延时一下
  31. setTimeout(function() {
  32. wv = currentWebview.children()[0]
  33. wv.setStyle({scalable:true})
  34. // #ifdef APP-PLUS
  35. //ios 禁用缓存,测试生效!!
  36. let cache1 = plus.ios.newObject('NSURLCache');
  37. let cache = plus.ios.invoke(cache1, 'sharedURLCache');
  38. plus.ios.invoke(cache, 'removeAllCachedResponses');
  39. plus.ios.invoke(cache, 'setDiskCapacity:', 0);
  40. plus.ios.invoke(cache, 'setMemoryCapacity:', 0);
  41. //安卓端缓存清理。
  42. plus.cache.clear();
  43. // #endif
  44. resolve(true)
  45. }, 1000);
  46. })
  47. }
  48. </script>
  49. <style lang="scss">
  50. page {
  51. height: auto;
  52. }
  53. </style>