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

const ua = navigator.userAgent
/**
 * 是否安卓设备
 */
fxy060608's avatar
fxy060608 已提交
9
const isAndroid = /android/i.test(ua)
fxy060608's avatar
fxy060608 已提交
10 11 12
/**
 * 是否iOS设备
 */
fxy060608's avatar
fxy060608 已提交
13
const isIOS = /iphone|ipad|ipod/i.test(ua)
14 15 16 17 18 19 20 21 22 23 24 25
/**
 * 是否是Windows设备
 */
const isWindows = ua.match(/Windows NT ([\d|\d.\d]*)/i)
/**
 * 是否是Mac设备
 */
const isMac = /Macintosh|Mac/i.test(ua)
/**
 * 是否是Linux设备
 */
const isLinux = /Linux|X11/i.test(ua)
26 27 28 29
/**
 * 是否是iPadOS
 */
const isIPadOS = isMac && navigator.maxTouchPoints > 0
fxy060608's avatar
fxy060608 已提交
30 31 32 33 34 35
/**
 * 获取系统信息-同步
 */
export function getSystemInfoSync () {
  var screen = window.screen
  var pixelRatio = window.devicePixelRatio
36
  // 横屏时 iOS 获取的屏幕宽高颠倒,进行纠正
37 38 39 40
  const screenFix = /^Apple/.test(navigator.vendor) && typeof window.orientation === 'number'
  const landscape = screenFix && Math.abs(window.orientation) === 90
  var screenWidth = screenFix ? Math[landscape ? 'max' : 'min'](screen.width, screen.height) : screen.width
  var screenHeight = screenFix ? Math[landscape ? 'min' : 'max'](screen.height, screen.width) : screen.height
d-u-a's avatar
d-u-a 已提交
41
  var windowWidth = Math.min(window.innerWidth, document.documentElement.clientWidth, screenWidth) || screenWidth
42
  var windowHeight = window.innerHeight
fxy060608's avatar
fxy060608 已提交
43
  var language = navigator.language
Q
qiang 已提交
44
  var statusBarHeight = safeAreaInsets.top
fxy060608's avatar
fxy060608 已提交
45 46 47 48 49 50
  var osname
  var osversion
  var model

  if (isIOS) {
    osname = 'iOS'
fxy060608's avatar
fxy060608 已提交
51
    const osversionFind = ua.match(/OS\s([\w_]+)\slike/)
fxy060608's avatar
fxy060608 已提交
52 53 54
    if (osversionFind) {
      osversion = osversionFind[1].replace(/_/g, '.')
    }
fxy060608's avatar
fxy060608 已提交
55
    const modelFind = ua.match(/\(([a-zA-Z]+);/)
fxy060608's avatar
fxy060608 已提交
56 57 58 59 60
    if (modelFind) {
      model = modelFind[1]
    }
  } else if (isAndroid) {
    osname = 'Android'
61
    // eslint-disable-next-line no-useless-escape
fxy060608's avatar
fxy060608 已提交
62
    const osversionFind = ua.match(/Android[\s/]([\w\.]+)[;\s]/)
fxy060608's avatar
fxy060608 已提交
63 64 65
    if (osversionFind) {
      osversion = osversionFind[1]
    }
fxy060608's avatar
fxy060608 已提交
66 67
    const infoFind = ua.match(/\((.+?)\)/)
    const infos = infoFind ? infoFind[1].split(';') : ua.split(' ')
68 69
    // 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]
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    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
      }
    }
88 89
  } else if (isIPadOS) {
    model = 'iPad'
90 91
    osname = 'iOS'
    osversion = typeof window.BigInt === 'function' ? '14.0' : '13.0'
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 139 140 141 142 143 144 145 146
  } else if (isWindows || isMac || isLinux) {
    model = 'PC'
    const osversionFind = ua.match(/\((.+?)\)/)[1]

    if (isWindows) {
      osname = 'Windows'
      osversion = ''
      switch (isWindows[1]) {
        case '5.1':
          osversion = 'XP'
          break
        case '6.0':
          osversion = 'Vista'
          break
        case '6.1':
          osversion = '7'
          break
        case '6.2':
          osversion = '8'
          break
        case '6.3':
          osversion = '8.1'
          break
        case '10.0':
          osversion = '10'
          break
      }

      const framework = osversionFind.match(/[Win|WOW]([\d]+)/)
      if (framework) {
        osversion += ` x${framework[1]}`
      }
    } else if (isMac) {
      osname = 'Mac'
      osversion = osversionFind.match(/Mac OS X (.+)/) || ''

      if (osversion) {
        osversion = osversion[1].replace(/_/g, '.')
        // '10_15_7' or '10.16; rv:86.0'
        if (osversion.indexOf(';') !== -1) {
          osversion = osversion.split(';')[0]
        }
      }
    } else if (isLinux) {
      osname = 'Linux'
      osversion = osversionFind.match(/Linux (.*)/) || ''

      if (osversion) {
        osversion = osversion[1]
        // 'x86_64' or 'x86_64; rv:79.0'
        if (osversion.indexOf(';') !== -1) {
          osversion = osversion.split(';')[0]
        }
      }
    }
fxy060608's avatar
fxy060608 已提交
147 148 149 150 151 152 153
  } else {
    osname = 'Other'
    osversion = '0'
  }

  var system = `${osname} ${osversion}`
  var platform = osname.toLocaleLowerCase()
154 155 156 157 158 159 160 161
  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 已提交
162 163 164 165

  const {
    top: windowTop,
    bottom: windowBottom
Q
qiang 已提交
166
  } = getWindowOffset()
fxy060608's avatar
fxy060608 已提交
167 168 169 170

  windowHeight -= windowTop
  windowHeight -= windowBottom

171 172
  return {
    windowTop,
fxy060608's avatar
fxy060608 已提交
173 174
    windowBottom,
    windowWidth,
175
    windowHeight,
fxy060608's avatar
fxy060608 已提交
176 177 178 179
    pixelRatio,
    screenWidth,
    screenHeight,
    language,
180
    statusBarHeight,
fxy060608's avatar
fxy060608 已提交
181 182
    system,
    platform,
183
    model,
Q
qiang 已提交
184 185 186 187 188 189
    safeArea,
    safeAreaInsets: {
      top: safeAreaInsets.top,
      right: safeAreaInsets.right,
      bottom: safeAreaInsets.bottom,
      left: safeAreaInsets.left
190
    },
Q
qiang 已提交
191
    deviceId: deviceId()
fxy060608's avatar
fxy060608 已提交
192 193 194 195 196 197 198
  }
}
/**
 * 获取系统信息-异步
 */
export function getSystemInfo () {
  return getSystemInfoSync()
199
}