bridge.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. const factory = function() {
  2. var COOKIE_APP_KEY = 'x_app';
  3. function setupWebViewJavascriptBridge(callback) {
  4. if (window.WebViewJavascriptBridge) {
  5. return callback(window.WebViewJavascriptBridge)
  6. }
  7. if (window.WVJBCallbacks) {
  8. return window.WVJBCallbacks.push(callback)
  9. }
  10. window.WVJBCallbacks = [callback]
  11. var WVJBIframe = document.createElement('iframe')
  12. WVJBIframe.style.display = 'none'
  13. WVJBIframe.src = 'https://__bridge_loaded__'
  14. document.documentElement.appendChild(WVJBIframe)
  15. setTimeout(function() {
  16. document.documentElement.removeChild(WVJBIframe)
  17. }, 0)
  18. }
  19. var compareVersions = (function () {
  20. var semver = /^v?(?:\d+)(\.(?:[x*]|\d+)(\.(?:[x*]|\d+)(\.(?:[x*]|\d+))?(?:-[\da-z\-]+(?:\.[\da-z\-]+)*)?(?:\+[\da-z\-]+(?:\.[\da-z\-]+)*)?)?)?$/i;
  21. function indexOrEnd(str, q) {
  22. return str.indexOf(q) === -1 ? str.length : str.indexOf(q);
  23. }
  24. function split(v) {
  25. var c = v.replace(/^v/, '').replace(/\+.*$/, '');
  26. var patchIndex = indexOrEnd(c, '-');
  27. var arr = c.substring(0, patchIndex).split('.');
  28. arr.push(c.substring(patchIndex + 1));
  29. return arr;
  30. }
  31. function tryParse(v) {
  32. return isNaN(Number(v)) ? v : Number(v);
  33. }
  34. function validate(version) {
  35. if (typeof version !== 'string') {
  36. throw new TypeError('Invalid argument expected string');
  37. }
  38. if (!semver.test(version)) {
  39. throw new Error('Invalid argument not valid semver (\'' + version + '\' received)');
  40. }
  41. }
  42. function compareVersions(v1, v2) {
  43. [v1, v2].forEach(validate);
  44. var s1 = split(v1);
  45. var s2 = split(v2);
  46. for (var i = 0; i < Math.max(s1.length - 1, s2.length - 1); i++) {
  47. var n1 = parseInt(s1[i] || 0, 10);
  48. var n2 = parseInt(s2[i] || 0, 10);
  49. if (n1 > n2) return 1;
  50. if (n2 > n1) return -1;
  51. }
  52. var sp1 = s1[s1.length - 1];
  53. var sp2 = s2[s2.length - 1];
  54. if (sp1 && sp2) {
  55. var p1 = sp1.split('.').map(tryParse);
  56. var p2 = sp2.split('.').map(tryParse);
  57. for (i = 0; i < Math.max(p1.length, p2.length); i++) {
  58. if (p1[i] === undefined || typeof p2[i] === 'string' && typeof p1[i] === 'number') return -1;
  59. if (p2[i] === undefined || typeof p1[i] === 'string' && typeof p2[i] === 'number') return 1;
  60. if (p1[i] > p2[i]) return 1;
  61. if (p2[i] > p1[i]) return -1;
  62. }
  63. } else if (sp1 || sp2) {
  64. return sp1 ? -1 : 1;
  65. }
  66. return 0;
  67. };
  68. var operatorResMap = {
  69. 'gt': [1],
  70. 'gte': [0, 1],
  71. 'eq': [0],
  72. 'lte': [-1, 0],
  73. 'lt': [-1]
  74. }
  75. compareVersions.compare = function(operator, v1, v2) {
  76. var res = compareVersions(v1, v2);
  77. return operatorResMap[operator].indexOf(res) > -1;
  78. }
  79. return compareVersions;
  80. })()
  81. function getCookie(key) {
  82. return decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(key).replace(/[-.+*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null;
  83. }
  84. function getAppInfo() {
  85. var val = getCookie(COOKIE_APP_KEY)
  86. var matched = val && /^(ios|android)\ ((?:\d\.?)+)$/.exec(val)
  87. if (!matched) {
  88. return null
  89. }
  90. return {
  91. os: matched[1],
  92. version: matched[2]
  93. }
  94. }
  95. function compareAppVersion(operator, version) {
  96. return !!this.appInfo && compareVersions.compare(operator, this.appInfo.version, version)
  97. }
  98. function invoke(fn, data, cb) {
  99. if (this.isIos && this.compareAppVersion('gte', '4.22.0')) {
  100. setupWebViewJavascriptBridge(function (bridge) {
  101. bridge.callHandler(fn, data, cb)
  102. })
  103. return
  104. }
  105. if (!this.root) {
  106. console.warn('Bridge invoke ' + fn + ' skipped.')
  107. return null
  108. }
  109. if (this.isAndroid) {
  110. if (typeof this.root[fn] === 'function') {
  111. return data ? this.root[fn](data) : this.root[fn]()
  112. } else {
  113. return null
  114. }
  115. } else {
  116. return this.root(fn, data)
  117. }
  118. }
  119. function load(url) {
  120. window.location.href = url
  121. }
  122. function login() {
  123. if (!this.appInfo) {
  124. return
  125. }
  126. this.load('proginn://login?backToPage=true')
  127. }
  128. function back() {
  129. if (!this.appInfo) {
  130. window.history.back()
  131. } else {
  132. if (this.isIos && this.compareAppVersion('gte', '4.22.0')) {
  133. this.invoke('back')
  134. } else {
  135. this.invoke('back_page')
  136. }
  137. }
  138. }
  139. function loadUserData(data) {
  140. if (this.isAndroid) {
  141. this.invoke('user_load', data)
  142. } else if (this.compareAppVersion('lt', '4.22.0')) {
  143. this.invoke('user_load', {
  144. userInfo: data,
  145. })
  146. } else {
  147. this.invoke('loadUserData', data)
  148. }
  149. }
  150. function loadShareData(data) {
  151. if (this.isAndroid) {
  152. this.invoke('load_share_data', JSON.stringify(data))
  153. } else if (this.compareAppVersion('lt', '4.22.0')) {
  154. this.invoke('load_share_data', data)
  155. } else {
  156. this.invoke('loadShareData', data)
  157. }
  158. }
  159. function setNavigationBarColor(hex) {
  160. if (this.isAndroid) {
  161. window.appBridge.setTitleBarColor(hex);
  162. }
  163. else if (this.compareAppVersion('lt', '4.22.0')) {
  164. this.invoke('setTitleBarColor', hex);
  165. }
  166. else {
  167. this.invoke('setNavigationBarColor', hex);
  168. }
  169. }
  170. function setNavigationBarTitle(text) {
  171. this.invoke('setNavigationBarTitle', text);
  172. }
  173. function close() {
  174. if (this.isAndroid || this.compareAppVersion('lt', '4.22.0')) {
  175. this.invoke('finishActivity')
  176. } else {
  177. this.invoke('close')
  178. }
  179. }
  180. var ua = window.navigator.userAgent
  181. var appInfo = getAppInfo()
  182. var isIos = /iP(hone|ad|od)/.test(ua) || (appInfo && appInfo.os === 'ios') || false
  183. var isAndroid = /Android/.test(ua) || (appInfo && appInfo.os === 'android') || false
  184. return {
  185. get isInApp() {
  186. return !!(appInfo || window.app_event)
  187. },
  188. root: window.app_event || window.appBridge,
  189. appInfo: appInfo,
  190. isIos: isIos,
  191. isAndroid: isAndroid,
  192. compareAppVersion: compareAppVersion,
  193. invoke: invoke,
  194. load: load,
  195. login: login,
  196. back: back,
  197. close: close,
  198. loadUserData: loadUserData,
  199. loadShareData: loadShareData,
  200. setNavigationBarColor: setNavigationBarColor,
  201. setNavigationBarTitle: setNavigationBarTitle
  202. }
  203. }
  204. const bridge = process.client ? factory() : {}
  205. export default bridge