enhance-system-info.js 4.7 KB
Newer Older
D
DCloud_LXH 已提交
1
import { getLocale } from 'uni-core/runtime/locale'
D
DCloud_LXH 已提交
2 3 4

const UUID_KEY = '__DC_STAT_UUID'
let deviceId
D
DCloud_LXH 已提交
5
export function useDeviceId (result) {
D
DCloud_LXH 已提交
6 7 8 9 10 11 12 13 14 15 16
  deviceId = deviceId || __GLOBAL__.getStorageSync(UUID_KEY)
  if (!deviceId) {
    deviceId = Date.now() + '' + Math.floor(Math.random() * 1e7)
    __GLOBAL__.setStorage({
      key: UUID_KEY,
      data: deviceId
    })
  }
  result.deviceId = deviceId
}

D
DCloud_LXH 已提交
17
export function addSafeAreaInsets (result) {
D
DCloud_LXH 已提交
18 19 20 21 22 23 24 25 26 27 28
  if (result.safeArea) {
    const safeArea = result.safeArea
    result.safeAreaInsets = {
      top: safeArea.top,
      left: safeArea.left,
      right: result.windowWidth - safeArea.right,
      bottom: result.screenHeight - safeArea.bottom
    }
  }
}

D
DCloud_LXH 已提交
29
export function populateParameters (result) {
30
  const {
D
DCloud_LXH 已提交
31 32
    brand = '', model = '', system = '',
    language = '', theme, version,
D
DCloud_LXH 已提交
33 34
    platform, fontSizeSetting,
    SDKVersion, pixelRatio, deviceOrientation
35
  } = result
D
DCloud_LXH 已提交
36
  // const isQuickApp = __PLATFORM__.indexOf('quickapp-webview') !== -1
D
DCloud_LXH 已提交
37 38 39 40 41

  // osName osVersion
  let osName = ''
  let osVersion = ''
  if (__PLATFORM__ === 'mp-alipay') {
42
    osName = platform
D
DCloud_LXH 已提交
43 44 45 46 47 48 49 50
    osVersion = system
  } else {
    osName = system.split(' ')[0] || ''
    osVersion = system.split(' ')[1] || ''
  }
  let hostVersion = version
  // host 枚举值 https://smartprogram.baidu.com/docs/develop/api/device_sys/hostlist/
  if (__PLATFORM__ === 'mp-baidu') {
51 52 53 54
    hostVersion = result.swanNativeVersion
  }
  if (__PLATFORM__ === 'mp-jd') {
    hostVersion = result.hostVersionName
D
DCloud_LXH 已提交
55 56 57
  }

  // deviceType
D
DCloud_LXH 已提交
58
  const deviceType = getGetDeviceType(result, model)
D
DCloud_LXH 已提交
59 60

  // deviceModel
D
DCloud_LXH 已提交
61
  const deviceBrand = getDeviceBrand(brand)
D
DCloud_LXH 已提交
62 63

  // hostName
D
DCloud_LXH 已提交
64
  const _hostName = getHostName(result)
D
DCloud_LXH 已提交
65

66 67 68 69 70 71 72 73 74 75 76 77
  // deviceOrientation
  let _deviceOrientation = deviceOrientation // 仅 微信 百度 支持
  if (__PLATFORM__ === 'mp-baidu') { _deviceOrientation = result.orientation }

  // devicePixelRatio
  let _devicePixelRatio = pixelRatio
  if (__PLATFORM__ === 'mp-baidu') { _devicePixelRatio = result.devicePixelRatio }

  // SDKVersion
  let _SDKVersion = SDKVersion
  if (__PLATFORM__ === 'mp-alipay') { _SDKVersion = my.SDKVersion }

D
DCloud_LXH 已提交
78 79 80
  // hostLanguage
  const hostLanguage = language.replace(/_/g, '-')

D
DCloud_LXH 已提交
81 82 83 84 85 86 87
  // wx.getAccountInfoSync

  const parameters = {
    appId: process.env.UNI_APP_ID,
    appName: process.env.UNI_APP_NAME,
    appVersion: process.env.UNI_APP_VERSION_NAME,
    appVersionCode: process.env.UNI_APP_VERSION_CODE,
D
DCloud_LXH 已提交
88
    appLanguage: getAppLanguage(hostLanguage),
D
DCloud_LXH 已提交
89 90
    uniCompileVersion: process.env.UNI_COMPILER_VERSION,
    uniRuntimeVersion: process.env.UNI_COMPILER_VERSION,
91
    uniPlatform: process.env.UNI_SUB_PLATFORM || process.env.UNI_PLATFORM,
D
DCloud_LXH 已提交
92 93 94
    deviceBrand,
    deviceModel: model,
    deviceType,
95 96
    devicePixelRatio: _devicePixelRatio,
    deviceOrientation: _deviceOrientation,
97
    osName: osName.toLocaleLowerCase(),
D
DCloud_LXH 已提交
98
    osVersion,
D
DCloud_LXH 已提交
99
    hostTheme: theme,
D
DCloud_LXH 已提交
100
    hostVersion,
D
DCloud_LXH 已提交
101
    hostLanguage,
D
DCloud_LXH 已提交
102
    hostName: _hostName,
103 104 105 106
    hostSDKVersion: _SDKVersion,
    hostFontSizeSetting: fontSizeSetting,
    windowTop: 0,
    windowBottom: 0,
D
DCloud_LXH 已提交
107
    // TODO
108 109 110 111 112
    osLanguage: undefined,
    osTheme: undefined,
    ua: undefined,
    hostPackageName: undefined,
    browserName: undefined,
D
DCloud_LXH 已提交
113
    browserVersion: undefined
D
DCloud_LXH 已提交
114 115 116
  }

  Object.assign(result, parameters)
D
DCloud_LXH 已提交
117
}
118

