get-system-info.js 3.6 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)
fxy060608's avatar
fxy060608 已提交
14 15 16 17 18 19
/**
 * 获取系统信息-同步
 */
export function getSystemInfoSync () {
  var screen = window.screen
  var pixelRatio = window.devicePixelRatio
20
  // 横屏时 iOS 获取的屏幕宽高颠倒,进行纠正
21 22 23 24
  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 已提交
25
  var windowWidth = Math.min(window.innerWidth, document.documentElement.clientWidth, screenWidth) || screenWidth
26
  var windowHeight = window.innerHeight
fxy060608's avatar
fxy060608 已提交
27
  var language = navigator.language
Q
qiang 已提交
28
  var statusBarHeight = safeAreaInsets.top
fxy060608's avatar
fxy060608 已提交
29 30 31 32 33 34
  var osname
  var osversion
  var model

  if (isIOS) {
    osname = 'iOS'
fxy060608's avatar
fxy060608 已提交
35
    const osversionFind = ua.match(/OS\s([\w_]+)\slike/)
fxy060608's avatar
fxy060608 已提交
36 37 38
    if (osversionFind) {
      osversion = osversionFind[1].replace(/_/g, '.')
    }
fxy060608's avatar
fxy060608 已提交
39
    const modelFind = ua.match(/\(([a-zA-Z]+);/)
fxy060608's avatar
fxy060608 已提交
40 41 42 43 44
    if (modelFind) {
      model = modelFind[1]
    }
  } else if (isAndroid) {
    osname = 'Android'
45
    // eslint-disable-next-line no-useless-escape
fxy060608's avatar
fxy060608 已提交
46
    const osversionFind = ua.match(/Android[\s/]([\w\.]+)[;\s]/)
fxy060608's avatar
fxy060608 已提交
47 48 49
    if (osversionFind) {
      osversion = osversionFind[1]
    }
fxy060608's avatar
fxy060608 已提交
50 51
    const infoFind = ua.match(/\((.+?)\)/)
    const infos = infoFind ? infoFind[1].split(';') : ua.split(' ')
52 53
    // 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]
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
    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 已提交
72 73 74 75 76 77 78
  } else {
    osname = 'Other'
    osversion = '0'
  }

  var system = `${osname} ${osversion}`
  var platform = osname.toLocaleLowerCase()
79 80 81 82 83 84 85 86
  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 已提交
87 88 89 90

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

  windowHeight -= windowTop
  windowHeight -= windowBottom

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