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

const ua = navigator.userAgent
/**
 * 是否安卓设备
 */
fxy060608's avatar
fxy060608 已提交
7
const isAndroid = /android/i.test(ua)
fxy060608's avatar
fxy060608 已提交
8 9 10
/**
 * 是否iOS设备
 */
fxy060608's avatar
fxy060608 已提交
11
const isIOS = /iphone|ipad|ipod/i.test(ua)
fxy060608's avatar
fxy060608 已提交
12 13 14 15 16 17 18 19 20 21 22
/**
 * 获取系统信息-同步
 */
export function getSystemInfoSync () {
  var windowWidth = window.innerWidth
  var windowHeight = window.innerHeight
  var screen = window.screen
  var pixelRatio = window.devicePixelRatio
  var screenWidth = screen.width
  var screenHeight = screen.height
  var language = navigator.language
23
  var statusBarHeight = 0
fxy060608's avatar
fxy060608 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
  var osname
  var osversion
  var model

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

  var system = `${osname} ${osversion}`
  var platform = osname.toLocaleLowerCase()

  const {
    top: windowTop,
    bottom: windowBottom
  } = getWindowOffset()

  windowHeight -= windowTop
  windowHeight -= windowBottom

83 84
  return {
    windowTop,
fxy060608's avatar
fxy060608 已提交
85 86
    windowBottom,
    windowWidth,
87
    windowHeight,
fxy060608's avatar
fxy060608 已提交
88 89 90 91
    pixelRatio,
    screenWidth,
    screenHeight,
    language,
92
    statusBarHeight,
fxy060608's avatar
fxy060608 已提交
93 94 95 96 97 98 99 100 101 102
    system,
    platform,
    model
  }
}
/**
 * 获取系统信息-异步
 */
export function getSystemInfo () {
  return getSystemInfoSync()
103
}