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'
D
DCloud_LXH 已提交
4
import { sortObject } from 'uni-shared'
5

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

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

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

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

export function getAppBaseInfo () {
  initBrowserInfo()
D
DCloud_LXH 已提交
43
  const { theme, language } = browserInfo
44

D
DCloud_LXH 已提交
45 46 47 48 49 50
  const appLanguage = uni
    ? uni.getLocale
      ? uni.getLocale()
      : language
    : language

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

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

D
DCloud_LXH 已提交
84
  const { ua, browserName, browserVersion, osname, osversion } = browserInfo
85 86 87 88 89 90 91 92

  const systemInfo = Object.assign(
    {},
    windowInfo,
    deviceInfo,
    appBaseInfo,
    {
      browserName,
D
DCloud_LXH 已提交
93
      browserVersion,
94 95 96 97
      fontSizeSetting: appBaseInfo.hostFontSizeSetting,
      osName: osname.toLocaleLowerCase(),
      osVersion: osversion,
      osLanguage: undefined,
D
DCloud_LXH 已提交
98 99 100 101 102
      osTheme: undefined,
      uniPlatform: 'web',
      uniCompileVersion: __uniConfig.compilerVersion,
      uniRuntimeVersion: __uniConfig.compilerVersion,
      ua
103 104 105 106 107 108 109
    }
  )

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

D
DCloud_LXH 已提交
110
  return sortObject(systemInfo)
111
}
fxy060608's avatar
fxy060608 已提交
112 113 114 115 116
/**
 * 获取系统信息-异步
 */
export function getSystemInfo () {
  return getSystemInfoSync()
117
}