| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- <template>
- <view class="wv-page">
- <view class="art-nav wv-nav" :style="{ paddingTop: statusBar + 'px' }">
- <view class="art-nav-row">
- <view class="wv-back" @tap="closePage">
- <text class="wv-back-icon">←</text>
- </view>
- <text class="art-nav-title">{{ navTitle }}</text>
- <view class="wv-home" @tap="goHome">
- <image class="wv-home-icon" src="/static/icon/icon-home.png" mode="aspectFit" />
- </view>
- </view>
- </view>
- <web-view v-if="pageUrl" class="wv-frame" :style="{ height: webViewHeight + 'px' }" :webview-styles="webviewStyles"
- :src="pageUrl" />
- <view v-if="!pageUrl" class="wv-empty" :style="{ height: webViewHeight + 'px' }">
- <text class="wv-empty-text">{{ copy.invalidLink }}</text>
- </view>
- </view>
- </template>
- <script>
- export default {
- data() {
- return {
- statusBar: 0,
- navTop: 0,
- webViewHeight: 600,
- navTitle: '',
- pageUrl: '',
- webviewStyles: {
- progress: {
- color: '#4a8cff'
- }
- }
- }
- },
- computed: {
- copy() {
- const t = this.$t.bind(this)
- return {
- invalidLink: t('common.invalidLink'),
- defaultTitle: t('common.webViewTitle')
- }
- }
- },
- onLoad(option) {
- const sys = uni.getSystemInfoSync()
- this.statusBar = sys.statusBarHeight || 0
- const navBarInner = uni.upx2px(88)
- this.navTop = this.statusBar + navBarInner
- this.webViewHeight = (sys.windowHeight || 667) - this.navTop
- let title = option.title || ''
- try {
- title = decodeURIComponent(title)
- } catch (_) { }
- this.navTitle = title || this.copy.defaultTitle
- this.webviewStyles = {
- progress: {
- color: '#4a8cff'
- },
- hideCallback: null,
- top: `${this.navTop}px`,
- height: `${this.webViewHeight}px`
- }
- const rawUrl = option.url ? decodeURIComponent(option.url) : ''
- this.pageUrl = this.normalizeUrl(rawUrl)
- },
- onReady() {
- // #ifdef APP-PLUS
- if (this.pageUrl) {
- this.adjustAppWebviewLayout()
- this.injectHideScript()
- }
- // #endif
- },
- methods: {
- // 注入隐藏广告的脚本
- injectHideScript() {
- const currentWebview = this.$scope.$getAppWebview()
- const webview = currentWebview.children()[0]
- if (!webview) return
- webview.removeEventListener('loaded', this.hideCallback)
- this.hideCallback = function () {
- const currentUrl = webview.getURL()
- if (currentUrl) {
- const jsCode = `
- (function() {
- var banner = document.querySelector('.float-banner-con');
- if (banner) banner.style.display = 'none';
- var overlay = document.querySelector('.mask, .overlay, .popup-mask');
- if (overlay) overlay.style.display = 'none';
- })();
- `
- webview.evalJS(jsCode)
- }
- }
- webview.addEventListener('loaded', this.hideCallback)
- },
- normalizeUrl(raw) {
- const url = String(raw || '').trim()
- if (!url) return ''
- if (/^https?:\/\//i.test(url)) return url
- if (url.startsWith('//')) return `https:${url}`
- return ''
- },
- adjustAppWebviewLayout(retry = 0) {
- // #ifdef APP-PLUS
- const pages = getCurrentPages()
- const page = pages[pages.length - 1]
- if (!page || !page.$getAppWebview) return
- const currentWebview = page.$getAppWebview()
- const apply = () => {
- const children = currentWebview.children()
- if (!children || !children.length) {
- if (retry < 10) {
- setTimeout(() => this.adjustAppWebviewLayout(retry + 1), 200)
- }
- return
- }
- const wv = children[0]
- wv.setStyle({
- top: `${this.navTop}px`,
- height: `${this.webViewHeight}px`,
- bottom: 'auto'
- })
- }
- setTimeout(apply, retry === 0 ? 300 : 0)
- // #endif
- },
- closePage() {
- const pages = getCurrentPages()
- if (pages.length > 1) {
- uni.navigateBack({ delta: 1 })
- return
- }
- this.goHome()
- },
- goHome() {
- uni.switchTab({
- url: '/pages/index/index',
- fail: () => uni.reLaunch({ url: '/pages/index/index' })
- })
- }
- }
- }
- </script>
- <style scoped>
- @import '../article/article-shared.css';
- .wv-page {
- min-height: 100vh;
- background-color: #ffffff;
- }
- .wv-nav {
- position: relative;
- z-index: 999;
- }
- .wv-back {
- min-width: 88rpx;
- height: 88rpx;
- margin-left: -12rpx;
- padding: 0 8rpx;
- display: flex;
- align-items: center;
- justify-content: center;
- box-sizing: border-box;
- }
- .wv-back-icon {
- font-size: 52rpx;
- font-weight: 600;
- color: #1a1d26;
- line-height: 1;
- }
- .wv-home {
- min-width: 88rpx;
- height: 88rpx;
- margin-right: -12rpx;
- padding: 0 8rpx;
- display: flex;
- align-items: center;
- justify-content: center;
- box-sizing: border-box;
- }
- .wv-home-icon {
- width: 48rpx;
- height: 48rpx;
- }
- .wv-frame {
- width: 100%;
- }
- .wv-empty {
- display: flex;
- align-items: center;
- justify-content: center;
- }
- .wv-empty-text {
- font-size: 28rpx;
- color: #8a8d9e;
- }
- </style>
|