Browse Source

feat: add support for iOS 4.22.0+

Acathur 4 years ago
parent
commit
fcbb8cf00e
7 changed files with 137 additions and 34 deletions
  1. 8 0
      .prettierrc
  2. 3 4
      dist/bridge/index.d.ts
  3. 53 14
      dist/bridge/index.js
  4. 8 0
      dist/types/global.d.ts
  5. 1 1
      package.json
  6. 55 15
      src/bridge/index.ts
  7. 9 0
      src/types/global.ts

+ 8 - 0
.prettierrc

@@ -0,0 +1,8 @@
+{
+  "trailingComma": "es5",
+  "tabWidth": 2,
+  "useTabs": false,
+  "semi": false,
+  "singleQuote": true,
+  "arrowParens": "avoid"
+}

+ 3 - 4
dist/bridge/index.d.ts

@@ -18,7 +18,7 @@ declare class ProginnBridge {
     get isLogined(): boolean;
     get uid(): string | null;
     inject(name: string, cb: (...args: any) => void, root?: string): void;
-    invoke(fn: string, payload?: any): any;
+    invoke(fn: string, data?: any, cb?: Function): any;
     back(): void;
     load(url: string): void;
     open(url: string, title?: string): void;
@@ -28,13 +28,12 @@ declare class ProginnBridge {
     syncCookies(opts?: SyncCookiesOptions): void;
     cacheCookiesInStorage(): void;
     loadCookiesInStorage(opts?: SyncCookiesOptions): void;
-    userLoad(userInfo: any): void;
-    topicLoad(id: string, data: {
+    loadUserData(data: any): void;
+    loadTopicData(data: {
         topic_id: string;
         user_id: string;
         share_content: any;
         topics: any[];
     }): void;
-    setNavigationBarTitle(title: string): void;
 }
 export default ProginnBridge;

+ 53 - 14
dist/bridge/index.js

@@ -1,6 +1,22 @@
 import cookies from 'js-cookie';
 import semver from 'semver';
 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';
+const setupWebViewJavascriptBridge = (callback) => {
+    if (window.WebViewJavascriptBridge) {
+        return callback(window.WebViewJavascriptBridge);
+    }
+    if (window.WVJBCallbacks) {
+        return window.WVJBCallbacks.push(callback);
+    }
+    window.WVJBCallbacks = [callback];
+    var WVJBIframe = document.createElement('iframe');
+    WVJBIframe.style.display = 'none';
+    WVJBIframe.src = 'https://__bridge_loaded__';
+    document.documentElement.appendChild(WVJBIframe);
+    setTimeout(function () {
+        document.documentElement.removeChild(WVJBIframe);
+    }, 0);
+};
 const parseJWT = (token) => {
     const payloadStr = token.split('.')[1];
     if (!payloadStr) {
@@ -44,7 +60,6 @@ let cachedAppInfo;
 let cachedappExtra;
 class ProginnBridge {
     constructor(opts) {
-        // @ts-ignore
         this.root = window.app_event || window.appBridge;
         const { notifier } = opts || {};
         this.notifier = notifier;
@@ -89,7 +104,13 @@ class ProginnBridge {
         window[root] = window[root] || {};
         window[root][name] = cb;
     }
-    invoke(fn, payload) {
+    invoke(fn, data = null, cb = () => { }) {
+        if (this.isIos && this.compareAppVersion('gte', '4.22.0')) {
+            setupWebViewJavascriptBridge((bridge) => {
+                bridge.callHandler(fn, data, cb);
+            });
+            return;
+        }
         if (!this.root) {
             // tslint:disable-next-line
             console.warn(`Bridge invoke ${fn} skipped.`);
@@ -97,14 +118,14 @@ class ProginnBridge {
         }
         if (this.isAndroid) {
             if (typeof this.root[fn] === 'function') {
-                return payload ? this.root[fn](payload) : this.root[fn]();
+                return data ? this.root[fn](data) : this.root[fn]();
             }
             else {
                 return null;
             }
         }
         else {
-            return this.root(fn, payload);
+            return this.root(fn, data);
         }
     }
     back() {
@@ -112,7 +133,12 @@ class ProginnBridge {
             window.history.back();
         }
         else {
-            this.invoke('back_page');
+            if (this.isIos && this.compareAppVersion('gte', '4.22.0')) {
+                this.invoke('back');
+            }
+            else {
+                this.invoke('back_page');
+            }
         }
     }
     load(url) {
@@ -179,16 +205,29 @@ class ProginnBridge {
             }
         }
     }
-    userLoad(userInfo) {
-        this.invoke('user_load', this.isAndroid ? userInfo : {
-            userInfo
-        });
-    }
-    topicLoad(id, data) {
-        this.invoke('topic_load', this.isAndroid ? id : data);
+    loadUserData(data) {
+        if (this.isAndroid) {
+            this.invoke('user_load', data);
+        }
+        else if (this.compareAppVersion('lt', '4.22.0')) {
+            this.invoke('user_load', {
+                userInfo: data,
+            });
+        }
+        else {
+            this.invoke('loadUserData', data);
+        }
     }
-    setNavigationBarTitle(title) {
-        this.invoke('setNavigationBarTitle', title);
+    loadTopicData(data) {
+        if (this.isAndroid) {
+            this.invoke('topic_load', data.topic_id);
+        }
+        else if (this.compareAppVersion('lt', '4.22.0')) {
+            this.invoke('topic_load', data);
+        }
+        else {
+            this.invoke('loadTopicData', data);
+        }
     }
 }
 export default ProginnBridge;

+ 8 - 0
dist/types/global.d.ts

@@ -3,3 +3,11 @@ export declare type SyncCookiesOptions = {
     domain?: string;
     expires?: number;
 };
+declare global {
+    interface Window {
+        app_event: any;
+        appBridge: any;
+        WebViewJavascriptBridge: any;
+        WVJBCallbacks: any[];
+    }
+}

+ 1 - 1
package.json

@@ -1,6 +1,6 @@
 {
   "name": "proginn-lib",
-  "version": "0.3.2",
+  "version": "0.4.0",
   "description": "Proginn front-end common library.",
   "main": "dist/index.js",
   "module": "dist/index.js",

+ 55 - 15
src/bridge/index.ts

@@ -3,6 +3,23 @@ import semver from 'semver'
 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'
 import { Notifier, SyncCookiesOptions } from '../types/global'
 
+const setupWebViewJavascriptBridge = (callback) => {
+  if (window.WebViewJavascriptBridge) {
+    return callback(window.WebViewJavascriptBridge)
+  }
+  if (window.WVJBCallbacks) {
+    return window.WVJBCallbacks.push(callback)
+  }
+  window.WVJBCallbacks = [callback]
+  var WVJBIframe = document.createElement('iframe')
+  WVJBIframe.style.display = 'none'
+  WVJBIframe.src = 'https://__bridge_loaded__'
+  document.documentElement.appendChild(WVJBIframe)
+  setTimeout(function () {
+    document.documentElement.removeChild(WVJBIframe)
+  }, 0)
+}
+
 const parseJWT = (token: string): any => {
   const payloadStr = token.split('.')[1]
 
@@ -57,7 +74,6 @@ let cachedAppInfo: any
 let cachedappExtra: any
 
 class ProginnBridge {
-  // @ts-ignore
   root = window.app_event || window.appBridge
 
   private notifier?: Notifier
@@ -120,7 +136,14 @@ class ProginnBridge {
     window[root][name] = cb
   }
 
-  invoke(fn: string, payload?: any) {
+  invoke(fn: string, data: any = null, cb: Function = () => {}) {
+    if (this.isIos && this.compareAppVersion('gte', '4.22.0')) {
+      setupWebViewJavascriptBridge((bridge) => {
+        bridge.callHandler(fn, data, cb)
+      })
+      return
+    }
+
     if (!this.root) {
       // tslint:disable-next-line
       console.warn(`Bridge invoke ${fn} skipped.`)
@@ -129,12 +152,12 @@ class ProginnBridge {
 
     if (this.isAndroid) {
       if (typeof this.root[fn] === 'function') {
-        return payload ? this.root[fn](payload) : this.root[fn]()
+        return data ? this.root[fn](data) : this.root[fn]()
       } else {
         return null
       }
     } else {
-      return this.root(fn, payload)
+      return this.root(fn, data)
     }
   }
 
@@ -142,7 +165,12 @@ class ProginnBridge {
     if (!this.isInApp) {
       window.history.back()
     } else {
-      this.invoke('back_page')
+      if (this.isIos && this.compareAppVersion('gte', '4.22.0')) {
+        this.invoke('back')
+      }
+      else {
+        this.invoke('back_page')
+      }
     }
   }
 
@@ -226,23 +254,35 @@ class ProginnBridge {
     }
   }
 
-  userLoad(userInfo: any) {
-    this.invoke('user_load', this.isAndroid ? userInfo : {
-      userInfo
-    })
+  loadUserData(data: any) {
+    if (this.isAndroid) {
+      this.invoke('user_load', data)
+    }
+    else if (this.compareAppVersion('lt', '4.22.0')) {
+      this.invoke('user_load', {
+        userInfo: data,
+      })
+    }
+    else {
+      this.invoke('loadUserData', data)
+    }
   }
 
-  topicLoad(id: string, data: {
+  loadTopicData(data: {
     topic_id: string
     user_id: string
     share_content: any
     topics: any[]
   }) {
-    this.invoke('topic_load', this.isAndroid ? id : data)
-  }
-
-  setNavigationBarTitle(title: string) {
-    this.invoke('setNavigationBarTitle', title)
+    if (this.isAndroid) {
+      this.invoke('topic_load', data.topic_id)
+    }
+    else if (this.compareAppVersion('lt', '4.22.0')) {
+      this.invoke('topic_load', data)
+    }
+    else {
+      this.invoke('loadTopicData', data)
+    }
   }
 }
 

+ 9 - 0
src/types/global.ts

@@ -3,3 +3,12 @@ export type SyncCookiesOptions = {
   domain?: string
   expires?: number // d
 }
+
+declare global {
+  interface Window {
+    app_event: any
+    appBridge: any
+    WebViewJavascriptBridge: any
+    WVJBCallbacks: any[]
+  }
+}