enhance-system-info.js 4.4 KB
Newer Older
1 2 3 4
function getDeviceBrand (model) {
  if (/iphone/gi.test(model) || /ipad/gi.test(model) || /mac/gi.test(model)) { return 'apple' }
  if (/windows/gi.test(model)) { return 'microsoft' }
}
D
DCloud_LXH 已提交
5 6 7

const UUID_KEY = '__DC_STAT_UUID'
let deviceId
D
DCloud_LXH 已提交
8
export function useDeviceId (result) {
D
DCloud_LXH 已提交
9 10 11 12 13 14 15 16 17 18 19
  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 已提交
20
export function addSafeAreaInsets (result) {
D
DCloud_LXH 已提交
21 22 23 24 25 26 27 28 29 30 31
  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 已提交
32
export function populateParameters (result) {
33 34 35 36 37 38 39
  const {
    brand, model, system,
    language, theme, version,
    hostName, platform, fontSizeSetting,
    SDKVersion, pixelRatio, deviceOrientation,
    environment
  } = result
40
  const isQuickApp = __PLATFORM__.indexOf('quickapp-webview') !== -1
D
DCloud_LXH 已提交
41 42 43 44 45

  // osName osVersion
  let osName = ''
  let osVersion = ''
  if (__PLATFORM__ === 'mp-alipay') {
46
    osName = platform
D
DCloud_LXH 已提交
47 48 49 50 51 52 53 54
    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') {
55 56 57 58
    hostVersion = result.swanNativeVersion
  }
  if (__PLATFORM__ === 'mp-jd') {
    hostVersion = result.hostVersionName
D
DCloud_LXH 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
  }

  // deviceType
  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
      }
    }
  }

  // deviceModel
  let deviceBrand = model.split(' ')[0].toLocaleLowerCase()
82
  if (__PLATFORM__ === 'mp-toutiao' || __PLATFORM__ === 'mp-lark' || isQuickApp) {
D
DCloud_LXH 已提交
83 84 85 86 87 88
    deviceBrand = brand.toLocaleLowerCase()
  } else {
    deviceBrand = getDeviceBrand(deviceBrand)
  }

  // hostName
89 90 91 92 93 94 95 96
  let _hostName = hostName || __PLATFORM__.split('-')[1] // mp-jd
  if (__PLATFORM__ === 'mp-weixin') {
    if (environment) {
      _hostName = environment
    } else if (result.host) {
      _hostName = result.host.env
    }
  }
D
DCloud_LXH 已提交
97
  if (__PLATFORM__ === 'mp-baidu' || __PLATFORM__ === 'mp-kuaishou') { _hostName = result.host }
D
DCloud_LXH 已提交
98
  if (__PLATFORM__ === 'mp-qq') _hostName = result.AppPlatform
D
DCloud_LXH 已提交
99
  if (__PLATFORM__ === 'mp-toutiao' || __PLATFORM__ === 'mp-lark') { _hostName = result.appName }
D
DCloud_LXH 已提交
100 101
  if (__PLATFORM__ === 'mp-alipay') _hostName = result.app

102 103 104 105 106 107 108 109 110 111 112 113
  // 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 已提交
114 115 116 117 118 119 120 121 122
  // 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,
    uniCompileVersion: process.env.UNI_COMPILER_VERSION,
    uniRuntimeVersion: process.env.UNI_COMPILER_VERSION,
123
    uniPlatform: process.env.UNI_SUB_PLATFORM || process.env.UNI_PLATFORM,
D
DCloud_LXH 已提交
124 125 126
    deviceBrand,
    deviceModel: model,
    deviceType,
127 128
    devicePixelRatio: _devicePixelRatio,
    deviceOrientation: _deviceOrientation,
129
    osName: osName.toLocaleLowerCase(),
D
DCloud_LXH 已提交
130
    osVersion,
D
DCloud_LXH 已提交
131
    hostTheme: theme,
D
DCloud_LXH 已提交
132
    hostVersion,
133
    hostLanguage: language.split('_', '-'),
D
DCloud_LXH 已提交
134
    hostName: _hostName,
135 136 137 138
    hostSDKVersion: _SDKVersion,
    hostFontSizeSetting: fontSizeSetting,
    windowTop: 0,
    windowBottom: 0,
D
DCloud_LXH 已提交
139
    // TODO
140 141 142 143 144 145
    osLanguage: undefined,
    osTheme: undefined,
    ua: undefined,
    hostPackageName: undefined,
    browserName: undefined,
    browseVersion: undefined
D
DCloud_LXH 已提交
146 147 148
  }

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