JSBridge.js
3.84 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
function AppClient(appObj, isAndroid) {
this.appObj = appObj;
this.isAndroid = isAndroid;
};
AppClient.prototype.closePage = function() {
if (this.isAndroid) {
this.appObj.closePageAction();
} else {
this.appObj.callHandler("closePageAction");
}
}
AppClient.prototype.login = function() {
if (this.isAndroid) {
this.appObj.closePageAction();
} else {
this.appObj.callHandler("closePageAction");
}
}
function isJson(str) {
try {
JSON.parse(str);
} catch (e) {
return false;
}
return true;
}
// window.invokeJsPassCompanyCodeAndName = function(_params) {
// companyCode = _params.companyCode;
// companyName = _params.companyName;
// var jsonStr = "";
// getSign(jsonStr);
// }
//
AppClient.prototype.getERCode = function (callbackName, callback, jsonStr) {
var dataParams = "";
if(isJson(jsonStr)){
dataParams = JSON.parse(jsonStr);
}
var iOSData = {
from: 'H5',
params: dataParams
};
if (this.isAndroid) {
this.appObj.getERCode('H5', jsonStr);
} else {
this.appObj.callHandler("getERCode", iOSData, callback);
this.appObj.registerHandler('postERCode', function(data, responseCallback) {
layer.open({
content: JSON.stringify(data),
time: 10
});
// log('ObjC called testJavascriptHandler with', data)
// var responseData = { 'Javascript Says':'Right back atcha!' }
// log('JS responding with', responseData)
// responseCallback(responseData)
})
}
};
AppClient.prototype.getGPS = function (callbackName, callback, jsonStr) {
var dataParams = "";
if(isJson(jsonStr)){
dataParams = JSON.parse(jsonStr);
}
var iOSData = {
from: 'H5',
params: dataParams
};
if (this.isAndroid) {
this.appObj.getGPS('H5', jsonStr);
} else {
this.appObj.callHandler("getGPS", iOSData, callback);
this.appObj.registerHandler('postGPS', function(data, responseCallback) {
var res = JSON.parse(data)
layer.open({
content: data,
time: 2
});
layer.open({
content: JSON.stringify(key),
time: 2
});
var key = res['testKey']
layer.open({
content: JSON.stringify(key),
time: 2
});
// log('ObjC called testJavascriptHandler with', data)
// var responseData = { 'Javascript Says':'Right back atcha!' }
// log('JS responding with', responseData)
// responseCallback(responseData)
})
}
};
//
// window. = function (_params) {
//
// }
// window.postGPS = function (params) {
// layer.open({
// content: JSON.stringify(_params),
// time: 10
// });
// }
var appClient = null;
if(window.android) {
appClient = new AppClient(window.android, true);
// 此处可以调用一些init的方法
} else {
layer.open({
content: '初始化',
time: 2
})
function setupWebViewJavascriptBridge(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 = 'https://__bridge_loaded__';
document.documentElement.appendChild(WVJBIframe);
setTimeout(function() { document.documentElement.removeChild(WVJBIframe) }, 0)
}
setupWebViewJavascriptBridge(function(bridge) {
appClient = new AppClient(bridge, false);
// 此处可以调用一些init的方法
});
}