systemInfo.ts 2.9 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'
D
DCloud_LXH 已提交
5
import { sortObject } from '@dcloudio/uni-shared'
Q
qiang 已提交
6

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

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

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

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

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

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

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

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

D
DCloud_LXH 已提交
97
    const extraData = {
98
      fontSizeSetting: appBaseInfo.hostFontSizeSetting,
D
DCloud_LXH 已提交
99
      osName: systemInfo.osName.toLowerCase(),
Q
qiang 已提交
100
    }
101

D
DCloud_LXH 已提交
102 103
    if (systemInfo.hostName) {
      ;(extraData as any).hostSDKVersion = systemInfo.uniRuntimeVersion
Q
qiang 已提交
104 105
    }

106 107
    const _systemInfo: UniApp.GetSystemInfoResult = extend(
      systemInfo,
D
DCloud_LXH 已提交
108
      windowInfo,
109 110 111
      deviceInfo,
      appBaseInfo,
      extraData
D
DCloud_LXH 已提交
112
    )
113 114 115 116 117

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

D
DCloud_LXH 已提交
118
    return sortObject(_systemInfo)
Q
qiang 已提交
119 120 121 122 123 124 125 126 127
  }
)

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