index.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. close() {
  145. if (this.isAndroid || this.compareAppVersion('lt', '4.22.0')) {
  146. this.invoke('finishActivity');
  147. }
  148. else {
  149. this.invoke('close');
  150. }
  151. }
  152. load(url) {
  153. window.location.href = url;
  154. }
  155. open(url, title) {
  156. if (this.isInApp && (!this.isAndroid || this.compareAppVersion('gte', '4.20.0'))) {
  157. url = `proginn://webview?url=${encodeURIComponent(url)}${title ? '&title=' + encodeURIComponent(title) : ''}`;
  158. }
  159. this.load(url);
  160. }
  161. login() {
  162. if (!this.isInApp) {
  163. return;
  164. }
  165. this.load('proginn://login?backToPage=true');
  166. }
  167. checkLogin(force = false) {
  168. if (force || !this.isLogined) {
  169. this.notifier && this.notifier(MSG_REQUIRE_LOGIN, -99);
  170. this.login();
  171. return false;
  172. }
  173. return true;
  174. }
  175. compareAppVersion(operator, version) {
  176. return this.appVersion ? semver[operator](this.appVersion, version) : false;
  177. }
  178. syncCookies(opts) {
  179. opts = opts || {};
  180. opts.domain = opts.domain || COOKIE_ROOT_DOMAIN;
  181. opts.expires = opts.expires || DEF_SYNCED_COOKIE_EXP;
  182. for (const key of APP_INJECT_COOKIE_KEYS) {
  183. const val = cookies.get(key);
  184. if (val) {
  185. cookies.set(key, val, opts);
  186. }
  187. }
  188. }
  189. // generally to fix android webview cookies non-inject bug
  190. cacheCookiesInStorage() {
  191. for (const key of APP_INJECT_COOKIE_KEYS) {
  192. const val = cookies.get(key);
  193. if (val) {
  194. window.localStorage.setItem(key, val);
  195. }
  196. else {
  197. window.localStorage.removeItem(key);
  198. }
  199. }
  200. }
  201. loadCookiesInStorage(opts) {
  202. opts = opts || {};
  203. opts.domain = opts.domain || COOKIE_ROOT_DOMAIN;
  204. opts.expires = opts.expires || DEF_SYNCED_COOKIE_EXP;
  205. for (const key of APP_INJECT_COOKIE_KEYS) {
  206. // pass if already exists
  207. if (cookies.get(key)) {
  208. continue;
  209. }
  210. const val = window.localStorage.getItem(key);
  211. if (val) {
  212. cookies.set(key, val, opts);
  213. }
  214. }
  215. }
  216. // load data
  217. loadUserData(data) {
  218. if (this.isAndroid) {
  219. this.invoke('user_load', data);
  220. }
  221. else if (this.compareAppVersion('lt', '4.22.0')) {
  222. this.invoke('user_load', {
  223. userInfo: data,
  224. });
  225. }
  226. else {
  227. this.invoke('loadUserData', data);
  228. }
  229. }
  230. loadShareData(data) {
  231. if (this.isAndroid) {
  232. this.invoke('load_share_data', JSON.stringify(data));
  233. }
  234. else if (this.compareAppVersion('lt', '4.22.0')) {
  235. this.invoke('load_share_data', data);
  236. }
  237. else {
  238. this.invoke('loadShareData', data);
  239. }
  240. }
  241. loadTopicData(data) {
  242. if (this.isAndroid) {
  243. this.invoke('topic_load', data.topic_id);
  244. }
  245. else if (this.compareAppVersion('lt', '4.22.0')) {
  246. this.invoke('topic_load', data);
  247. }
  248. else {
  249. this.invoke('loadTopicData', data);
  250. }
  251. }
  252. // ui
  253. setNavigationBarColor(hex) {
  254. if (this.isAndroid) {
  255. window.appBridge.setTitleBarColor(hex);
  256. }
  257. else if (this.compareAppVersion('lt', '4.22.0')) {
  258. this.invoke('setTitleBarColor', hex);
  259. }
  260. else {
  261. this.invoke('setNavigationBarColor', hex);
  262. }
  263. }
  264. setNavigationBarTitle(text) {
  265. this.invoke('setNavigationBarTitle', text);
  266. }
  267. }
  268. export default ProginnBridge;