判断iOS系统;判断iPhoneX及以后版本;判断微信环境;判断网络

这里写自定义目录标题

  • 判断IOS系统
  • 判断iPhoneX及以后版本
  • 判断微信环境
  • 判断网络

判断IOS系统

function getIsIOS(){
    const UA = navigator.userAgent.toLowerCase();
    const isAndroid = /android|adr/gi.test(UA);
    const isIos = /iphone|ipod|ipad/gi.test(UA) && !isAndroid;
    return isIos
}

判断iPhoneX及以后版本

function isIphoneX() {
    // X XS, XS Max, XR
    const xSeriesConfig = [
      {
        devicePixelRatio: 3,
        width: 375,
        height: 812,
      },
      {
        devicePixelRatio: 3,
        width: 414,
        height: 896,
      },
      {
        devicePixelRatio: 2,
        width: 414,
        height: 896,
      },
    ]
    // h5
    if (typeof window !== 'undefined' && window) {
      const isIOS = /iphone/gi.test(window.navigator.userAgent)
      if (!isIOS) {
        return false
      }
      const { devicePixelRatio, screen } = window
      const { width, height } = screen
      return xSeriesConfig.some(item => item.devicePixelRatio === devicePixelRatio && item.width === width && item.height === height)
    }
    return false
  }

判断微信环境

export function getIsWeixin() {
    const ua = navigator.userAgent.toLowerCase();
    if (ua.match(/MicroMessenger/i) == "micromessenger") {
        return true;
    }
    return false;
}

判断网络

function isOnLine() {
    function onLine(callback) {
        var img = new Image();
        img.src = 'https://www.baidu.com/favicon.ico?_t=' + Date.now();
        img.onload = function () {
            if (callback) callback(true)
        };
        img.onerror = function () {
            if (callback) callback(false)
        };
    }
    onLine(function (flag) {
        if (flag) {
            // console.log('网络畅通')
        } else {
            window.LeKeBridge.sendMessage2Native("toast", "网络断开");
            // console.log('网络故障')
        }
    })
}

你可能感兴趣的:(判断iOS系统;判断iPhoneX及以后版本;判断微信环境;判断网络)