customWebview.vue
4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<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>