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

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

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

export const getDeviceInfo = defineSyncApi<typeof uni.getDeviceInfo>(
  'getDeviceInfo',
  () => {
    weexGetSystemInfoSync()
D
DCloud_LXH 已提交
25
    const {
26
      deviceBrand = '',
D
DCloud_LXH 已提交
27 28 29 30 31
      deviceModel,
      osName,
      osVersion,
      deviceOrientation,
      deviceType,
32
      deviceId,
D
DCloud_LXH 已提交
33
    } = systemInfo
34 35

    const brand = deviceBrand.toLowerCase()
D
DCloud_LXH 已提交
36
    const _osName = osName.toLowerCase()
37 38

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

export const getAppBaseInfo = defineSyncApi<typeof uni.getAppBaseInfo>(
  'getAppBaseInfo',
  () => {
    weexGetSystemInfoSync()
    const {
      hostPackageName,
      hostName,
      hostVersion,
      hostLanguage,
D
DCloud_LXH 已提交
62
      osLanguage,
D
DCloud_LXH 已提交
63
      osTheme,
64 65 66 67 68
      hostTheme,
      appId,
      appName,
      appVersion,
      appVersionCode,
69
      appWgtVersion,
70 71 72 73 74 75 76
    } = systemInfo

    return {
      appId,
      appName,
      appVersion,
      appVersionCode,
77
      appWgtVersion,
D
DCloud_LXH 已提交
78
      appLanguage: getLocale ? getLocale() : osLanguage,
D
DCloud_LXH 已提交
79
      enableDebug: false,
80 81 82 83 84 85
      hostPackageName,
      hostName,
      hostVersion,
      hostLanguage,
      hostTheme,
      hostFontSizeSetting: undefined,
D
DCloud_LXH 已提交
86 87 88
      hostSDKVersion: undefined,
      language: osLanguage,
      SDKVersion: '',
D
DCloud_LXH 已提交
89
      theme: hostTheme || osTheme,
D
DCloud_LXH 已提交
90
      version: plus.runtime.innerVersion!,
91 92 93
    }
  }
)
Q
qiang 已提交
94 95 96 97

export const getSystemInfoSync = defineSyncApi<typeof uni.getSystemInfoSync>(
  'getSystemInfoSync',
  () => {
98 99 100 101 102 103 104
    _initSystemInfo = true
    weexGetSystemInfoSync()
    _initSystemInfo = false
    const windowInfo = getWindowInfo()
    const deviceInfo = getDeviceInfo()
    const appBaseInfo = getAppBaseInfo()
    _initSystemInfo = true
D
DCloud_LXH 已提交
105

D
DCloud_LXH 已提交
106
    const extraData = {
107
      fontSizeSetting: appBaseInfo.hostFontSizeSetting,
D
DCloud_LXH 已提交
108
      osName: systemInfo.osName.toLowerCase(),
Q
qiang 已提交
109
    }
110

D
DCloud_LXH 已提交
111 112
    if (systemInfo.hostName) {
      ;(extraData as any).hostSDKVersion = systemInfo.uniRuntimeVersion
Q
qiang 已提交
113 114
    }

115 116
    const _systemInfo: UniApp.GetSystemInfoResult = extend(
      systemInfo,
D
DCloud_LXH 已提交
117
      windowInfo,
118 119 120
      deviceInfo,
      appBaseInfo,
      extraData
D
DCloud_LXH 已提交
121
    )
122 123 124

    delete (_systemInfo as any).screenTop
    delete (_systemInfo as any).enableDebug
D
DCloud_LXH 已提交
125 126 127
    if (!__uniConfig.darkmode) {
      delete (_systemInfo as any).theme
    }
128

D
DCloud_LXH 已提交
129
    return sortObject(_systemInfo)
Q
qiang 已提交
130 131 132 133 134 135 136 137 138
  }
)

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