All files / angle-util/src DeviceUtil.ts

0% Statements 0/41
0% Branches 0/29
0% Functions 0/8
0% Lines 0/36

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138                                                                                                                                                                                                                                                                                   
/**
 * this util module is about to provide support for device
 * @module angle-util/deviceUtil
 */
 
// 定义type
type VersionResult = -1 | 0 | 1 | false // (-1 代表当前app版本号比比较的版本号低,0 代表当前app版本号与比较的版本号一致,1 代表当前app版本号与比较的版本号高。如果传入了非法的version号,返回值会是false)
 
// 定义shape
export interface NativeMsg {
  [propName: string]: any
}
 
export default {
  /**
   * 判断手机是否在app内部打开
   * @author youngbeen
   * @param
   * @return { boolean } 是否在app内打开
   * @example
   * deviceUtil.isApp()    // true
   */
  isApp (): boolean {
    const userAgent: string = window.navigator.userAgent.toLowerCase()
    if (userAgent.indexOf('umsopencore/1.0.0 51') > -1) {
      return true
    }
    return false
  },
  /**
   * 判断是否是微信环境
   * @author youngbeen
   * @param
   * @return { boolean } 是否在微信内打开
   * @example
   * deviceUtil.isWechat()    // true
   */
  isWechat (): boolean {
    const userAgent: string = window.navigator.userAgent.toLowerCase()
    if (userAgent.indexOf('micromessenger') === -1) {
      return false
    }
    return true
  },
  /**
   * 判断是否是IOS设备
   * @author youngbeen
   * @param
   * @return { boolean } 是否在IOS内打开
   * @example
   * deviceUtil.isIOS()    // true
   */
  isIOS (): boolean {
    const ua: string = window.navigator.userAgent
    let result: boolean = /iPad|iPhone|iPod/.test(ua) && !(<any>window).MSStream
    return result
  },
  /**
   * 判断是否是Android设备
   * @author youngbeen
   * @param
   * @return { boolean } 是否在Android内打开
   * @example
   * deviceUtil.isAndroid()    // true
   */
  isAndroid (): boolean {
    const ua: string = window.navigator.userAgent
    if (ua.indexOf('Android') > -1 || ua.indexOf('Adr') > -1) {
      return true
    } else {
      return false
    }
  },
  downloadApp (): void {
    window.location.href = 'http://info.snapp.chinaums.com/snapp/info/www/qmcs_download/index.html'
  },
  /**
   * 同ums native通信
   * @author youngbeen
   * @param { object } 传递的js结构体
   * @return
   * @example
   * deviceUtil.communicateToNative({ msg: 'test' })
   */
  communicateToNative (jsBridgeParam: NativeMsg = {}): void {
    if (this.isIOS()) {
      (<any>window).webkit.messageHandlers.jsToNativeData.postMessage({
        body: jsBridgeParam
      })
    } else if (this.isAndroid()) {
      let paramJsonStr: string = JSON.stringify(jsBridgeParam);
      (<any>window).Android.jsToNativeMethod(paramJsonStr)
    }
  },
  /**
   * 比较app版本号
   * @author youngbeen
   * @param { string } version - 要比较的目标版本号
   * @return -1,0,1,false (-1 代表当前app版本号比比较的版本号低,0 代表当前app版本号与比较的版本号一致,1 代表当前app版本号与比较的版本号高。如果传入了非法的version号,返回值会是false)
   * @example
   * deviceUtil.compareVersion('4.16.0.0')    // 1, 说明当前App版本号高于4.16.0.0
   */
  compareVersion (version: string): VersionResult {
    if (version) {
      let appVersion: RegExpMatchArray | string = navigator.userAgent.match(/feifan;([\d\.]+)/i)
      appVersion = appVersion ? appVersion[1] : '0.0.0.0'
 
      let appArray: Array<string> = appVersion.split('.')
      let tarArray: Array<string> = version.split('.')
 
      for (let i: number = 0; i < appArray.length || i < tarArray.length; i++) {
        let n1: string | number =+ appArray[i]
        let n2: string | number =+ tarArray[i]
        if (isNaN(n1)) n1 = 0
        if (isNaN(n2)) n2 = 0
        if (n1 < n2) return -1
        if (n1 > n2) return 1
      }
      return 0
    } else {
      return false
    }
  },
  /**
   * 转化H5 url为schema url
   * @author youngbeen
   * @param { string } url - 待转化的H5 url
   * @return { string } 转化好的schema url或者空字符串
   * @example
   * let h5Url = 'https://www.abc.com/'
   * let schemaUrl = deviceUtil.toSchema(h5Url)    // wandaappfeifan://app/CommonWebView?url=https%3A%2F%2Fwww.abc.com%2F
   * schemaUrl = deviceUtil.toSchema('')  // ''
   */
  toSchema (url: string): string {
    return `wandaappfeifan://app/CommonWebView?url=${encodeURIComponent(url)}`
  }
}