office.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <template>
  2. <div class="wrap">
  3. <web-view :src="filePath" :update-title="false"></web-view>
  4. </div>
  5. </template>
  6. <script>
  7. import {
  8. Base64
  9. } from 'js-base64'
  10. var wv
  11. var currentWebview
  12. //引入相关样式
  13. export default {
  14. data() {
  15. return {
  16. filePath: "", // 预览地址
  17. path: "", // 文件类型
  18. list1: [],
  19. list2: []
  20. };
  21. },
  22. onLoad(options) {
  23. console.log('options', options.url)
  24. // uni.showLoading({
  25. // title: "加载中..."
  26. // })
  27. // currentWebview = this.$scope.$getAppWebview() //获取当前页面的webview对象
  28. // currentWebview.onLoad = this.loadSuccess()
  29. this.filePath =
  30. `http://61.240.156.161:8201/onlinePreview?url=${encodeURIComponent(Base64.encode(options.url))}&officePreviewType=pdf`
  31. this.path = options.url
  32. uni.hideLoading();
  33. },
  34. onNavigationBarButtonTap(e) {
  35. let that = this
  36. uni.showLoading({
  37. title: "打开中...",
  38. })
  39. if (that.list1.indexOf(that.path) == -1) {
  40. uni.downloadFile({
  41. url: that.path,
  42. success: function(res) {
  43. console.log(res)
  44. if (res.statusCode === 200) {
  45. that.list1.push(that.path)
  46. that.list2.push(res.tempFilePath)
  47. uni.openDocument({
  48. filePath: res.tempFilePath,
  49. success: function(res) {
  50. uni.hideLoading();
  51. }
  52. });
  53. } else {
  54. uni.hideLoading()
  55. uni.showToast({
  56. title: "下载失败",
  57. icon: 'none'
  58. })
  59. }
  60. },
  61. fail: function(res) {
  62. uni.hideLoading()
  63. uni.showToast({
  64. title: "下载失败",
  65. icon: 'none'
  66. })
  67. }
  68. });
  69. } else {
  70. uni.openDocument({
  71. filePath: that.list2[that.list1.indexOf(that.path)],
  72. success: function(res) {
  73. uni.hideLoading();
  74. }
  75. });
  76. }
  77. },
  78. methods: {
  79. loadSuccess() {
  80. uni.hideLoading()
  81. },
  82. onWebViewLoaded() {
  83. console.log('加载完成')
  84. }
  85. },
  86. onReady() {
  87. setTimeout(function() {
  88. // wv = currentWebview.children()[0]
  89. // wv.setStyle({
  90. // scalable: true
  91. // })
  92. }, 1000); //如果是页面初始化调用时,需要延时一下
  93. }
  94. };
  95. </script>
  96. <style scoped lang="scss">
  97. .wrap {
  98. width: 100vw;
  99. height: 100vh;
  100. padding: 40px 0 0 0;
  101. .content {
  102. white-space: pre;
  103. width: 100%;
  104. height: 100%;
  105. padding: 20px 10px;
  106. background: #fff;
  107. overflow-x: auto;
  108. }
  109. .van-loading {
  110. background-color: #fff;
  111. position: absolute;
  112. width: 100vw;
  113. height: 100vh;
  114. display: flex;
  115. align-items: center;
  116. justify-content: center;
  117. }
  118. }
  119. </style>