// euclid hybrid bridge
let euclid = (function() {
/**************** properties ******************/
let callbacks = {};
function registerCallback(callback) {
let callbackId = uuid();
callbacks[callbackId] = callback;
return callbackId;
}
/**************** native call js ******************/
function androidCallJs(params) {
if (typeof params==="object") {
nativeCallJs(JSON.stringify(params));
}
else {
nativeCallJs(params);
}
}
function iOSCallJs(params) {
nativeCallJs(params);
}
function nativeCallJs(paramsString) {
console.debug("nativeCallJs:" + paramsString);
let params = JSON.parse(paramsString);
let callbackId = params["callbackId"];
let callback = euclid.callbacks[callbackId];
console.info(callbacks.toString());
for (let key in euclid.callbacks) {
console.info(key);
console.info(euclid.callbacks[key]);
}
console.info(callbackId);
console.info(callback);
if (callback !== undefined && callback != null) {
common.log(callback.name);
callback(params);
}
}
/**************** js call native ******************/
function uuid() {
var s = [];
var hexDigits = "0123456789abcdef";
for (var i = 0; i < 36; i++) {
s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
}
s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
s[8] = s[13] = s[18] = s[23] = "-";
var uuid1 = s.join("");
return uuid1;
}
function jsCallIOS(paramsString) {
EuclidAndroidBridge.jsCallIOS(paramsString);
}
function jsCallAndroid(paramsString) {
EuclidAndroidBridge.jsCallAndroid(paramsString);
}
function jsCallNative(plugin, method, params, callback) {
let callbackId = registerCallback(callback);
let paramsString = JSON.stringify({plugin: plugin, method: method, callbackId: callbackId, params: params === null ? {} : params});
let u = navigator.userAgent;
let isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
if (isIOS) {
jsCallIOS(paramsString);
}
else if (isAndroid) {
jsCallAndroid(paramsString);
}
else {
alert("请使用手机访问");
}
}
/**************** plugins ******************/
//照相机插件
let camera = (function () {
let pluginName = "camera";
//拍照
let takePicture = function takePicture(params, callback) {
console.info("拍照");
jsCallNative(pluginName,"takePicture", params, callback);
};
return {
takePicture: takePicture
}
})();
//common插件
let common = (function () {
let pluginName = "common";
//拍照
let screenShot = function screenShot(params, callback) {
jsCallNative(pluginName, "screenShot", params, callback);
};
let paramsSign = function paramsSign(params, callback) {
jsCallNative(pluginName,"paramsSign", params, callback);
};
let log = function log(params) {
if (typeof params === "string") {
params = {log: params};
}
jsCallNative(pluginName,"log", params);
};
let base64ToImage = function base64ToImage(base64str) {
// 去掉url的头,并转化为byte
let bytes = window.atob(base64str);
// 处理异常,将ascii码小于0的转换为大于0
let ab = new ArrayBuffer(bytes.length);
// 生成视图(直接针对内存):8位无符号整数,长度1个字节
let ia = new Uint8Array(ab);
for (let i = 0; i < bytes.length; i++) {
ia[i] = bytes.charCodeAt(i);
}
return new Blob([ab], {
type: mine
});
};
return {
log: log,
screenShot: screenShot,
paramsSign: paramsSign,
base64ToImage
}
})();
/**************** public ******************/
return {
//公有方法
camera: camera,
common: common,
//私有属性
callbacks: callbacks,
//iOS call js
iOSCallJs: iOSCallJs,
//android call js
androidCallJs: androidCallJs,
};
})();
网友评论