D
DCloud_LXH 已提交
119
export function getGetDeviceType (result, model) {
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
  let deviceType = result.deviceType || 'phone'
  if (__PLATFORM__ !== 'mp-baidu') {
    const deviceTypeMaps = {
      ipad: 'pad',
      windows: 'pc',
      mac: 'pc'
    }
    const deviceTypeMapsKeys = Object.keys(deviceTypeMaps)
    const _model = model.toLocaleLowerCase()
    for (let index = 0; index < deviceTypeMapsKeys.length; index++) {
      const _m = deviceTypeMapsKeys[index]
      if (_model.indexOf(_m) !== -1) {
        deviceType = deviceTypeMaps[_m]
        break
      }
    }
  }
  return deviceType
}

D
DCloud_LXH 已提交
140 141 142
export function getDeviceBrand (brand) {
  let deviceBrand = brand
  if (deviceBrand) {
143 144 145
    deviceBrand = brand.toLocaleLowerCase()
  }
  return deviceBrand
D
DCloud_LXH 已提交
146
}
D
DCloud_LXH 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170

export function getAppLanguage (defaultLanguage) {
  return getLocale
    ? getLocale()
    : defaultLanguage
}

export function getHostName (result) {
  const _platform = __PLATFORM__ === 'mp-weixin' ? 'WeChat' : __PLATFORM__.split('-')[1]
  let _hostName = result.hostName || _platform // mp-jd
  if (__PLATFORM__ === 'mp-weixin') {
    if (result.environment) {
      _hostName = result.environment
    } else if (result.host && result.host.env) {
      _hostName = result.host.env
    }
  }
  if (__PLATFORM__ === 'mp-baidu' || __PLATFORM__ === 'mp-kuaishou') { _hostName = result.host }
  if (__PLATFORM__ === 'mp-qq') _hostName = result.AppPlatform
  if (__PLATFORM__ === 'mp-toutiao' || __PLATFORM__ === 'mp-lark') { _hostName = result.appName }
  if (__PLATFORM__ === 'mp-alipay') _hostName = result.app

  return _hostName
}