bridgeToAppFun.js 2.57 KB
Newer Older
leiqingsong's avatar
leiqingsong committed
1 2
// H5 去调用App的方法
import jsBridge from "./bridge";
leiqingsong's avatar
leiqingsong committed
3

leiqingsong's avatar
leiqingsong committed
4
class bridgeToAppFun {
leiqingsong's avatar
leiqingsong committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
  constructor() {
    this.userAgent = null;
    console.log(navigator.userAgent);
    if (
      navigator.userAgent.indexOf("Android") > -1 ||
      navigator.userAgent.indexOf("Adr") > -1
    ) {
      this.userAgent = "android";
    } else if (navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)) {
      this.userAgent = "ios";
    } else {
      this.userAgent = "web";
    }
  }
  // 获取Token
  getAuthToken() {
    if (this.userAgent === "android") {
      // android
      let token = null;
      try {
        token = window.android.getAuthToken();
        localStorage.setItem("token", token);
      } catch {
        console.log("token获取失败");
      }
      return new Promise((resolve, reject) => {
        if (token) {
          resolve(token);
        } else {
          reject("失败");
        }
      });
    } else if (this.userAgent === "ios") {
      // ios
      return new Promise((resolve, reject) => {
        jsBridge.callhandler("getAuthToken", null, data => {
          localStorage.setItem("token", data);
          if (data) {
            resolve(data);
          } else {
            console.log("调用失败");
            reject("getAuthToken", data);
          }
        });
      });
    }
  }
  // 退出
  logoutToApp() {
    console.log("退出登录");
    if (this.userAgent === "android") {
      console.log("an");
      window.android.logout();
    } else {
leiqingsong's avatar
leiqingsong committed
59
      console.log('ios');
leiqingsong's avatar
leiqingsong committed
60 61 62 63 64 65 66
      jsBridge.callhandler("logout");
    }
  }
  navigateBack() {
    console.log("返回");
    if (this.userAgent === "android") {
      console.log("an");
leiqingsong's avatar
leiqingsong committed
67 68 69 70
      try {
        const referrer = document.referrer;
        if (referrer !== '') {
          console.log('reffer不为空', referrer);
leiqingsong's avatar
leiqingsong committed
71
          // location.href = referrer;
leiqingsong's avatar
leiqingsong committed
72 73 74 75 76 77 78
        } else {
          console.log('调用navigateBack');
          window.android.navigateBack();
        }
      } catch {
        console.log('返回调用失败,都不行');
      }
leiqingsong's avatar
leiqingsong committed
79 80 81 82 83 84 85 86 87 88 89 90 91
    } else {
      jsBridge.callhandler("navigateBack");
    }
  }
  showBottomBar(params) {
    console.log("隐藏底部");
    if (this.userAgent === "android") {
      console.log("an");
      window.android.showBottomBar(params);
    } else {
      jsBridge.callhandler("showBottomBar", params);
    }
  }
leiqingsong's avatar
leiqingsong committed
92 93 94 95 96 97 98 99 100
  redirectToLogin() {
    console.log("去登陆");
    if (this.userAgent === "android") {
      console.log("an");
      window.android.redirectToLogin();
    } else {
      jsBridge.callhandler("redirectToLogin");
    }
  }
leiqingsong's avatar
leiqingsong committed
101
}
leiqingsong's avatar
leiqingsong committed
102

leiqingsong's avatar
leiqingsong committed
103
export default bridgeToAppFun;