374 lines
10 KiB
JavaScript
374 lines
10 KiB
JavaScript
var ANDROID = 1;
|
||
var IOS = 2;
|
||
var curApp = "";
|
||
var ua = window.navigator.userAgent.toLowerCase();
|
||
if (/android/.test(ua)) {
|
||
curApp = ANDROID;
|
||
} else if (/(iphone|ipad|ipod|ios)/.test(ua)) {
|
||
curApp = IOS;
|
||
} else {
|
||
document.write("curApp => 其他类型 => " + ua);
|
||
}
|
||
if (curApp === ANDROID) {
|
||
var bridge = {
|
||
//default:this,
|
||
call: function (b, a, c) {
|
||
var e = "";
|
||
"function" == typeof a && ((c = a), (a = {}));
|
||
a = {
|
||
data: void 0 === a ? null : a,
|
||
};
|
||
if ("function" == typeof c) {
|
||
var g = "dscb" + window.dscb++;
|
||
window[g] = c;
|
||
a._dscbstub = g;
|
||
}
|
||
a = JSON.stringify(a);
|
||
if (window._dsbridge) e = _dsbridge.call(b, a);
|
||
else if (window._dswk || -1 != navigator.userAgent.indexOf("_dsbridge"))
|
||
e = prompt("_dsbridge=" + b, a);
|
||
return JSON.parse(e || "{}").data;
|
||
},
|
||
register: function (b, a, c) {
|
||
c = c ? window._dsaf : window._dsf;
|
||
window._dsInit ||
|
||
((window._dsInit = !0),
|
||
setTimeout(function () {
|
||
bridge.call("_dsb.dsinit");
|
||
}, 0));
|
||
"object" == typeof a ? (c._obs[b] = a) : (c[b] = a);
|
||
},
|
||
registerAsyn: function (b, a) {
|
||
this.register(b, a, !0);
|
||
},
|
||
hasNativeMethod: function (b, a) {
|
||
return this.call("_dsb.hasNativeMethod", {
|
||
name: b,
|
||
type: a || "all",
|
||
});
|
||
},
|
||
disableJavascriptDialogBlock: function (b) {
|
||
this.call("_dsb.disableJavascriptDialogBlock", {
|
||
disable: !1 !== b,
|
||
});
|
||
},
|
||
};
|
||
!(function () {
|
||
//!function "执行"
|
||
if (!window._dsf) {
|
||
var b = {
|
||
_dsf: {
|
||
_obs: {},
|
||
},
|
||
_dsaf: {
|
||
_obs: {},
|
||
},
|
||
dscb: 0,
|
||
dsBridge: bridge,
|
||
close: function () {
|
||
bridge.call("_dsb.closePage");
|
||
},
|
||
_handleMessageFromNative: function (a) {
|
||
var e = JSON.parse(a.data),
|
||
b = {
|
||
id: a.callbackId,
|
||
complete: !0,
|
||
},
|
||
c = this._dsf[a.method],
|
||
d = this._dsaf[a.method],
|
||
h = function (a, c) {
|
||
b.data = a.apply(c, e);
|
||
bridge.call("_dsb.returnValue", b);
|
||
},
|
||
k = function (a, c) {
|
||
e.push(function (a, c) {
|
||
b.data = a;
|
||
b.complete = !1 !== c;
|
||
bridge.call("_dsb.returnValue", b);
|
||
});
|
||
a.apply(c, e);
|
||
};
|
||
if (c) h(c, this._dsf);
|
||
else if (d) k(d, this._dsaf);
|
||
else if (((c = a.method.split(".")), !(2 > c.length))) {
|
||
a = c.pop();
|
||
var c = c.join("."),
|
||
d = this._dsf._obs,
|
||
d = d[c] || {},
|
||
f = d[a];
|
||
f && "function" == typeof f
|
||
? h(f, d)
|
||
: ((d = this._dsaf._obs),
|
||
(d = d[c] || {}),
|
||
(f = d[a]) && "function" == typeof f && k(f, d));
|
||
}
|
||
},
|
||
},
|
||
a;
|
||
for (a in b) window[a] = b[a];
|
||
bridge.register("_hasJavascriptMethod", function (a, b) {
|
||
b = a.split(".");
|
||
if (2 > b.length) return !(!_dsf[b] && !_dsaf[b]);
|
||
a = b.pop();
|
||
b = b.join(".");
|
||
return (b = _dsf._obs[b] || _dsaf._obs[b]) && !!b[a];
|
||
});
|
||
}
|
||
})();
|
||
} else if (curApp == IOS) {
|
||
var jsBridge = function (bridgeName, bridgeScheme) {
|
||
this.bridgeName = bridgeName || "WebViewJavascriptBridge";
|
||
this.bridgeScheme = bridgeScheme || "https";
|
||
var ua = navigator.userAgent || navigator.vendor || window.opera;
|
||
var ANDROID = 1;
|
||
var IOS = 2;
|
||
var SUCCESS_CODE = 0;
|
||
ua = ua.toLowerCase();
|
||
if (/android/.test(ua)) {
|
||
this.app = ANDROID;
|
||
} else if (/(iphone|ipad|ipod|ios)/.test(ua)) {
|
||
this.app = IOS;
|
||
}
|
||
|
||
/* 获取 bridge */
|
||
this.getBridge = function (callback) {
|
||
if (window[this.bridgeName]) {
|
||
callback(window[this.bridgeName]);
|
||
return;
|
||
}
|
||
if (this.app === ANDROID) {
|
||
document.addEventListener(
|
||
"WebViewJavascriptBridgeReady",
|
||
function () {
|
||
callback(window[this.bridgeName]);
|
||
},
|
||
false
|
||
);
|
||
} else if (this.app === IOS) {
|
||
// WebViewJavascriptBridge 由native在注入
|
||
// https://github.com/marcuswestin/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);
|
||
}
|
||
};
|
||
/**
|
||
* 给ios和android注册事件监听
|
||
*
|
||
* @memberof jsBridge
|
||
*/
|
||
this.on = function (event, jsHandler) {
|
||
this.registHandler(event, jsHandler);
|
||
};
|
||
|
||
/**
|
||
* 默认监听事件
|
||
*
|
||
* @memberof jsBridge
|
||
*/
|
||
this.registDefaultHandlerForApp = function () {
|
||
this.on("reload", function () {
|
||
location.reload();
|
||
});
|
||
};
|
||
|
||
/**
|
||
* 注册事件逻辑
|
||
*
|
||
* @memberof jsBridge
|
||
*/
|
||
this.registHandler = function (event, jsHandler) {
|
||
this.getBridge(function (bridge) {
|
||
if (
|
||
bridge &&
|
||
bridge.registerHandler &&
|
||
typeof bridge.registerHandler === "function"
|
||
) {
|
||
bridge.registerHandler(event, function (data2js, responseCallback) {
|
||
responseCallback = responseCallback || function () {};
|
||
jsHandler(data2js, responseCallback);
|
||
});
|
||
}
|
||
});
|
||
};
|
||
/**
|
||
* 调用 app
|
||
* @param {string} event
|
||
* @param {object} params
|
||
* @param {fun} callback
|
||
* @return JSON对象
|
||
*/
|
||
this.callHandler = function (event, params, callback) {
|
||
this.getBridge(function (bridge) {
|
||
if (
|
||
bridge &&
|
||
bridge.registerHandler &&
|
||
typeof bridge.registerHandler === "function"
|
||
) {
|
||
bridge.callHandler(event, params, callback);
|
||
}
|
||
});
|
||
};
|
||
this.registDefaultHandlerForApp();
|
||
|
||
return this;
|
||
};
|
||
}
|
||
|
||
if (!jsBridge) {
|
||
jsBridge = function () {};
|
||
}
|
||
jsBridge.prototype.curApp = curApp;
|
||
/**
|
||
* 扫码
|
||
*/
|
||
jsBridge.prototype.scanQRCode = function (params, success) {
|
||
if (this.curApp == ANDROID) {
|
||
dsBridge.call("scanQRCode", params, success);
|
||
} else if (this.curApp == IOS) {
|
||
this.callHandler("scanQRCode", params, success);
|
||
}
|
||
};
|
||
/**
|
||
* 获取BTY地址
|
||
*/
|
||
jsBridge.prototype.getCurrentBTYAddress = function (params, success) {
|
||
if (this.curApp == ANDROID) {
|
||
dsBridge.call("getCurrentBTYAddress", params, success);
|
||
} else if (this.curApp == IOS) {
|
||
this.callHandler("getCurrentBTYAddress", params, success);
|
||
}
|
||
};
|
||
/**
|
||
* 获取所有币种的地址:getAddress
|
||
* 入参:cointype为BTC,ETH,TRX,BTY等主链
|
||
*/
|
||
jsBridge.prototype.getAddress = function (params, success) {
|
||
if (this.curApp == ANDROID) {
|
||
dsBridge.call("getAddress", params, success);
|
||
} else if (this.curApp == IOS) {
|
||
this.callHandler("getAddress", params, success);
|
||
}
|
||
};
|
||
/**
|
||
* 设置标题
|
||
*/
|
||
jsBridge.prototype.setTitle = function (params, success) {
|
||
if (this.curApp == ANDROID) {
|
||
dsBridge.call("setTitle", params, success);
|
||
} else if (this.curApp == IOS) {
|
||
this.callHandler("setTitle", params, success);
|
||
}
|
||
};
|
||
/**
|
||
* 获取当前语言
|
||
*/
|
||
jsBridge.prototype.getLang = function (params, success) {
|
||
if (this.curApp == ANDROID) {
|
||
dsBridge.call("getLang", params, success);
|
||
} else if (this.curApp == IOS) {
|
||
this.callHandler("getLang", params, success);
|
||
}
|
||
};
|
||
/**
|
||
* 返回上一个网页
|
||
*/
|
||
jsBridge.prototype.closeCurrentWebview = function (params, success) {
|
||
if (this.curApp == ANDROID) {
|
||
dsBridge.call("closeCurrentWebview", params, success);
|
||
} else if (this.curApp == IOS) {
|
||
this.callHandler("closeCurrentWebview", params, success);
|
||
}
|
||
};
|
||
/**
|
||
* 获取设备id:getDeviceId
|
||
*/
|
||
jsBridge.prototype.getDeviceId = function (params, success) {
|
||
if (this.curApp == ANDROID) {
|
||
dsBridge.call("getDeviceId", params, success);
|
||
} else if (this.curApp == IOS) {
|
||
this.callHandler("getDeviceId", params, success);
|
||
}
|
||
};
|
||
/**
|
||
* 跳转钱包 gotoWallet
|
||
*/
|
||
jsBridge.prototype.gotoWallet = function (params, success) {
|
||
if (this.curApp == ANDROID) {
|
||
dsBridge.call("gotoWallet", params, success);
|
||
} else if (this.curApp == IOS) {
|
||
this.callHandler("gotoWallet", params, success);
|
||
}
|
||
};
|
||
/**
|
||
* 微信分享
|
||
*/
|
||
jsBridge.prototype.wechatShare = function (params, success) {
|
||
if (this.curApp == ANDROID) {
|
||
dsBridge.call("wechatShare", params, success);
|
||
} else if (this.curApp == IOS) {
|
||
this.callHandler("wechatShare", params, success);
|
||
}
|
||
};
|
||
/**
|
||
* 构造数据:create
|
||
*/
|
||
jsBridge.prototype.create = function (params, success) {
|
||
if (this.curApp == ANDROID) {
|
||
dsBridge.call("create", params, success);
|
||
} else if (this.curApp == IOS) {
|
||
this.callHandler("create", params, success);
|
||
}
|
||
};
|
||
/**
|
||
* 普通签名:sign
|
||
*/
|
||
jsBridge.prototype.sign = function (params, success) {
|
||
if (this.curApp == ANDROID) {
|
||
dsBridge.call("sign", params, success);
|
||
} else if (this.curApp == IOS) {
|
||
this.callHandler("sign", params, success);
|
||
}
|
||
};
|
||
/**
|
||
* 交易组签名:signTxGroup
|
||
* 入参:交易组只支持BTY
|
||
*/
|
||
jsBridge.prototype.signTxGroup = function (params, success) {
|
||
if (this.curApp == ANDROID) {
|
||
dsBridge.call("signTxGroup", params, success);
|
||
} else if (this.curApp == IOS) {
|
||
this.callHandler("signTxGroup", params, success);
|
||
}
|
||
};
|
||
/**
|
||
* 发送交易:send
|
||
* cointype为BTC,ETH,TRX,BTY等主链
|
||
*/
|
||
jsBridge.prototype.send = function (params, success) {
|
||
if (this.curApp == ANDROID) {
|
||
dsBridge.call("send", params, success);
|
||
} else if (this.curApp == IOS) {
|
||
this.callHandler("send", params, success);
|
||
}
|
||
};
|
||
//=====================================================================================
|
||
//==================================增加新的应用方法====================================
|
||
//=====================================================================================
|
||
if (/wallet/.test(ua)) {
|
||
console.log("在钱包环境");
|
||
} else {
|
||
console.log("不是在钱包环境");
|
||
}
|
||
|
||
//export default new jsBridge();
|
||
var jsBridge = new jsBridge();
|