get-system-info.js 7.0 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'
D
DCloud_LXH 已提交
4
import { IEVersion, getDeviceBrand } from 'uni-shared'
fxy060608's avatar
fxy060608 已提交
5 6 7 8 9

const ua = navigator.userAgent
/**
 * 是否安卓设备
 */
fxy060608's avatar
fxy060608 已提交
10
const isAndroid = /android/i.test(ua)
fxy060608's avatar
fxy060608 已提交
11 12 13
/**
 * 是否iOS设备
 */
fxy060608's avatar
fxy060608 已提交
14
const isIOS = /iphone|ipad|ipod/i.test(ua)
15 16 17 18 19 20 21 22 23 24 25 26
/**
 * 是否是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)
27 28 29 30
/**
 * 是否是iPadOS
 */
const isIPadOS = isMac && navigator.maxTouchPoints > 0
fxy060608's avatar
fxy060608 已提交
31 32 33 34 35 36
/**
 * 获取系统信息-同步
 */
export function getSystemInfoSync () {
  var screen = window.screen
  var pixelRatio = window.devicePixelRatio
37
  // 横屏时 iOS 获取的屏幕宽高颠倒,进行纠正
38 39 40 41
  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 已提交
42
  var windowWidth = Math.min(window.innerWidth, document.documentElement.clientWidth, screenWidth) || screenWidth
43
  var windowHeight = window.innerHeight
fxy060608's avatar
fxy060608 已提交
44
  var language = navigator.language
Q
qiang 已提交
45
  var statusBarHeight = safeAreaInsets.top
fxy060608's avatar
fxy060608 已提交
46 47 48
  var osname
  var osversion
  var model
D
DCloud_LXH 已提交
49
  let deviceType = 'phone'
fxy060608's avatar
fxy060608 已提交
50 51 52

  if (isIOS) {
    osname = 'iOS'
fxy060608's avatar
fxy060608 已提交
53
    const osversionFind = ua.match(/OS\s([\w_]+)\slike/)
fxy060608's avatar
fxy060608 已提交
54 55 56
    if (osversionFind) {
      osversion = osversionFind[1].replace(/_/g, '.')
    }
fxy060608's avatar
fxy060608 已提交
57
    const modelFind = ua.match(/\(([a-zA-Z]+);/)
fxy060608's avatar
fxy060608 已提交
58 59 60 61 62
    if (modelFind) {
      model = modelFind[1]
    }
  } else if (isAndroid) {
    osname = 'Android'
63
    // eslint-disable-next-line no-useless-escape
fxy060608's avatar
fxy060608 已提交
64
    const osversionFind = ua.match(/Android[\s/]([\w\.]+)[;\s]/)
fxy060608's avatar
fxy060608 已提交
65 66 67
    if (osversionFind) {
      osversion = osversionFind[1]
    }
fxy060608's avatar
fxy060608 已提交
68 69
    const infoFind = ua.match(/\((.+?)\)/)
    const infos = infoFind ? infoFind[1].split(';') : ua.split(' ')
70 71
    // 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]
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    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
      }
    }
90 91
  } else if (isIPadOS) {
    model = 'iPad'
92 93
    osname = 'iOS'
    osversion = typeof window.BigInt === 'function' ? '14.0' : '13.0'
D
DCloud_LXH 已提交
94
    deviceType = 'pad'
95 96
  } else if (isWindows || isMac || isLinux) {
    model = 'PC'
D
DCloud_LXH 已提交
97 98
    osname = 'PC'
    deviceType = 'pc'
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 147 148 149 150 151
    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 已提交
152 153 154
  } else {
    osname = 'Other'
    osversion = '0'
D
DCloud_LXH 已提交
155
    deviceType = 'other'
fxy060608's avatar
fxy060608 已提交
156 157 158 159
  }

  var system = `${osname} ${osversion}`
  var platform = osname.toLocaleLowerCase()
160 161 162 163 164 165 166 167
  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 已提交
168 169 170 171

  const {
    top: windowTop,
    bottom: windowBottom
Q
qiang 已提交
172
  } = getWindowOffset()
fxy060608's avatar
fxy060608 已提交
173 174 175 176

  windowHeight -= windowTop
  windowHeight -= windowBottom

D
DCloud_LXH 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
  let browserName
  let browseVersion = String(IEVersion())
  if (browseVersion !== '-1') { browserName = 'IE' } else {
    const browseVendors = ['Version', 'Firefox', 'Chrome', 'Edge{0,1}']
    const vendors = ['Safari', 'Firefox', 'Chrome', 'Edge']
    for (let index = 0; index < browseVendors.length; index++) {
      const vendor = browseVendors[index]
      const reg = new RegExp(`(${vendor})/(\\S*)\\b`)
      if (reg.test(ua)) {
        browserName = vendors[index]
        browseVersion = ua.match(reg)[2]
      }
    }
  }

  // deviceBrand
  let deviceBrand = ''
  if (model) {
    const _model = model.toLocaleLowerCase()
    deviceBrand = getDeviceBrand(_model) ||
      getDeviceBrand(osname.toLocaleLowerCase()) ||
      _model.split(' ')[0]
  }

201 202
  return {
    windowTop,
fxy060608's avatar
fxy060608 已提交
203 204
    windowBottom,
    windowWidth,
205
    windowHeight,
fxy060608's avatar
fxy060608 已提交
206 207 208 209
    pixelRatio,
    screenWidth,
    screenHeight,
    language,
210
    statusBarHeight,
fxy060608's avatar
fxy060608 已提交
211 212
    system,
    platform,
D
DCloud_LXH 已提交
213 214
    deviceBrand,
    deviceType,
215
    model,
D
DCloud_LXH 已提交
216
    deviceModel: model,
Q
qiang 已提交
217 218 219 220 221 222
    safeArea,
    safeAreaInsets: {
      top: safeAreaInsets.top,
      right: safeAreaInsets.right,
      bottom: safeAreaInsets.bottom,
      left: safeAreaInsets.left
223
    },
D
DCloud_LXH 已提交
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
    deviceId: deviceId(),
    SDKVersion: '',
    ua,
    uniPlatform: 'web',
    browserName,
    browseVersion,
    osLanguage: language,
    osName: osname,
    osVersion: osversion,
    hostLanguage: language,
    version: __uniConfig.appVersion,
    uniCompileVersion: __uniConfig.compilerVersion,
    uniRuntimeVersion: __uniConfig.compilerVersion,
    appId: __uniConfig.appId,
    appName: __uniConfig.appName,
    appVersion: __uniConfig.appVersion,
    appVersionCode: __uniConfig.appVersionCode,
    osTheme: '',
    hostTheme: ''
fxy060608's avatar
fxy060608 已提交
243 244 245 246 247 248 249
  }
}
/**
 * 获取系统信息-异步
 */
export function getSystemInfo () {
  return getSystemInfoSync()
250
}