system.js 3.0 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 {
D
DCloud_LXH 已提交
45
    hostPackageName, hostName, osLanguage,
46 47 48 49
    hostVersion, hostLanguage, hostTheme,
    appId, appName, appVersion, appVersionCode
  } = systemInfo

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

56 57 58 59 60 61 62 63
  return {
    SDKVersion: '',
    hostSDKVersion: '',
    enableDebug: false,
    appId,
    appName,
    appVersion,
    appVersionCode,
D
DCloud_LXH 已提交
64
    appLanguage,
65
    version: plus.runtime.innerVersion,
D
DCloud_LXH 已提交
66
    language: osLanguage,
67 68 69 70 71 72 73 74 75
    theme: '',
    hostPackageName,
    hostName,
    hostVersion,
    hostLanguage,
    hostTheme,
    hostFontSizeSetting: undefined
  }
}
Q
qiang 已提交
76

Q
qiang 已提交
77
export function getSystemInfoSync () {
fxy060608's avatar
fxy060608 已提交
78 79
  return callApiSync(getSystemInfo, Object.create(null), 'getSystemInfo', 'getSystemInfoSync')
}
fxy060608's avatar
fxy060608 已提交
80

Q
qiang 已提交
81
export function getSystemInfo () {
82 83 84
  _initSystemInfo = true
  weexGetSystemInfoSync()
  _initSystemInfo = false
D
DCloud_LXH 已提交
85
  const windowInfo = getWindowInfo()
86 87 88 89
  const deviceInfo = getDeviceInfo()
  const appBaseInfo = getAppBaseInfo()
  _initSystemInfo = true

D
DCloud_LXH 已提交
90
  const { osName, osLanguage, osVersion } = systemInfo
D
DCloud_LXH 已提交
91
  const _osName = osName.toLowerCase()
92 93
  const osLanguageSplit = osLanguage.split('-')
  const osLanguageSplitLast = osLanguageSplit[osLanguageSplit.length - 1]
D
DCloud_LXH 已提交
94
  const _osLanguage = `${osLanguageSplit[0]}${osLanguageSplitLast ? '-' + osLanguageSplitLast : ''}`
95

D
DCloud_LXH 已提交
96
  const extraData = {
97 98 99 100
    errMsg: 'getSystemInfo:ok',
    fontSizeSetting: appBaseInfo.hostFontSizeSetting,
    uniCompileVersion: __uniConfig.compilerVersion,
    uniRuntimeVersion: __uniConfig.compilerVersion,
D
DCloud_LXH 已提交
101 102
    osLanguage: _osLanguage,
    osName: _osName
Q
qiang 已提交
103
  }
104

D
DCloud_LXH 已提交
105 106
  if (_osName === 'ios') {
    extraData.romName = _osName
107
    extraData.romVersion = osVersion
fxy060608's avatar
fxy060608 已提交
108
  }
109

110 111 112
  const _systemInfo = Object.assign(
    {},
    systemInfo,
D
DCloud_LXH 已提交
113
    windowInfo,
114 115 116 117 118 119 120 121 122 123
    deviceInfo,
    appBaseInfo,
    extraData
  )

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

  return _systemInfo
D
DCloud_LXH 已提交
124
}