system.js 2.9 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import {
2
  callApiSync
fxy060608's avatar
fxy060608 已提交
3 4
} from '../util'

5
import { getWindowInfo } from './get-window-info'
fxy060608's avatar
fxy060608 已提交
6

7
import deviceId from 'uni-platform/helpers/uuid'
fxy060608's avatar
fxy060608 已提交
8

9 10
let systemInfo = {}
let _initSystemInfo = true
11

12 13 14 15 16 17 18 19 20 21
function weexGetSystemInfoSync () {
  if (!_initSystemInfo) return
  const { getSystemInfoSync } = weex.requireModule('plus')
  systemInfo = getSystemInfoSync()
}

export function getDeviceInfo () {
  weexGetSystemInfoSync()
  const {
    deviceBrand, deviceModel, osName,
D
DCloud_LXH 已提交
22
    osVersion, deviceOrientation, deviceType
23 24 25
  } = systemInfo

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

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

export function getAppBaseInfo () {
  weexGetSystemInfoSync()
  const {
45
    hostPackageName, hostName,
46 47 48 49 50 51 52 53 54 55 56 57
    hostVersion, hostLanguage, hostTheme,
    appId, appName, appVersion, appVersionCode
  } = systemInfo

  return {
    SDKVersion: '',
    hostSDKVersion: '',
    enableDebug: false,
    appId,
    appName,
    appVersion,
    appVersionCode,
58
    appLanguage: uni.getLocale ? uni.getLocale() : hostLanguage,
59
    version: plus.runtime.innerVersion,
60
    language: hostLanguage,
61 62 63 64 65 66 67 68 69
    theme: '',
    hostPackageName,
    hostName,
    hostVersion,
    hostLanguage,
    hostTheme,
    hostFontSizeSetting: undefined
  }
}
Q
qiang 已提交
70

Q
qiang 已提交
71
export function getSystemInfoSync () {
fxy060608's avatar
fxy060608 已提交
72 73
  return callApiSync(getSystemInfo, Object.create(null), 'getSystemInfo', 'getSystemInfoSync')
}
fxy060608's avatar
fxy060608 已提交
74

Q
qiang 已提交
75
export function getSystemInfo () {
76 77 78
  _initSystemInfo = true
  weexGetSystemInfoSync()
  _initSystemInfo = false
D
DCloud_LXH 已提交
79
  const windowInfo = getWindowInfo()
80 81 82 83
  const deviceInfo = getDeviceInfo()
  const appBaseInfo = getAppBaseInfo()
  _initSystemInfo = true

D
DCloud_LXH 已提交
84
  const { osName, osLanguage, osVersion } = systemInfo
D
DCloud_LXH 已提交
85
  const _osName = osName.toLowerCase()
86 87
  const osLanguageSplit = osLanguage.split('-')
  const osLanguageSplitLast = osLanguageSplit[osLanguageSplit.length - 1]
D
DCloud_LXH 已提交
88
  const _osLanguage = `${osLanguageSplit[0]}${osLanguageSplitLast ? '-' + osLanguageSplitLast : ''}`
89

D
DCloud_LXH 已提交
90
  const extraData = {
91 92 93 94
    errMsg: 'getSystemInfo:ok',
    fontSizeSetting: appBaseInfo.hostFontSizeSetting,
    uniCompileVersion: __uniConfig.compilerVersion,
    uniRuntimeVersion: __uniConfig.compilerVersion,
D
DCloud_LXH 已提交
95 96
    osLanguage: _osLanguage,
    osName: _osName
Q
qiang 已提交
97
  }
98

D
DCloud_LXH 已提交
99 100
  if (_osName === 'ios') {
    extraData.romName = _osName
101
    extraData.romVersion = osVersion
fxy060608's avatar
fxy060608 已提交
102
  }
103

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

  delete _systemInfo.screenTop
  delete _systemInfo.enableDebug
  delete _systemInfo.theme

  return _systemInfo
D
DCloud_LXH 已提交
118
}