get-system-info.js 3.4 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import getWindowOffset from 'uni-platform/helpers/get-window-offset'
2
import safeAreaInsets from 'safe-area-insets'
fxy060608's avatar
fxy060608 已提交
3 4 5 6 7

const ua = navigator.userAgent
/**
 * 是否安卓设备
 */
fxy060608's avatar
fxy060608 已提交
8
const isAndroid = /android/i.test(ua)
fxy060608's avatar
fxy060608 已提交
9 10 11
/**
 * 是否iOS设备
 */
fxy060608's avatar
fxy060608 已提交
12
const isIOS = /iphone|ipad|ipod/i.test(ua)
fxy060608's avatar
fxy060608 已提交
13 14 15 16 17 18
/**
 * 获取系统信息-同步
 */
export function getSystemInfoSync () {
  var screen = window.screen
  var pixelRatio = window.devicePixelRatio
19 20 21 22
  // 横屏时 iOS 获取的屏幕宽高颠倒,进行纠正
  var landscape = Math.abs(window.orientation) === 90
  var screenWidth = Math[landscape ? 'max' : 'min'](screen.width, screen.height)
  var screenHeight = Math[landscape ? 'min' : 'max'](screen.height, screen.width)
23 24
  var windowWidth = Math.min(window.innerWidth, document.documentElement.clientWidth, screenWidth)
  var windowHeight = window.innerHeight
fxy060608's avatar
fxy060608 已提交
25
  var language = navigator.language
Q
qiang 已提交
26
  var statusBarHeight = safeAreaInsets.top
fxy060608's avatar
fxy060608 已提交
27 28 29 30 31 32
  var osname
  var osversion
  var model

  if (isIOS) {
    osname = 'iOS'
fxy060608's avatar
fxy060608 已提交
33
    const osversionFind = ua.match(/OS\s([\w_]+)\slike/)
fxy060608's avatar
fxy060608 已提交
34 35 36
    if (osversionFind) {
      osversion = osversionFind[1].replace(/_/g, '.')
    }
fxy060608's avatar
fxy060608 已提交
37
    const modelFind = ua.match(/\(([a-zA-Z]+);/)
fxy060608's avatar
fxy060608 已提交
38 39 40 41 42
    if (modelFind) {
      model = modelFind[1]
    }
  } else if (isAndroid) {
    osname = 'Android'
43
    // eslint-disable-next-line no-useless-escape
fxy060608's avatar
fxy060608 已提交
44
    const osversionFind = ua.match(/Android[\s/]([\w\.]+)[;\s]/)
fxy060608's avatar
fxy060608 已提交
45 46 47
    if (osversionFind) {
      osversion = osversionFind[1]
    }
fxy060608's avatar
fxy060608 已提交
48 49
    const infoFind = ua.match(/\((.+?)\)/)
    const infos = infoFind ? infoFind[1].split(';') : ua.split(' ')
50 51
    // eslint-disable-next-line no-useless-escape
    const otherInfo = [/\bAndroid\b/i, /\bLinux\b/i, /\bU\b/i, /^\s?[a-z][a-z]$/i, /^\s?[a-z][a-z]-[a-z][a-z]$/i, /\bwv\b/i, /\/[\d\.,]+$/, /^\s?[\d\.,]+$/, /\bBrowser\b/i, /\bMobile\b/i]
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    for (let i = 0; i < infos.length; i++) {
      const info = infos[i]
      if (info.indexOf('Build') > 0) {
        model = info.split('Build')[0].trim()
        break
      }
      let other
      for (let o = 0; o < otherInfo.length; o++) {
        if (otherInfo[o].test(info)) {
          other = true
          break
        }
      }
      if (!other) {
        model = info.trim()
        break
      }
    }
fxy060608's avatar
fxy060608 已提交
70 71 72 73 74 75 76
  } else {
    osname = 'Other'
    osversion = '0'
  }

  var system = `${osname} ${osversion}`
  var platform = osname.toLocaleLowerCase()
77 78 79 80 81 82 83 84
  var safeArea = {
    left: safeAreaInsets.left,
    right: windowWidth - safeAreaInsets.right,
    top: safeAreaInsets.top,
    bottom: windowHeight - safeAreaInsets.bottom,
    width: windowWidth - safeAreaInsets.left - safeAreaInsets.right,
    height: windowHeight - safeAreaInsets.top - safeAreaInsets.bottom
  }
fxy060608's avatar
fxy060608 已提交
85 86 87 88

  const {
    top: windowTop,
    bottom: windowBottom
Q
qiang 已提交
89
  } = getWindowOffset()
fxy060608's avatar
fxy060608 已提交
90 91 92 93

  windowHeight -= windowTop
  windowHeight -= windowBottom

94 95
  return {
    windowTop,
fxy060608's avatar
fxy060608 已提交
96 97
    windowBottom,
    windowWidth,
98
    windowHeight,
fxy060608's avatar
fxy060608 已提交
99 100 101 102
    pixelRatio,
    screenWidth,
    screenHeight,
    language,
103
    statusBarHeight,
fxy060608's avatar
fxy060608 已提交
104 105
    system,
    platform,
106
    model,
Q
qiang 已提交
107 108 109 110 111 112 113
    safeArea,
    safeAreaInsets: {
      top: safeAreaInsets.top,
      right: safeAreaInsets.right,
      bottom: safeAreaInsets.bottom,
      left: safeAreaInsets.left
    }
fxy060608's avatar
fxy060608 已提交
114 115 116 117 118 119 120
  }
}
/**
 * 获取系统信息-异步
 */
export function getSystemInfo () {
  return getSystemInfoSync()
121
}