index.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. import cookies from 'js-cookie';
  2. import semver from 'semver';
  3. import { COOKIE_ROOT_DOMAIN, COOKIE_APP_KEY, COOKIE_APP_EXTRA_KEY, COOKIE_ACCESS_TOKEN_KEY, MSG_REQUIRE_LOGIN, APP_INJECT_COOKIE_KEYS, DEF_SYNCED_COOKIE_EXP } from './constant';
  4. const setupWebViewJavascriptBridge = (callback) => {
  5. if (window.WebViewJavascriptBridge) {
  6. return callback(window.WebViewJavascriptBridge);
  7. }
  8. if (window.WVJBCallbacks) {
  9. return window.WVJBCallbacks.push(callback);
  10. }
  11. window.WVJBCallbacks = [callback];
  12. var WVJBIframe = document.createElement('iframe');
  13. WVJBIframe.style.display = 'none';
  14. WVJBIframe.src = 'https://__bridge_loaded__';
  15. document.documentElement.appendChild(WVJBIframe);
  16. setTimeout(function () {
  17. document.documentElement.removeChild(WVJBIframe);
  18. }, 0);
  19. };
  20. const parseJWT = (token) => {
  21. const payloadStr = token.split('.')[1];
  22. if (!payloadStr) {
  23. return null;
  24. }
  25. try {
  26. return JSON.parse(atob(payloadStr));
  27. }
  28. catch (e) {
  29. // tslint:disable-next-line
  30. console.error('[parseJWT]', e);
  31. }
  32. return null;
  33. };
  34. const getAppInfo = () => {
  35. const val = cookies.get(COOKIE_APP_KEY);
  36. const matched = val && /^(ios|android)\ ((?:\d\.?)+)$/.exec(val);
  37. if (!matched) {
  38. return null;
  39. }
  40. return {
  41. os: matched[1],
  42. version: matched[2]
  43. };
  44. };
  45. const getAppExtra = () => {
  46. const val = cookies.get(COOKIE_APP_EXTRA_KEY);
  47. const segments = val && val.split(/\s+/);
  48. if (!segments || !segments.length) {
  49. return {};
  50. }
  51. return segments.reduce((map, item) => {
  52. const matched = /^([0-9a-zA-Z_-]+)\/(.+)$/.exec(item);
  53. if (matched) {
  54. map[matched[1]] = matched[2];
  55. }
  56. return map;
  57. }, {});
  58. };
  59. let cachedAppInfo;
  60. let cachedappExtra;
  61. class ProginnBridge {
  62. constructor(opts) {
  63. this.root = window.app_event || window.appBridge;
  64. const { notifier } = opts || {};
  65. this.notifier = notifier;
  66. }
  67. get appInfo() {
  68. cachedAppInfo = cachedAppInfo || getAppInfo();
  69. return cachedAppInfo;
  70. }
  71. get appExtra() {
  72. cachedappExtra = cachedappExtra || getAppExtra();
  73. return cachedappExtra;
  74. }
  75. get appVersion() {
  76. var _a;
  77. return (_a = this.appInfo) === null || _a === void 0 ? void 0 : _a.version;
  78. }
  79. get os() {
  80. var _a;
  81. return (_a = this.appInfo) === null || _a === void 0 ? void 0 : _a.os;
  82. }
  83. get isInApp() {
  84. return !!(this.appInfo || this.root);
  85. }
  86. get isAndroid() {
  87. return /Android/.test(window.navigator.userAgent) || this.os === 'android' || false;
  88. }
  89. get isIos() {
  90. return /iP(hone|ad|od)/.test(window.navigator.userAgent) || this.os === 'ios' || false;
  91. }
  92. get cookie() {
  93. return cookies.get();
  94. }
  95. get isLogined() {
  96. return !!cookies.get(COOKIE_ACCESS_TOKEN_KEY);
  97. }
  98. get uid() {
  99. const token = cookies.get(COOKIE_ACCESS_TOKEN_KEY);
  100. const payload = token && parseJWT(token);
  101. return payload === null || payload === void 0 ? void 0 : payload.uid;
  102. }
  103. inject(name, cb, root = 'Proginn') {
  104. window[root] = window[root] || {};
  105. window[root][name] = cb;
  106. }
  107. invoke(fn, data = null, cb = () => { }) {
  108. if (this.isIos && this.compareAppVersion('gte', '4.22.0')) {
  109. setupWebViewJavascriptBridge((bridge) => {
  110. bridge.callHandler(fn, data, cb);
  111. });
  112. return;
  113. }
  114. if (!this.root) {
  115. // tslint:disable-next-line
  116. console.warn(`Bridge invoke ${fn} skipped.`);
  117. return null;
  118. }
  119. if (this.isAndroid) {
  120. if (typeof this.root[fn] === 'function') {
  121. return data ? this.root[fn](data) : this.root[fn]();
  122. }
  123. else {
  124. return null;
  125. }
  126. }
  127. else {
  128. return this.root(fn, data);
  129. }
  130. }
  131. back() {
  132. if (!this.isInApp) {
  133. window.history.back();
  134. }
  135. else {
  136. if (this.isIos && this.compareAppVersion('gte', '4.22.0')) {
  137. this.invoke('back');
  138. }
  139. else {
  140. this.invoke('back_page');
  141. }
  142. }
  143. }
  144. load(url) {
  145. window.location.href = url;
  146. }
  147. open(url, title) {
  148. if (this.isInApp && (!this.isAndroid || this.compareAppVersion('gte', '4.20.0'))) {
  149. url = `proginn://webview?url=${encodeURIComponent(url)}${title ? '&title=' + encodeURIComponent(title) : ''}`;
  150. }
  151. this.load(url);
  152. }
  153. login() {
  154. if (!this.isInApp) {
  155. return;
  156. }
  157. this.load('proginn://login?backToPage=true');
  158. }
  159. checkLogin(force = false) {
  160. if (force || !this.isLogined) {
  161. this.notifier && this.notifier(MSG_REQUIRE_LOGIN, -99);
  162. this.login();
  163. return false;
  164. }
  165. return true;
  166. }
  167. compareAppVersion(operator, version) {
  168. return this.appVersion ? semver[operator](this.appVersion, version) : false;
  169. }
  170. syncCookies(opts) {
  171. opts = opts || {};
  172. opts.domain = opts.domain || COOKIE_ROOT_DOMAIN;
  173. opts.expires = opts.expires || DEF_SYNCED_COOKIE_EXP;
  174. for (const key of APP_INJECT_COOKIE_KEYS) {
  175. const val = cookies.get(key);
  176. if (val) {
  177. cookies.set(key, val, opts);
  178. }
  179. }
  180. }
  181. // generally to fix android webview cookies non-inject bug
  182. cacheCookiesInStorage() {
  183. for (const key of APP_INJECT_COOKIE_KEYS) {
  184. const val = cookies.get(key);
  185. if (val) {
  186. window.localStorage.setItem(key, val);
  187. }
  188. else {
  189. window.localStorage.removeItem(key);
  190. }
  191. }
  192. }
  193. loadCookiesInStorage(opts) {
  194. opts = opts || {};
  195. opts.domain = opts.domain || COOKIE_ROOT_DOMAIN;
  196. opts.expires = opts.expires || DEF_SYNCED_COOKIE_EXP;
  197. for (const key of APP_INJECT_COOKIE_KEYS) {
  198. // pass if already exists
  199. if (cookies.get(key)) {
  200. continue;
  201. }
  202. const val = window.localStorage.getItem(key);
  203. if (val) {
  204. cookies.set(key, val, opts);
  205. }
  206. }
  207. }
  208. loadUserData(data) {
  209. if (this.isAndroid) {
  210. this.invoke('user_load', data);
  211. }
  212. else if (this.compareAppVersion('lt', '4.22.0')) {
  213. this.invoke('user_load', {
  214. userInfo: data,
  215. });
  216. }
  217. else {
  218. this.invoke('loadUserData', data);
  219. }
  220. }
  221. loadTopicData(data) {
  222. if (this.isAndroid) {
  223. this.invoke('topic_load', data.topic_id);
  224. }
  225. else if (this.compareAppVersion('lt', '4.22.0')) {
  226. this.invoke('topic_load', data);
  227. }
  228. else {
  229. this.invoke('loadTopicData', data);
  230. }
  231. }
  232. }
  233. export default ProginnBridge;