get-system-info.js 2.5 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 21 22
    deviceBrand,
    deviceModel,
    brand,
    model,
    platform,
    system
  } = browserInfo
D
DCloud_LXH 已提交
23

24
  return {
D
DCloud_LXH 已提交
25
    deviceBrand,
26 27
    deviceModel,
    brand,
28
    model,
29 30 31 32 33 34 35 36 37
    system,
    platform
  }
}

export function getAppBaseInfo () {
  initBrowserInfo()
  const {
    theme,
D
DCloud_LXH 已提交
38 39
    browserName,
    browseVersion,
40 41 42 43 44 45 46 47 48 49 50 51 52
    language
  } = browserInfo

  return {
    SDKVersion: '',
    hostSDKVersion: '',
    enableDebug: false,
    hostPackageName: '',
    hostFontSizeSetting: undefined,
    language,
    hostName: browserName,
    hostVersion: browseVersion,
    hostTheme: theme,
D
DCloud_LXH 已提交
53
    hostLanguage: language,
54
    theme,
D
DCloud_LXH 已提交
55 56 57 58
    appId: __uniConfig.appId,
    appName: __uniConfig.appName,
    appVersion: __uniConfig.appVersion,
    appVersionCode: __uniConfig.appVersionCode,
59 60
    appLanguage: uni.getLocale(),
    version: __uniConfig.appVersion
fxy060608's avatar
fxy060608 已提交
61 62
  }
}
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

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

  const { ua, deviceType, browserName, browseVersion, osname, osversion, deviceOrientation } = browserInfo
  const { pixelRatio } = windowInfo

  const systemInfo = Object.assign(
    {},
    windowInfo,
    deviceInfo,
    appBaseInfo,
    {
      ua,
      deviceType,
      browserName,
      browseVersion,
      deviceId: deviceId(),
      devicePixelRatio: pixelRatio,
      deviceOrientation,
      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 已提交
109 110 111 112 113
/**
 * 获取系统信息-异步
 */
export function getSystemInfo () {
  return getSystemInfoSync()
114
}