customWebview.vue 4.5 KB
<template>
	<view>
		<!-- <web-view :src="url" fullscreen='false'></web-view> -->
	</view>
</template>

<script>
	import {
		default as app_config
	} from '@/utils/app.js';
	import common from '@/utils/common.js'
	
	import loginHelper from '../utils/loginHelper.js'
	
	var wv; //计划创建的webview
	var nwating;
	export default {
		name: "customWebview",
		data() {
			return {
				url: this.src
			};
		},
		props: {
			src: {
				type: String,
				default: () => ""
			},
			top: {
				type: Number,
				default: () => 0
			},
			height: {
				type: Number,
				default: () => uni.getSystemInfoSync().windowHeight
			}
		},
		created() {
			
			uni.$on("loginStatus", function(arg) {
				if(arg) {
					wv.reload()
					console.log("loginStatus:" + arg)
				}
			})
			
			let appInfo = app_config.app_info;
			let header = {
				AppVersion: encodeURI(JSON.stringify(appInfo))
			}
			// #ifdef APP-PLUS
			nwating = plus.nativeUI.showWaiting();
			wv = plus.webview.create("", "normal_h5-webview", {
			    additionalHttpHeaders: {
			        header: header,
			    },
			    plusrequire: "none", //禁止远程网页使用plus的API,有些使用mui制作的网页可能会监听plus.key,造成关闭页面混乱,可以通过这种方式禁止
			    'uni-app': 'none', //不加载uni-app渲染层框架,避免样式冲突
			    top: this.top,
			    height: this.height
			})
			let pages = getCurrentPages()
			let currentPage = pages[pages.length - 1]
			var currentWebview = currentPage.$getAppWebview() //此对象相当于html5plus里的plus.webview.currentWebview()。在uni-app里vue页面直接使用plus.webview.currentWebview()无效
			currentWebview.append(wv);
			console.log(header)
			wv.loadURL(this.url, header);
			wv.addEventListener('loaded', function () {
			    // wv.evalJS("$(\".header\").hide();");
				console.log("load---")
			    nwating.close();
			    wv.show();
			}, false);
			var _this = this;
			wv.overrideUrlLoading({
				effect: "touchstart",
				mode: "allow",
				match: "^https:\/\/app\.ydniu\.com\/experts\/(kl8|sd|pl3|ssq|dlt|pl5|zq)\?zqe&app_header=true$"
				// exclude: "redirect"
			}, (e) => {
				console.log('----请求拦截了------', e.url + "--" + _this.url)
		
				if (e.url === _this.url) {
					console.log("reload")
					wv.reload();
				} else if (e.url.indexOf("login") !== -1) {
					// 前端检测到未登录,需要登录,说明当前登录信息已经失效,清除登录信息
					loginHelper.logout()
					console.log('-------拦截请求login', e.url)
					uni.navigateTo({
						url: `/pages/login_pwd/login_pwd`
					})
				} else if ((e.url.indexOf("?zq") !== -1 || e.url.indexOf("user/payment") !== -1) && e.url
					.startsWith("http")) {
					console.log('-------拦截请求?zq', e.url)
					let tempData = {
						url: e.url
					}
					
					uni.navigateTo({
						url: `/pages/notitle_webview/notitle_webview?info=${encodeURIComponent(JSON.stringify(tempData))}`
					})
				}  else if (e.url.indexOf("?ydn_back") !== -1 ||
					e.url === 'https://app.ydniu.com/' ||
					e.url === 'https://app.ydniu.com' || 
					e.url === 'https://app.ydniu.com/sports' || 
					e.url.startsWith("https://app.ydniu.com/?")) {
					console.log('-------拦截请求ydn_back', e.url)
					uni.navigateBack({})
			
				} else if (e.url.startsWith("weixin") || e.url.startsWith("alipay")) {
					console.log('-------拦截请求weixin', e.url)
					plus.runtime.openURL(e.url, err => {
						uni.showToast({
							title: '打开失败!请检查是否已安装',
							icon: 'none'
						});
					});
				} else if (e.url.startsWith("ydncp")) {
					var urlStr = e.url.substring("ydncp:".length, e.url.length);
					let tempData = {
						url: e.url,
					}
					
					uni.navigateTo({
						url:'/pages/notitle_webview/notitle_webview'
					})
					
					uni.navigateTo({
						url: `/pages/notitle_webview/notitle_webview?info=${encodeURIComponent(JSON.stringify(tempData))}`
					})
				} else {
					console.log('-------拦截请求tempData', e.url)
					let tempData = {
						url: e.url,
					}
					uni.navigateTo({
						url: `/pages/notitle_webview/notitle_webview?info=${encodeURIComponent(JSON.stringify(tempData))}`
					})
				}
				console.log('----请求拦截了End------', e.url)
			})
			// #endif
		},
		mounted() {

		},
		methods: {
			onRefreshInfo(data) {
				console.log(data)
				this.url = data
				nwating = plus.nativeUI.showWaiting();
				wv.loadURL(this.url)
			}
		}
	}
</script>

<style>

</style>