system.js 2.8 KB
Newer Older
D
DCloud_LXH 已提交
1
import { callApiSync } from '../util'
2
import { getWindowInfo } from './get-window-info'
D
DCloud_LXH 已提交
3
import { sortObject } from 'uni-shared'
fxy060608's avatar
fxy060608 已提交
4

5 6
let systemInfo = {}
let _initSystemInfo = true
7

D
DCloud_LXH 已提交
8
export function weexGetSystemInfoSync () {
9 10 11
  if (!_initSystemInfo) return
  const { getSystemInfoSync } = weex.requireModule('plus')
  systemInfo = getSystemInfoSync()
12 13 14
  if (typeof systemInfo === 'string') {
    try {
      systemInfo = JSON.parse(systemInfo)
D
DCloud_LXH 已提交
15
    } catch (error) { }
16
  }
D
DCloud_LXH 已提交
17
  return systemInfo
18 19 20 21 22
}

export function getDeviceInfo () {
  weexGetSystemInfoSync()
  const {
23
    deviceBrand = '', deviceModel, osName,
24 25
    osVersion, deviceOrientation, deviceType,
    deviceId
26 27 28
  } = systemInfo

  const brand = deviceBrand.toLowerCase()
D
DCloud_LXH 已提交
29
  const _osName = osName.toLowerCase()
30 31

  return {
D
DCloud_LXH 已提交
32
    brand,
33 34
    deviceBrand: brand,
    deviceModel,
D
DCloud_LXH 已提交
35
    devicePixelRatio: plus.screen.scale,
36
    deviceId,
D
DCloud_LXH 已提交
37 38
    deviceOrientation,
    deviceType,
39
    model: deviceModel,
D
DCloud_LXH 已提交
40 41
    platform: _osName,
    system: `${_osName === 'ios' ? 'iOS' : 'Android'} ${osVersion}`
42 43 44 45 46 47
  }
}

export function getAppBaseInfo () {
  weexGetSystemInfoSync()
  const {
D
DCloud_LXH 已提交
48
    hostPackageName, hostName, osLanguage,
49
    hostVersion, hostLanguage, hostTheme,
50
    appId, appName, appVersion, appVersionCode,
51
    appWgtVersion
52 53
  } = systemInfo

D
DCloud_LXH 已提交
54 55 56 57 58 59
  const appLanguage = uni
    ? uni.getLocale
      ? uni.getLocale()
      : hostLanguage
    : hostLanguage

60 61 62 63 64
  return {
    appId,
    appName,
    appVersion,
    appVersionCode,
65
    appWgtVersion,
D
DCloud_LXH 已提交
66
    appLanguage,
D
DCloud_LXH 已提交
67 68
    enableDebug: false,
    hostSDKVersion: undefined,
69 70 71 72 73
    hostPackageName,
    hostName,
    hostVersion,
    hostLanguage,
    hostTheme,
D
DCloud_LXH 已提交
74 75 76
    hostFontSizeSetting: undefined,
    language: osLanguage,
    SDKVersion: '',
77
    theme: plus.navigator.getUIStyle(),
D
DCloud_LXH 已提交
78
    version: plus.runtime.innerVersion
79 80
  }
}
Q
qiang 已提交
81

Q
qiang 已提交
82
export function getSystemInfoSync () {
fxy060608's avatar
fxy060608 已提交
83 84
  return callApiSync(getSystemInfo, Object.create(null), 'getSystemInfo', 'getSystemInfoSync')
}
fxy060608's avatar
fxy060608 已提交
85

Q
qiang 已提交
86
export function getSystemInfo () {
87 88 89
  _initSystemInfo = true
  weexGetSystemInfoSync()
  _initSystemInfo = false
D
DCloud_LXH 已提交
90
  const windowInfo = getWindowInfo()
91 92 93 94
  const deviceInfo = getDeviceInfo()
  const appBaseInfo = getAppBaseInfo()
  _initSystemInfo = true

D
DCloud_LXH 已提交
95
  const extraData = {
96 97
    errMsg: 'getSystemInfo:ok',
    fontSizeSetting: appBaseInfo.hostFontSizeSetting,
D
DCloud_LXH 已提交
98
    osName: systemInfo.osName.toLowerCase()
Q
qiang 已提交
99
  }
100

D
DCloud_LXH 已提交
101 102
  if (systemInfo.hostName) {
    extraData.hostSDKVersion = systemInfo.uniRuntimeVersion
fxy060608's avatar
fxy060608 已提交
103
  }
104

105 106 107
  const _systemInfo = Object.assign(
    {},
    systemInfo,
D
DCloud_LXH 已提交
108
    windowInfo,
109 110 111 112 113 114 115
    deviceInfo,
    appBaseInfo,
    extraData
  )

  delete _systemInfo.screenTop
  delete _systemInfo.enableDebug
D
DCloud_LXH 已提交
116 117 118
  if (!__uniConfig.darkmode) {
    delete _systemInfo.theme
  }
119

D
DCloud_LXH 已提交
120
  return sortObject(_systemInfo)
D
DCloud_LXH 已提交
121
}