office.vue 2.4 KB

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