index.vue 2.5 KB
Newer Older
M
mehaotian 已提交
1
<template>
fxy060608's avatar
fxy060608 已提交
2
  <uni-web-view v-on="$listeners" />
M
mehaotian 已提交
3
</template>
Q
qiang 已提交
4 5 6
<script>
import {
  WEBVIEW_INSERTED,
雪洛's avatar
雪洛 已提交
7 8
  WEBVIEW_REMOVED
} from '../../../constants'
M
mehaotian 已提交
9 10 11 12 13 14 15
let webview = false
const insertHTMLWebView = ({
  htmlId
}) => {
  const parentWebview = plus.webview.currentWebview()
  // fixed by hxy web-view 组件所在的 webview 不注入 uni-app 框架
  const styles = {
Q
qiang 已提交
16 17
    'uni-app': 'none',
    isUniH5: true
M
mehaotian 已提交
18 19 20
  }
  const parentTitleNView = parentWebview.getTitleNView()
  if (parentTitleNView) {
Q
qiang 已提交
21 22 23 24
    if (plus.navigator.isImmersedStatusbar()) {
      styles.top = 44 + plus.navigator.getStatusbarHeight()
    } else {
      styles.top = 44
25
    }
M
mehaotian 已提交
26 27 28 29 30
    styles.bottom = 0
  }
  webview = plus.webview.create('', htmlId, styles)
  if (parentTitleNView) {
    webview.addEventListener('titleUpdate', function () {
fxy060608's avatar
fxy060608 已提交
31
      const title = webview.getTitle()
M
mehaotian 已提交
32 33 34 35 36 37
      parentWebview.setStyle({
        titleNView: {
          titleText: (!title || title === 'null') ? '' : title
        }
      })
    })
Q
qiang 已提交
38
  }
M
mehaotian 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
  plus.webview.currentWebview().append(webview)
}

const updateHTMLWebView = ({
  htmlId,
  src,
  webviewStyles
}) => {
  // fixed by xxx 非空时才执行更新操作
  const realPath = src || ''
  if (!realPath) {
    return
  }
  if (/^(http|https):\/\//.test(realPath) && webviewStyles.progress) {
    webview.setStyle({
      progress: {
        color: webviewStyles.progress.color
      }
    })
Q
qiang 已提交
58
  }
M
mehaotian 已提交
59
  webview.loadURL(realPath)
Q
qiang 已提交
60
}
M
mehaotian 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

const removeHTMLWebView = () => {
  plus.webview.currentWebview().remove(webview)
  webview.close('none')
  webview = false
}

export default {
  name: 'WebView',
  props: {
    src: {
      type: String,
      default: ''
    },
    webviewStyles: {
      type: Object,
      default () {
        return {}
      }
    }
  },
  watch: {
    src (val, oldVal) {
      webview && updateHTMLWebView({
        src: this.$getRealPath(val),
        webviewStyles: this.webviewStyles
      })
    }
  },
  mounted () {
    this.htmlId = 'webviewId' + this.$page.id
    insertHTMLWebView({
      htmlId: this.htmlId
    })
    updateHTMLWebView({
      src: this.$getRealPath(this.src),
      webviewStyles: this.webviewStyles
Q
qiang 已提交
98 99
    })
    UniViewJSBridge.publishHandler(WEBVIEW_INSERTED, {}, this.$page.id)
M
mehaotian 已提交
100 101
  },
  beforeDestroy () {
Q
qiang 已提交
102 103
    removeHTMLWebView()
    UniViewJSBridge.publishHandler(WEBVIEW_REMOVED, {}, this.$page.id)
M
mehaotian 已提交
104 105 106 107 108 109 110 111 112 113 114
  }
}
</script>
<style>
  uni-web-view {
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
  }
Q
qiang 已提交
115
</style>