Skip to content

Commit

Permalink
feat: 增加webview组件
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangzhenfei committed Nov 18, 2020
1 parent 2e94186 commit bb40e63
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
73 changes: 73 additions & 0 deletions components/pi-webview/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<template>
<view class="pi-scroll-container">
<!-- #ifndef MP -->
<pi-navbar :show-home="false">{{ title }}</pi-navbar>
<!-- #endif -->
<!-- #ifndef APP-PLUS -->
<web-view :progress="true" :src="webviewURL" class="pi-scroll pi-safearea" />
<!-- #endif -->
</view>
</template>

<script>
export default {
name: 'PiWebview',
props: {
title: {
type: String
},
url: {
type: String
}
},
data() {
return {
webviewURL: ''
}
},
created() {
this.init()
},
methods: {
init() {
// 设置标题
uni.setNavigationBarTitle({
title: this.title
})
if (!this.url) {
return this.$toast('打开页面URL为空,请检查')
}
// H5 url 需要加时间戳,否则会有缓存问题
this.webviewURL = this.urlAddParam(this.url || '', {
sadaisTimestamp: new Date().getTime() + '-' + Math.round(Math.random(1) * 10000)
})
console.log('webview页面即将打开以下URL:', this.webviewURL)
// #ifdef APP-PLUS
const wv = plus.webview.create('', 'custom-webview', {
'plusrequire': 'none', // 禁止远程网页使用plus的API,有些使用mui制作的网页可能会监听plus.key,造成关闭页面混乱,可以通过这种方式禁止
'uni-app': 'none', // 不加载uni-app渲染层框架,避免样式冲突
'top': uni.getSystemInfoSync().statusBarHeight + 44 // 放置在titleNView下方。如果还想在webview上方加个地址栏的什么的,可以继续降低TOP值
})
wv.loadURL(this.webviewURL)
const currentWebview = this.$mp.page.$getAppWebview() // 获取当前页面的webview对象
currentWebview.append(wv) // 一定要append到当前的页面里,才能跟随当前页面一起做动画,一起关闭
// #endif
},
urlAddParam(url, params) {
params = this.$pi.navi.objToUrl(params)
if (url.indexOf('?') !== -1 && params) {
url = url + params.replace('?', '&')
} else {
url = url + params
}
return url
}
}
}
</script>

<style lang="scss" scoped>
.pi-scroll-container {
background-color: #ffffff;
}
</style>
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sadais-piui",
"version": "1.0.20",
"version": "1.0.21",
"description": "piui组件库",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit bb40e63

Please sign in to comment.