-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathjsBridge-plugin.js
86 lines (72 loc) · 2.59 KB
/
jsBridge-plugin.js
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
'use strict'
import device from './device'
var JsBridgePlugin = {};
JsBridgePlugin.install = function (Vue, option) {
//初始化jsBridge
Vue.prototype.$jsBridge = {
registerHandler : function (name,callback) {
console.error('没有注册成功')
},
callHandler : function (name, params, callback) {
console.error('没有调用成功')
}
}
//所有原生和js通信的命令
Vue.prototype.$jsBridgeCmd = {
chooseImage : 'chooseImage',
}
//先注释掉 初始化jsBridge 需要用到时再初始化
if(device.webView){
if(device.android){
androidSetWebViewJavascriptBridge(function(bridge) {
Vue.prototype.$jsBridge = bridge;
console.log('初始化$jsBridge'+ (typeof bridge.registerHandler == 'function'))
})
}else if(device.ios){
iosSetupWebViewJavascriptBridge(function(bridge) {
Vue.prototype.$jsBridge = bridge
// console.log('初始化$jsBridge',Vue.prototype.$jsBridge)
})
}
}
};
export default JsBridgePlugin;
//安卓
export function androidSetWebViewJavascriptBridge(callback) {
if (window.WebViewJavascriptBridge) {
console.log('有WebViewJavascriptBridge对象')
return callback(WebViewJavascriptBridge)
} else {
console.log('没有WebViewJavascriptBridge对象,等待WebViewJavascriptBridgeReady事件回调')
document.addEventListener(
'WebViewJavascriptBridgeReady'
, function(){
console.log('WebViewJavascriptBridgeReady事件回调了')
callback(WebViewJavascriptBridge)
},
false
);
}
}
//ios
export function iosSetupWebViewJavascriptBridge(callback) {
if (window.WebViewJavascriptBridge) { return callback(WebViewJavascriptBridge); }
if (window.WVJBCallbacks) { return window.WVJBCallbacks.push(callback); }
window.WVJBCallbacks = [callback];
var WVJBIframe = document.createElement('iframe');
WVJBIframe.style.display = 'none';
WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__';
document.documentElement.appendChild(WVJBIframe);
setTimeout(function() { document.documentElement.removeChild(WVJBIframe) }, 0)
}
let func = function () {
};
if(device.webView) {
if (device.android) {
func = androidSetWebViewJavascriptBridge
} else if (device.ios) {
func = iosSetupWebViewJavascriptBridge
}
}
//在路由中使用这个方法 注册web方法让原生调用
export let setJsBridge = func;