jsbridge.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. var ANDROID = 1;
  2. var IOS = 2;
  3. var curApp = "";
  4. var ua = window.navigator.userAgent.toLowerCase();
  5. if (/android/.test(ua)) {
  6. curApp = ANDROID;
  7. } else if (/(iphone|ipad|ipod|ios)/.test(ua)) {
  8. curApp = IOS;
  9. } else {
  10. document.write("curApp => 其他类型 => " + ua);
  11. }
  12. if (curApp === ANDROID) {
  13. var bridge = {
  14. //default:this,
  15. call: function (b, a, c) {
  16. var e = "";
  17. "function" == typeof a && ((c = a), (a = {}));
  18. a = {
  19. data: void 0 === a ? null : a,
  20. };
  21. if ("function" == typeof c) {
  22. var g = "dscb" + window.dscb++;
  23. window[g] = c;
  24. a._dscbstub = g;
  25. }
  26. a = JSON.stringify(a);
  27. if (window._dsbridge) e = _dsbridge.call(b, a);
  28. else if (window._dswk || -1 != navigator.userAgent.indexOf("_dsbridge"))
  29. e = prompt("_dsbridge=" + b, a);
  30. return JSON.parse(e || "{}").data;
  31. },
  32. register: function (b, a, c) {
  33. c = c ? window._dsaf : window._dsf;
  34. window._dsInit ||
  35. ((window._dsInit = !0),
  36. setTimeout(function () {
  37. bridge.call("_dsb.dsinit");
  38. }, 0));
  39. "object" == typeof a ? (c._obs[b] = a) : (c[b] = a);
  40. },
  41. registerAsyn: function (b, a) {
  42. this.register(b, a, !0);
  43. },
  44. hasNativeMethod: function (b, a) {
  45. return this.call("_dsb.hasNativeMethod", {
  46. name: b,
  47. type: a || "all",
  48. });
  49. },
  50. disableJavascriptDialogBlock: function (b) {
  51. this.call("_dsb.disableJavascriptDialogBlock", {
  52. disable: !1 !== b,
  53. });
  54. },
  55. };
  56. !(function () {
  57. //!function "执行"
  58. if (!window._dsf) {
  59. var b = {
  60. _dsf: {
  61. _obs: {},
  62. },
  63. _dsaf: {
  64. _obs: {},
  65. },
  66. dscb: 0,
  67. dsBridge: bridge,
  68. close: function () {
  69. bridge.call("_dsb.closePage");
  70. },
  71. _handleMessageFromNative: function (a) {
  72. var e = JSON.parse(a.data),
  73. b = {
  74. id: a.callbackId,
  75. complete: !0,
  76. },
  77. c = this._dsf[a.method],
  78. d = this._dsaf[a.method],
  79. h = function (a, c) {
  80. b.data = a.apply(c, e);
  81. bridge.call("_dsb.returnValue", b);
  82. },
  83. k = function (a, c) {
  84. e.push(function (a, c) {
  85. b.data = a;
  86. b.complete = !1 !== c;
  87. bridge.call("_dsb.returnValue", b);
  88. });
  89. a.apply(c, e);
  90. };
  91. if (c) h(c, this._dsf);
  92. else if (d) k(d, this._dsaf);
  93. else if (((c = a.method.split(".")), !(2 > c.length))) {
  94. a = c.pop();
  95. var c = c.join("."),
  96. d = this._dsf._obs,
  97. d = d[c] || {},
  98. f = d[a];
  99. f && "function" == typeof f
  100. ? h(f, d)
  101. : ((d = this._dsaf._obs),
  102. (d = d[c] || {}),
  103. (f = d[a]) && "function" == typeof f && k(f, d));
  104. }
  105. },
  106. },
  107. a;
  108. for (a in b) window[a] = b[a];
  109. bridge.register("_hasJavascriptMethod", function (a, b) {
  110. b = a.split(".");
  111. if (2 > b.length) return !(!_dsf[b] && !_dsaf[b]);
  112. a = b.pop();
  113. b = b.join(".");
  114. return (b = _dsf._obs[b] || _dsaf._obs[b]) && !!b[a];
  115. });
  116. }
  117. })();
  118. } else if (curApp == IOS) {
  119. var jsBridge = function (bridgeName, bridgeScheme) {
  120. this.bridgeName = bridgeName || "WebViewJavascriptBridge";
  121. this.bridgeScheme = bridgeScheme || "https";
  122. var ua = navigator.userAgent || navigator.vendor || window.opera;
  123. var ANDROID = 1;
  124. var IOS = 2;
  125. var SUCCESS_CODE = 0;
  126. ua = ua.toLowerCase();
  127. if (/android/.test(ua)) {
  128. this.app = ANDROID;
  129. } else if (/(iphone|ipad|ipod|ios)/.test(ua)) {
  130. this.app = IOS;
  131. }
  132. /* 获取 bridge */
  133. this.getBridge = function (callback) {
  134. if (window[this.bridgeName]) {
  135. callback(window[this.bridgeName]);
  136. return;
  137. }
  138. if (this.app === ANDROID) {
  139. document.addEventListener(
  140. "WebViewJavascriptBridgeReady",
  141. function () {
  142. callback(window[this.bridgeName]);
  143. },
  144. false
  145. );
  146. } else if (this.app === IOS) {
  147. // WebViewJavascriptBridge 由native在注入
  148. // https://github.com/marcuswestin/WebViewJavascriptBridge
  149. if (window.WVJBCallbacks) {
  150. return window.WVJBCallbacks.push(callback);
  151. }
  152. window.WVJBCallbacks = [callback];
  153. var WVJBIframe = document.createElement("iframe");
  154. WVJBIframe.style.display = "none";
  155. WVJBIframe.src = "https://__bridge_loaded__";
  156. document.documentElement.appendChild(WVJBIframe);
  157. setTimeout(function () {
  158. document.documentElement.removeChild(WVJBIframe);
  159. }, 0);
  160. }
  161. };
  162. /**
  163. * 给ios和android注册事件监听
  164. *
  165. * @memberof jsBridge
  166. */
  167. this.on = function (event, jsHandler) {
  168. this.registHandler(event, jsHandler);
  169. };
  170. /**
  171. * 默认监听事件
  172. *
  173. * @memberof jsBridge
  174. */
  175. this.registDefaultHandlerForApp = function () {
  176. this.on("reload", function () {
  177. location.reload();
  178. });
  179. };
  180. /**
  181. * 注册事件逻辑
  182. *
  183. * @memberof jsBridge
  184. */
  185. this.registHandler = function (event, jsHandler) {
  186. this.getBridge(function (bridge) {
  187. if (
  188. bridge &&
  189. bridge.registerHandler &&
  190. typeof bridge.registerHandler === "function"
  191. ) {
  192. bridge.registerHandler(event, function (data2js, responseCallback) {
  193. responseCallback = responseCallback || function () {};
  194. jsHandler(data2js, responseCallback);
  195. });
  196. }
  197. });
  198. };
  199. /**
  200. * 调用 app
  201. * @param {string} event
  202. * @param {object} params
  203. * @param {fun} callback
  204. * @return JSON对象
  205. */
  206. this.callHandler = function (event, params, callback) {
  207. this.getBridge(function (bridge) {
  208. if (
  209. bridge &&
  210. bridge.registerHandler &&
  211. typeof bridge.registerHandler === "function"
  212. ) {
  213. bridge.callHandler(event, params, callback);
  214. }
  215. });
  216. };
  217. this.registDefaultHandlerForApp();
  218. return this;
  219. };
  220. }
  221. if (!jsBridge) {
  222. jsBridge = function () {};
  223. }
  224. jsBridge.prototype.curApp = curApp;
  225. /**
  226. * 扫码
  227. */
  228. jsBridge.prototype.scanQRCode = function (params, success) {
  229. if (this.curApp == ANDROID) {
  230. dsBridge.call("scanQRCode", params, success);
  231. } else if (this.curApp == IOS) {
  232. this.callHandler("scanQRCode", params, success);
  233. }
  234. };
  235. /**
  236. * 获取BTY地址
  237. */
  238. jsBridge.prototype.getCurrentBTYAddress = function (params, success) {
  239. if (this.curApp == ANDROID) {
  240. dsBridge.call("getCurrentBTYAddress", params, success);
  241. } else if (this.curApp == IOS) {
  242. this.callHandler("getCurrentBTYAddress", params, success);
  243. }
  244. };
  245. /**
  246. * 获取所有币种的地址:getAddress
  247. * 入参:cointype为BTC,ETH,TRX,BTY等主链
  248. */
  249. jsBridge.prototype.getAddress = function (params, success) {
  250. if (this.curApp == ANDROID) {
  251. dsBridge.call("getAddress", params, success);
  252. } else if (this.curApp == IOS) {
  253. this.callHandler("getAddress", params, success);
  254. }
  255. };
  256. /**
  257. * 设置标题
  258. */
  259. jsBridge.prototype.setTitle = function (params, success) {
  260. if (this.curApp == ANDROID) {
  261. dsBridge.call("setTitle", params, success);
  262. } else if (this.curApp == IOS) {
  263. this.callHandler("setTitle", params, success);
  264. }
  265. };
  266. /**
  267. * 获取当前语言
  268. */
  269. jsBridge.prototype.getLang = function (params, success) {
  270. if (this.curApp == ANDROID) {
  271. dsBridge.call("getLang", params, success);
  272. } else if (this.curApp == IOS) {
  273. this.callHandler("getLang", params, success);
  274. }
  275. };
  276. /**
  277. * 返回上一个网页
  278. */
  279. jsBridge.prototype.closeCurrentWebview = function (params, success) {
  280. if (this.curApp == ANDROID) {
  281. dsBridge.call("closeCurrentWebview", params, success);
  282. } else if (this.curApp == IOS) {
  283. this.callHandler("closeCurrentWebview", params, success);
  284. }
  285. };
  286. /**
  287. * 获取设备id:getDeviceId
  288. */
  289. jsBridge.prototype.getDeviceId = function (params, success) {
  290. if (this.curApp == ANDROID) {
  291. dsBridge.call("getDeviceId", params, success);
  292. } else if (this.curApp == IOS) {
  293. this.callHandler("getDeviceId", params, success);
  294. }
  295. };
  296. /**
  297. * 跳转钱包 gotoWallet
  298. */
  299. jsBridge.prototype.gotoWallet = function (params, success) {
  300. if (this.curApp == ANDROID) {
  301. dsBridge.call("gotoWallet", params, success);
  302. } else if (this.curApp == IOS) {
  303. this.callHandler("gotoWallet", params, success);
  304. }
  305. };
  306. /**
  307. * 微信分享
  308. */
  309. jsBridge.prototype.wechatShare = function (params, success) {
  310. if (this.curApp == ANDROID) {
  311. dsBridge.call("wechatShare", params, success);
  312. } else if (this.curApp == IOS) {
  313. this.callHandler("wechatShare", params, success);
  314. }
  315. };
  316. /**
  317. * 构造数据:create
  318. */
  319. jsBridge.prototype.create = function (params, success) {
  320. if (this.curApp == ANDROID) {
  321. dsBridge.call("create", params, success);
  322. } else if (this.curApp == IOS) {
  323. this.callHandler("create", params, success);
  324. }
  325. };
  326. /**
  327. * 普通签名:sign
  328. */
  329. jsBridge.prototype.sign = function (params, success) {
  330. if (this.curApp == ANDROID) {
  331. dsBridge.call("sign", params, success);
  332. } else if (this.curApp == IOS) {
  333. this.callHandler("sign", params, success);
  334. }
  335. };
  336. /**
  337. * 交易组签名:signTxGroup
  338. * 入参:交易组只支持BTY
  339. */
  340. jsBridge.prototype.signTxGroup = function (params, success) {
  341. if (this.curApp == ANDROID) {
  342. dsBridge.call("signTxGroup", params, success);
  343. } else if (this.curApp == IOS) {
  344. this.callHandler("signTxGroup", params, success);
  345. }
  346. };
  347. /**
  348. * 发送交易:send
  349. * cointype为BTC,ETH,TRX,BTY等主链
  350. */
  351. jsBridge.prototype.send = function (params, success) {
  352. if (this.curApp == ANDROID) {
  353. dsBridge.call("send", params, success);
  354. } else if (this.curApp == IOS) {
  355. this.callHandler("send", params, success);
  356. }
  357. };
  358. //=====================================================================================
  359. //==================================增加新的应用方法====================================
  360. //=====================================================================================
  361. if (/wallet/.test(ua)) {
  362. console.log("在钱包环境");
  363. } else {
  364. console.log("不是在钱包环境");
  365. }
  366. //export default new jsBridge();
  367. var jsBridge = new jsBridge();