get-system-info.js 7.9 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'
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

function IEVersion () {
  const userAgent = navigator.userAgent
  const isIE = userAgent.indexOf('compatible') > -1 && userAgent.indexOf('MSIE') > -1
  const isEdge = userAgent.indexOf('Edge') > -1 && !isIE
  const isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf('rv:11.0') > -1
  if (isIE) {
    const reIE = new RegExp('MSIE (\\d+\\.\\d+);')
    reIE.test(userAgent)
    const fIEVersion = parseFloat(RegExp.$1)
    if (fIEVersion > 6) {
      return fIEVersion
    } else {
      return 6
    }
  } else if (isEdge) {
    return -1
  } else if (isIE11) {
    return 11
  } else {
    return -1
  }
}

function getDeviceBrand (model) {
  if (/iphone/gi.test(model) || /ipad/gi.test(model) || /mac/gi.test(model)) { return 'apple' }
  if (/windows/gi.test(model)) { return 'microsoft' }
}
fxy060608's avatar
fxy060608 已提交
32 33 34 35 36

const ua = navigator.userAgent
/**
 * 是否安卓设备
 */
fxy060608's avatar
fxy060608 已提交
37
const isAndroid = /android/i.test(ua)
fxy060608's avatar
fxy060608 已提交
38 39 40
/**
 * 是否iOS设备
 */
fxy060608's avatar
fxy060608 已提交
41
const isIOS = /iphone|ipad|ipod/i.test(ua)
42 43 44 45 46 47 48 49 50 51 52 53
/**
 * 是否是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)
54 55 56 57
/**
 * 是否是iPadOS
 */
const isIPadOS = isMac && navigator.maxTouchPoints > 0
fxy060608's avatar
fxy060608 已提交
58 59 60 61 62 63
/**
 * 获取系统信息-同步
 */
export function getSystemInfoSync () {
  var screen = window.screen
  var pixelRatio = window.devicePixelRatio
64
  // 横屏时 iOS 获取的屏幕宽高颠倒,进行纠正
65 66 67 68
  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 已提交
69
  var windowWidth = Math.min(window.innerWidth, document.documentElement.clientWidth, screenWidth) || screenWidth
70
  var windowHeight = window.innerHeight
fxy060608's avatar
fxy060608 已提交
71
  var language = navigator.language
Q
qiang 已提交
72
  var statusBarHeight = safeAreaInsets.top
fxy060608's avatar
fxy060608 已提交
73 74 75
  var osname
  var osversion
  var model
D
DCloud_LXH 已提交
76
  let deviceType = 'phone'
fxy060608's avatar
fxy060608 已提交
77 78 79

  if (isIOS) {
    osname = 'iOS'
fxy060608's avatar
fxy060608 已提交
80
    const osversionFind = ua.match(/OS\s([\w_]+)\slike/)
fxy060608's avatar
fxy060608 已提交
81 82 83
    if (osversionFind) {
      osversion = osversionFind[1].replace(/_/g, '.')
    }
fxy060608's avatar
fxy060608 已提交
84
    const modelFind = ua.match(/\(([a-zA-Z]+);/)
fxy060608's avatar
fxy060608 已提交
85 86 87 88 89
    if (modelFind) {
      model = modelFind[1]
    }
  } else if (isAndroid) {
    osname = 'Android'
90
    // eslint-disable-next-line no-useless-escape
fxy060608's avatar
fxy060608 已提交
91
    const osversionFind = ua.match(/Android[\s/]([\w\.]+)[;\s]/)
fxy060608's avatar
fxy060608 已提交
92 93 94
    if (osversionFind) {
      osversion = osversionFind[1]
    }
fxy060608's avatar
fxy060608 已提交
95 96
    const infoFind = ua.match(/\((.+?)\)/)
    const infos = infoFind ? infoFind[1].split(';') : ua.split(' ')
97 98
    // 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]
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    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
      }
    }
117 118
  } else if (isIPadOS) {
    model = 'iPad'
119 120
    osname = 'iOS'
    osversion = typeof window.BigInt === 'function' ? '14.0' : '13.0'
D
DCloud_LXH 已提交
121
    deviceType = 'pad'
122 123
  } else if (isWindows || isMac || isLinux) {
    model = 'PC'
D
DCloud_LXH 已提交
124 125
    osname = 'PC'
    deviceType = 'pc'
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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
    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 已提交
179 180 181
  } else {
    osname = 'Other'
    osversion = '0'
D
DCloud_LXH 已提交
182
    deviceType = 'other'
fxy060608's avatar
fxy060608 已提交
183 184 185 186
  }

  var system = `${osname} ${osversion}`
  var platform = osname.toLocaleLowerCase()
187 188 189 190 191 192 193 194
  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 已提交
195 196 197 198

  const {
    top: windowTop,
    bottom: windowBottom
Q
qiang 已提交
199
  } = getWindowOffset()
fxy060608's avatar
fxy060608 已提交
200 201 202 203

  windowHeight -= windowTop
  windowHeight -= windowBottom

D
DCloud_LXH 已提交
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
  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]
  }

228 229
  return {
    windowTop,
fxy060608's avatar
fxy060608 已提交
230 231
    windowBottom,
    windowWidth,
232
    windowHeight,
fxy060608's avatar
fxy060608 已提交
233 234 235 236
    pixelRatio,
    screenWidth,
    screenHeight,
    language,
237
    statusBarHeight,
fxy060608's avatar
fxy060608 已提交
238 239
    system,
    platform,
D
DCloud_LXH 已提交
240 241
    deviceBrand,
    deviceType,
242
    model,
D
DCloud_LXH 已提交
243
    deviceModel: model,
Q
qiang 已提交
244 245 246 247 248 249
    safeArea,
    safeAreaInsets: {
      top: safeAreaInsets.top,
      right: safeAreaInsets.right,
      bottom: safeAreaInsets.bottom,
      left: safeAreaInsets.left
250
    },
D
DCloud_LXH 已提交
251 252 253 254 255 256 257
    deviceId: deviceId(),
    SDKVersion: '',
    ua,
    uniPlatform: 'web',
    browserName,
    browseVersion,
    osLanguage: language,
258
    osName: osname.toLocaleLowerCase(),
D
DCloud_LXH 已提交
259 260 261 262 263 264 265 266 267
    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,
D
DCloud_LXH 已提交
268 269
    hostName: browserName,
    hostVersion: browseVersion,
D
DCloud_LXH 已提交
270
    osTheme: '',
D
DCloud_LXH 已提交
271 272
    hostTheme: '',
    hostPackageName: ''
fxy060608's avatar
fxy060608 已提交
273 274 275 276 277 278 279
  }
}
/**
 * 获取系统信息-异步
 */
export function getSystemInfo () {
  return getSystemInfoSync()
280
}