systemInfo.ts 3.1 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

9 10 11 12
function weexGetSystemInfoSync() {
  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) {}
  }
18 19 20 21 22 23
}

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

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

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

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

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

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

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

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

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

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

D
DCloud_LXH 已提交
125
    return sortObject(_systemInfo)
Q
qiang 已提交
126 127 128 129 130 131 132 133 134
  }
)

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