wxjs-new.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. var WeixinJSBridge = (function(global) {
  2. if (typeof global.WeixinJSBridge !== 'undefined') {
  3. return global.WeixinJSBridge
  4. }
  5. var isWebView = global.hasOwnProperty('document');
  6. var isIosWebView = false;
  7. var invokeCallbacks = {};
  8. var invokeCallbackId = 0;
  9. var onCallbacks = {};
  10. if (isWebView) {
  11. var userAgent = global.navigator.userAgent;
  12. var isAndroidWebView = userAgent.indexOf('Android') != -1;
  13. isIosWebView = !isAndroidWebView;
  14. }
  15. var _WeixinJSCore = global.WeixinJSCore
  16. delete global.WeixinJSCore
  17. var _invokeHandler = function(event, paramsString, callbackId) {
  18. var result = _WeixinJSCore.invokeHandler(event, paramsString, callbackId);
  19. if (typeof result !== 'undefined' && typeof invokeCallbacks[callbackId] === 'function' && result !== '') {
  20. try {
  21. result = JSON.parse(result)
  22. } catch (e) {
  23. console.error('invokeHandler result parse failed ' + result)
  24. result = {}
  25. }
  26. invokeCallbacks[callbackId](result)
  27. delete invokeCallbacks[callbackId]
  28. }
  29. }
  30. var invoke = function(event, params, callback) {
  31. var paramsString = JSON.stringify(params || {});
  32. var callbackId = ++invokeCallbackId
  33. invokeCallbacks[callbackId] = callback;
  34. _invokeHandler(event, paramsString, callbackId);
  35. }
  36. var _invokeCallbackHandler = function(callbackId, result) {
  37. var callback = invokeCallbacks[callbackId];
  38. if (typeof callback == 'function') {
  39. callback(result);
  40. }
  41. delete invokeCallbacks[callbackId];
  42. }
  43. var on = function(event, callback) {
  44. onCallbacks[event] = callback;
  45. }
  46. var _subscribeHandler = function(event, result) {
  47. var callback = onCallbacks[event];
  48. if (typeof callback == 'function') {
  49. callback(result);
  50. }
  51. }
  52. var vsprintf = function (fmt, argv) {
  53. argv.unshift(fmt);
  54. return sprintf.apply(null, argv);
  55. };
  56. var _session_data = {}
  57. function log(fmt) {
  58. var curFuncIdentifier = global.WeixinJSBridge.log;
  59. if (curFuncIdentifier !== _logIdentifier) {
  60. return;
  61. }
  62. var argv = [];
  63. for (var i = 0; i < arguments.length; i++) {
  64. argv.push(arguments[i]);
  65. };
  66. var fm = argv.shift();
  67. var msg;
  68. try {
  69. msg = vsprintf(fm, argv);
  70. } catch (e) {
  71. msg = fmt;
  72. }
  73. invoke('log', {
  74. 'msg': msg
  75. });
  76. }
  77. var _logIdentifier = log;
  78. function env(key) {
  79. var curFuncIdentifier = global.WeixinJSBridge.env;
  80. if (curFuncIdentifier !== _envIdentifier) {
  81. return '';
  82. }
  83. return _session_data[key];
  84. }
  85. var _envIdentifier = env;
  86. // the first event
  87. on('sys:init', function (ses) {
  88. // 避免由于Java层多次发起init请求,造成网页端多次收到WeixinJSBridgeReady事件
  89. if (global.WeixinJSBridge._hasInit) {
  90. log('hasInit, no need to init again');
  91. return;
  92. }
  93. global.WeixinJSBridge._hasInit = true;
  94. // bridge ready
  95. var readyEvent;
  96. try {
  97. readyEvent = new Event('WeixinJSBridgeReady');
  98. } catch (e) {
  99. readyEvent = document.createEvent('Event');
  100. readyEvent.initEvent('WeixinJSBridgeReady');
  101. }
  102. document.dispatchEvent(readyEvent);
  103. });
  104. return {
  105. invoke: invoke,
  106. call: invoke,
  107. on: on,
  108. log: log,
  109. env: env,
  110. _hasInit: false,
  111. // java 2 js
  112. subscribeHandler: _subscribeHandler,
  113. invokeCallbackHandler: _invokeCallbackHandler
  114. }
  115. }) (this);