;(function(window, document, adapterName) {緩存
adapterName = adapterName || "App";app
if (window.RYMJSBRIDGE) {
return
}post
var app = new Adapter();
var slice = Array.prototype.slice;
var toString = Object.prototype.toString;
var callindex = 0;this
/**
* 調用Native接口適配層的構造器
* @constructor
*/
function Adapter() {
var ua = navigator.userAgent.toUpperCase()
this.IS_ANDROID = ua.indexOf("ANDROID") !== -1;
this.IS_IOS = ua.indexOf("IPHONE OS") !== -1;
this.IS_MOCK = ua.indexOf("MOCK") !== -1;
this.IS_ANYDOOR = ua.indexOf("ANYDOOR") !== -1;
}url
/**
* 調用一個Native方法
* @param {Array.<string> } name 方法名稱
* @param {}
*/
Adapter.prototype.call = function(name) {
// 獲取傳遞給Native方法的參數
var self = this;
var args = slice.call(arguments, 1);
var successCallback = '',
errorCallback = '',
item = null,
returnArg;
var methodName = name[name.length - 1];
if (self.IS_ANDROID && self.IS_ANYDOOR) {
if (window.HostApp) {
var _str = "",
newArguments = [];
for (var i = 0; i < args.length; i++) {
if (typeof args[i] === 'function') {
var callbackName = methodName + 'Callback' + callindex;
window[callbackName] = args[i];
newArguments.push(callbackName);
callindex++;
} else if (typeof args[i] === 'object') {
newArguments.push(JSON.stringify(args[i]));
} else {
newArguments.push(args[i]);
}
}
try {
HostApp[methodName].apply(window.HostApp, newArguments);
} catch (e) {
var params = slice.call(arguments, 0);
setTimeout(function() {
app["call"].apply(app, params);
}, 300);
}
} else {
var params = slice.call(arguments, 0);
setTimeout(function() {
app["call"].apply(app, params);
}, 1000);
}
} else if (self.IS_IOS && self.IS_ANYDOOR) {
var newArguments = "",
tempArgument = [];
for (var i = 0; i < args.length; i++) {
// tempArgument = args[i];
if (typeof args[i] === 'function') {
var callbackName = methodName + 'Callback' + callindex;
window[callbackName] = args[i];
tempArgument.push(callbackName);
callindex++;
} else {
tempArgument.push(args[i]);
}
}
callindex++;
var iframe = document.createElement("iframe");
console.log("tempArgument" + tempArgument);
var _src = 'callnative://' + methodName + '/' + encodeURIComponent(JSON.stringify(tempArgument)) + '/' + callindex;
console.log(_src);
iframe.src = _src;
iframe.style.display = "none";
document.body.appendChild(iframe);
iframe.parentNode.removeChild(iframe);
iframe = null;
} else {
console.log("未知環境");
}
}.net
/**
* 調用物理返回按鈕
*
* @return {undefined} 沒有返回
*/
Adapter.prototype.goBack = function() {
if (this.IS_ANDROID && HostApp && HostApp.goBackOrForward) {
this.call(["goBackOrForward"], function(res) {
try {
res = JSON.parse(res);
var flag = res.result;
if (flag == "true") {
console.log("success back");
} else {
console.log("success error");
}
} catch (e) {prototype
}
}, function(err) {插件
}, -1);
} else {
history.back();
}
}code
/**
* 判斷選擇最新版本,用於判斷當前版本是否在某一版本以後
*
* @param {string} version1 版本號
* @param {string} version2 版本號
* @return {string} 最新的版本號
*/
Adapter.prototype.getLatestVersion = function(version1, version2) {
if (version1 === version2) {
return version1;
}
var verArr1 = version1.split(".");
var verArr2 = version2.split(".");
var len = verArr1.length > verArr2.length ? verArr1.length : verArr2.length;
for (var i = 0; i < len; i++) {
var num1 = parseInt(verArr1[i], 10);
var num2 = parseInt(verArr2[i], 10);
if (num1 > num2) {
return version1;
} else if (num1 < num2) {
return version2;
}
}
return version1;
}orm
/**
* 部分插件的首地址爲後臺的.do服務,由SDK post參數過去後,由插件後臺決定應該跳轉到什麼地址。
* 這裏模擬了SDK的post操做,通常活動集合頁或插件跳轉時用到
*
* @param {string} version1 版本號
* @param {string} version2 版本號
* @return {string} 最新的版本號
*/
Adapter.prototype.postRedirectUrl = function(url, ticketData, deviceData) {
var form = document.createElement("form");
var itemMap = [
'<input type="hidden" name="SHA1Value" id="SHA1Value" value="' + ticketData.signature + '" />',
'<input type="hidden" name="signature" id="signature" value="' + ticketData.signature + '" />',
'<input type="hidden" name="timestamp" id="timestamp" value="' + ticketData.timestamp + '" />',
'<input type="hidden" name="SSOTicket" id="SSOTicket" value="' + ticketData.SSOTicket + '" />',
'<input type="hidden" name="deviceId" id="deviceId" value="' + deviceData.deviceId + '" />',
'<input type="hidden" name="deviceType" id="deviceType" value="' + deviceData.deviceType + '" />',
'<input type="hidden" name="osVersion" id="osVersion" value="' + deviceData.osVersion + '" />',
'<input type="hidden" name="anyDoorSdkVersion" id="anyDoorSdkVersion" value="' + deviceData.anyDoorSdkVersion + '" />',
'<input type="hidden" name="sdkVersion" id="sdkVersion" value="' + deviceData.anyDoorSdkVersion + '" />',
'<input type="hidden" name="appVersion" id="appVersion" value="' + deviceData.appVersion + '" />',
'<input type="hidden" name="appId" id="appId" value="' + deviceData.appId + '" />'
];
form.innerHTML = itemMap.join("");
form.action = url;
form.method = "POST";
form.style["display"] = "none";
document.body.appendChild(form);
form.submit();
}
/**
* 獲取SSOTicket對象的Promise調用
* @return {Promise} 一個Promise對象
*/
Adapter.prototype.getSSOTicket = function() {
var dfd = $.Deferred();
var self = this;
self.call(["sendMessage"], function(ticketInfo) {
var ticket = JSON.parse(ticketInfo);
if (ticket.SSOTicket) {
dfd.resolve(ticket);
} else {
dfd.reject({});
}
}, function(error) {
dfd.reject(error);
}, ["getSSOTicket"]);
return dfd;
}
/**
* 獲取設備信息的Promise調用,並緩存
* @return {Promise} 一個Promise對象
*/
Adapter.prototype.getDeviceInfo = function() {
var dfd = $.Deferred();
var self = this;
if (self.deviceInfo) {
setTimeout(function() {
dfd.resolve(self.deviceInfo);
}, 16);
return dfd;
} else {
self.call(["sendMessage"], function(deviceData) {
var deviceInfo = JSON.parse(deviceData);
self.deviceInfo = deviceInfo;
dfd.resolve(deviceInfo);
}, function(error) {
dfd.reject(error);
}, ["getDeviceInfo"]);
}
return dfd;
}
/**
* 調用宿主登陸
* @param {string} redirectURL
* @return {undefined}
*/
Adapter.prototype.getSSOLogin = function(redirectURL) {
var self = this;
self.call(["getSSOLogin"], function(msg) {
console.log(msg);
}, function(msg) {
console.log(msg);
}, {
redirectURL: redirectURL || ""
})
}
window[adapterName] = window.RYMJSBRIDGE = app;
})(window, document, "App")