systemInfo.ts 3.4 KB
Newer Older
D
DCloud_LXH 已提交
1
import { defineAsyncApi, defineSyncApi, getLocale } from '@dcloudio/uni-api'
Q
qiang 已提交
2
import deviceId from '../../../helpers/uuid'
D
DCloud_LXH 已提交
3
import { extend } from '@vue/shared'
4
import { getWindowInfo } from './getWindowInfo'
Q
qiang 已提交
5

6 7
let systemInfo: any
let _initSystemInfo = true
Q
qiang 已提交
8

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

export const getDeviceInfo = defineSyncApi<typeof uni.getDeviceInfo>(
  'getDeviceInfo',
  () => {
    weexGetSystemInfoSync()
D
DCloud_LXH 已提交
19 20 21 22 23 24 25 26
    const {
      deviceBrand,
      deviceModel,
      osName,
      osVersion,
      deviceOrientation,
      deviceType,
    } = systemInfo
27 28

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

    return {
      deviceBrand: brand,
      deviceModel,
D
DCloud_LXH 已提交
34 35 36 37
      devicePixelRatio: plus.screen.scale!,
      deviceId: deviceId(),
      deviceOrientation,
      deviceType,
38 39
      brand,
      model: deviceModel,
D
DCloud_LXH 已提交
40 41
      system: `${_osName === 'ios' ? 'iOS' : 'Android'} ${osVersion}`,
      platform: _osName,
fxy060608's avatar
fxy060608 已提交
42
    }
Q
qiang 已提交
43
  }
44 45 46 47 48 49 50 51 52 53 54
)

export const getAppBaseInfo = defineSyncApi<typeof uni.getAppBaseInfo>(
  'getAppBaseInfo',
  () => {
    weexGetSystemInfoSync()
    const {
      hostPackageName,
      hostName,
      hostVersion,
      hostLanguage,
D
DCloud_LXH 已提交
55
      osLanguage,
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
      hostTheme,
      appId,
      appName,
      appVersion,
      appVersionCode,
    } = systemInfo

    return {
      SDKVersion: '',
      hostSDKVersion: '',
      enableDebug: false,
      appId,
      appName,
      appVersion,
      appVersionCode,
D
DCloud_LXH 已提交
71
      appLanguage: getLocale ? getLocale() : osLanguage,
72
      version: plus.runtime.innerVersion!,
D
DCloud_LXH 已提交
73
      language: osLanguage,
74 75 76 77 78 79 80 81 82 83
      theme: '',
      hostPackageName,
      hostName,
      hostVersion,
      hostLanguage,
      hostTheme,
      hostFontSizeSetting: undefined,
    }
  }
)
Q
qiang 已提交
84 85 86 87

export const getSystemInfoSync = defineSyncApi<typeof uni.getSystemInfoSync>(
  'getSystemInfoSync',
  () => {
88 89 90 91 92 93 94
    _initSystemInfo = true
    weexGetSystemInfoSync()
    _initSystemInfo = false
    const windowInfo = getWindowInfo()
    const deviceInfo = getDeviceInfo()
    const appBaseInfo = getAppBaseInfo()
    _initSystemInfo = true
D
DCloud_LXH 已提交
95

96
    const { osName, osLanguage, osVersion } = systemInfo
D
DCloud_LXH 已提交
97
    const _osName = osName.toLowerCase()
98 99 100 101 102
    const osLanguageSplit = osLanguage.split('-')
    const osLanguageSplitLast = osLanguageSplit[osLanguageSplit.length - 1]
    let _osLanguage = `${osLanguageSplit[0]}${
      osLanguageSplitLast ? '-' + osLanguageSplitLast : ''
    }`
Q
qiang 已提交
103

104 105 106 107 108 109
    let extraData = {
      errMsg: 'getSystemInfo:ok',
      fontSizeSetting: appBaseInfo.hostFontSizeSetting,
      uniCompileVersion: __uniConfig.compilerVersion,
      uniRuntimeVersion: __uniConfig.compilerVersion,
      osLanguage: _osLanguage,
D
DCloud_LXH 已提交
110
      osName: _osName,
Q
qiang 已提交
111
    }
112

D
DCloud_LXH 已提交
113 114
    if (_osName === 'ios') {
      ;(extraData as any).romName = _osName
115
      ;(extraData as any).romVersion = osVersion
Q
qiang 已提交
116 117
    }

118 119
    const _systemInfo: UniApp.GetSystemInfoResult = extend(
      systemInfo,
D
DCloud_LXH 已提交
120
      windowInfo,
121 122 123
      deviceInfo,
      appBaseInfo,
      extraData
D
DCloud_LXH 已提交
124
    )
125 126 127 128 129 130

    delete (_systemInfo as any).screenTop
    delete (_systemInfo as any).enableDebug
    delete (_systemInfo as any).theme

    return _systemInfo
Q
qiang 已提交
131 132 133 134 135 136 137 138 139
  }
)

export const getSystemInfo = defineAsyncApi<typeof uni.getSystemInfo>(
  'getSystemInfo',
  (_, { resolve }) => {
    return resolve(getSystemInfoSync())
  }
)