get-system-info.js 2.6 KB
Newer Older
1
import { getWindowInfo } from './get-window-info'
Q
qiang 已提交
2
import deviceId from 'uni-platform/helpers/uuid'
3
import { getBrowserInfo } from '../base/get-browser-info'
4

5 6
let browserInfo = {}
let _initBrowserInfo = true
7

8 9 10
function initBrowserInfo () {
  if (!_initBrowserInfo) return
  browserInfo = getBrowserInfo()
11
}
fxy060608's avatar
fxy060608 已提交
12

13 14
export function getDeviceInfo () {
  initBrowserInfo()
fxy060608's avatar
fxy060608 已提交
15
  const {
16 17 18 19 20
    deviceBrand,
    deviceModel,
    brand,
    model,
    platform,
D
DCloud_LXH 已提交
21 22 23
    system,
    deviceOrientation,
    deviceType
24
  } = browserInfo
D
DCloud_LXH 已提交
25

26
  return {
D
DCloud_LXH 已提交
27
    deviceBrand,
28
    deviceModel,
D
DCloud_LXH 已提交
29 30 31 32
    devicePixelRatio: window.devicePixelRatio,
    deviceId: deviceId(),
    deviceOrientation,
    deviceType,
33
    brand,
34
    model,
35 36 37 38 39 40 41 42 43
    system,
    platform
  }
}

export function getAppBaseInfo () {
  initBrowserInfo()
  const {
    theme,
D
DCloud_LXH 已提交
44 45
    browserName,
    browseVersion,
46 47 48
    language
  } = browserInfo

D
DCloud_LXH 已提交
49 50 51 52 53 54
  const appLanguage = uni
    ? uni.getLocale
      ? uni.getLocale()
      : language
    : language

55 56 57 58 59 60 61 62 63 64
  return {
    SDKVersion: '',
    hostSDKVersion: '',
    enableDebug: false,
    hostPackageName: '',
    hostFontSizeSetting: undefined,
    language,
    hostName: browserName,
    hostVersion: browseVersion,
    hostTheme: theme,
D
DCloud_LXH 已提交
65
    hostLanguage: language,
66
    theme,
D
DCloud_LXH 已提交
67 68 69 70
    appId: __uniConfig.appId,
    appName: __uniConfig.appName,
    appVersion: __uniConfig.appVersion,
    appVersionCode: __uniConfig.appVersionCode,
D
DCloud_LXH 已提交
71
    appLanguage,
72
    version: __uniConfig.appVersion
fxy060608's avatar
fxy060608 已提交
73 74
  }
}
75 76 77 78 79 80 81 82 83 84 85 86 87

/**
 * 获取系统信息-同步
 */
export function getSystemInfoSync () {
  _initBrowserInfo = true
  initBrowserInfo()
  _initBrowserInfo = false
  const windowInfo = getWindowInfo()
  const deviceInfo = getDeviceInfo()
  const appBaseInfo = getAppBaseInfo()
  _initBrowserInfo = true

D
DCloud_LXH 已提交
88
  const { ua, browserName, browseVersion, osname, osversion } = browserInfo
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115

  const systemInfo = Object.assign(
    {},
    windowInfo,
    deviceInfo,
    appBaseInfo,
    {
      ua,
      browserName,
      browseVersion,
      uniPlatform: 'web',
      uniCompileVersion: __uniConfig.compilerVersion,
      uniRuntimeVersion: __uniConfig.compilerVersion,
      fontSizeSetting: appBaseInfo.hostFontSizeSetting,
      osName: osname.toLocaleLowerCase(),
      osVersion: osversion,
      osLanguage: undefined,
      osTheme: undefined
    }
  )

  delete systemInfo.screenTop
  delete systemInfo.enableDebug
  delete systemInfo.theme

  return systemInfo
}
fxy060608's avatar
fxy060608 已提交
116 117 118 119 120
/**
 * 获取系统信息-异步
 */
export function getSystemInfo () {
  return getSystemInfoSync()
121
}