bridge.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. (function () {
  2. 'use strict';
  3. function isBridgeReady() {
  4. return window.flutter_inappwebview && typeof window.flutter_inappwebview.callHandler === 'function';
  5. }
  6. function callHandler(name, args) {
  7. if (!isBridgeReady()) return undefined;
  8. try {
  9. if (args !== undefined) {
  10. return window.flutter_inappwebview.callHandler(name, args);
  11. }
  12. return window.flutter_inappwebview.callHandler(name);
  13. } catch (e) {
  14. return undefined;
  15. }
  16. }
  17. function callHandlerSafe(name, args) {
  18. if (!isBridgeReady()) return;
  19. try {
  20. if (args !== undefined) {
  21. window.flutter_inappwebview.callHandler(name, args);
  22. } else {
  23. window.flutter_inappwebview.callHandler(name);
  24. }
  25. } catch (e) { }
  26. }
  27. async function getToken() {
  28. if (!isBridgeReady()) throw new Error('Bridge not ready');
  29. try {
  30. const token = await window.flutter_inappwebview.callHandler('getToken');
  31. if (token) return token;
  32. throw new Error('Bridge not ready');
  33. } catch (e) {
  34. throw new Error('Bridge not ready');
  35. }
  36. }
  37. async function getActivityId() {
  38. if (!isBridgeReady()) throw new Error('Bridge not ready');
  39. try {
  40. const result = await window.flutter_inappwebview.callHandler('getActivityId');
  41. return result || '';
  42. } catch (e) {
  43. return '';
  44. }
  45. }
  46. async function getLocalTime(timestamp) {
  47. // if (!isBridgeReady()) throw new Error('Bridge not ready');
  48. try {
  49. return await window.flutter_inappwebview.callHandler('getLocalTime', timestamp);
  50. } catch (e) {
  51. return timestamp || '';
  52. }
  53. }
  54. function closeWebview() {
  55. callHandlerSafe('closeWebview');
  56. }
  57. function jumpToLogin() {
  58. callHandlerSafe('jumpToLogin');
  59. }
  60. function jumpToDeposit() {
  61. callHandlerSafe('jumpToDeposit');
  62. }
  63. function showToast(message) {
  64. callHandlerSafe('showToast', message);
  65. }
  66. function showInviteView(params) {
  67. callHandlerSafe('showInviteView', params);
  68. }
  69. function goRouterName(path) {
  70. callHandlerSafe('goRouterName', { path: path });
  71. }
  72. window.BEXBridge = {
  73. isBridgeReady: isBridgeReady,
  74. callHandler: callHandler,
  75. callHandlerSafe: callHandlerSafe,
  76. getToken: getToken,
  77. getActivityId: getActivityId,
  78. getLocalTime: getLocalTime,
  79. closeWebview: closeWebview,
  80. jumpToLogin: jumpToLogin,
  81. jumpToDeposit: jumpToDeposit,
  82. showToast: showToast,
  83. showInviteView: showInviteView,
  84. goRouterName: goRouterName
  85. };
  86. })();