enhanceSystemInfo.ts 5.3 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import { extend } from '@vue/shared'
2

3
function _getDeviceBrand(model: string) {
4 5 6 7 8 9
  if (/iphone/gi.test(model) || /ipad/gi.test(model) || /mac/gi.test(model)) {
    return 'apple'
  }
  if (/windows/gi.test(model)) {
    return 'microsoft'
  }
10
  return ''
11
}
D
DCloud_LXH 已提交
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

const UUID_KEY = '__DC_STAT_UUID'
let deviceId: string
interface Global {
  getStorageSync: UniApp.Uni['getStorageSync']
}
export function useDeviceId(global: Global = __GLOBAL__ as Global) {
  return function addDeviceId(_: any, toRes: UniApp.GetSystemInfoResult) {
    deviceId = deviceId || global.getStorageSync(UUID_KEY)
    if (!deviceId) {
      deviceId = Date.now() + '' + Math.floor(Math.random() * 1e7)
      __GLOBAL__.setStorage({
        key: UUID_KEY,
        data: deviceId,
      })
    }
    toRes.deviceId = deviceId
  }
}

export function addSafeAreaInsets(
  fromRes: any,
  toRes: UniApp.GetSystemInfoResult
) {
  if (fromRes.safeArea) {
    const safeArea = fromRes.safeArea
    toRes.safeAreaInsets = {
      top: safeArea.top,
      left: safeArea.left,
      right: fromRes.windowWidth - safeArea.right,
      bottom: fromRes.screenHeight - safeArea.bottom,
    }
  }
}

export function populateParameters(
  fromRes: any,
  toRes: UniApp.GetSystemInfoResult
) {
51
  const {
52 53 54 55
    brand = '',
    model = '',
    system = '',
    language = '',
56 57
    theme,
    version,
58
    hostName,
59
    platform,
60 61 62 63 64
    fontSizeSetting,
    SDKVersion,
    pixelRatio,
    deviceOrientation,
    environment,
65 66
  } = fromRes
  const isQuickApp = __PLATFORM__.indexOf('quickapp-webview') !== -1
D
DCloud_LXH 已提交
67 68 69 70 71

  // osName osVersion
  let osName = ''
  let osVersion = ''
  if (__PLATFORM__ === 'mp-alipay') {
72
    osName = platform
D
DCloud_LXH 已提交
73 74 75 76 77 78 79 80
    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') {
81 82 83 84
    hostVersion = fromRes.swanNativeVersion
  }
  if (__PLATFORM__ === 'mp-jd') {
    hostVersion = fromRes.hostVersionName
D
DCloud_LXH 已提交
85 86 87
  }

  // deviceType
88
  let deviceType = getGetDeviceType(fromRes, model)
D
DCloud_LXH 已提交
89 90

  // deviceModel
91
  let deviceBrand = getDeviceBrand(brand, model, isQuickApp)
D
DCloud_LXH 已提交
92 93

  // hostName
94 95 96 97
  let _hostName = hostName || __PLATFORM__.split('-')[1] // mp-jd
  if (__PLATFORM__ === 'mp-weixin') {
    if (environment) {
      _hostName = environment
98
    } else if (fromRes.host && fromRes.host.env) {
99 100 101 102
      _hostName = fromRes.host.env
    }
  }
  if (__PLATFORM__ === 'mp-baidu' || __PLATFORM__ === 'mp-kuaishou') {
D
DCloud_LXH 已提交
103
    _hostName = fromRes.host
104
  }
D
DCloud_LXH 已提交
105
  if (__PLATFORM__ === 'mp-qq') _hostName = fromRes.AppPlatform
106
  if (__PLATFORM__ === 'mp-toutiao' || __PLATFORM__ === 'mp-lark') {
D
DCloud_LXH 已提交
107
    _hostName = fromRes.appName
108
  }
D
DCloud_LXH 已提交
109 110
  if (__PLATFORM__ === 'mp-alipay') _hostName = fromRes.app

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
  // deviceOrientation
  let _deviceOrientation = deviceOrientation // 仅 微信 百度 支持
  if (__PLATFORM__ === 'mp-baidu') {
    _deviceOrientation = fromRes.orientation
  }

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

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

D
DCloud_LXH 已提交
129 130 131 132 133 134 135 136 137
  // 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,
138
    uniPlatform: process.env.UNI_SUB_PLATFORM || process.env.UNI_PLATFORM,
D
DCloud_LXH 已提交
139 140 141
    deviceBrand,
    deviceModel: model,
    deviceType,
142 143
    devicePixelRatio: _devicePixelRatio,
    deviceOrientation: _deviceOrientation,
144
    osName: osName.toLocaleLowerCase(),
D
DCloud_LXH 已提交
145
    osVersion,
D
DCloud_LXH 已提交
146
    hostTheme: theme,
D
DCloud_LXH 已提交
147
    hostVersion,
D
DCloud_LXH 已提交
148
    hostLanguage: language.replace('_', '-'),
D
DCloud_LXH 已提交
149
    hostName: _hostName,
150 151 152 153
    hostSDKVersion: _SDKVersion,
    hostFontSizeSetting: fontSizeSetting,
    windowTop: 0,
    windowBottom: 0,
D
DCloud_LXH 已提交
154
    // TODO
155 156 157 158 159 160
    osLanguage: undefined,
    osTheme: undefined,
    ua: undefined,
    hostPackageName: undefined,
    browserName: undefined,
    browseVersion: undefined,
D
DCloud_LXH 已提交
161 162
  }

fxy060608's avatar
fxy060608 已提交
163
  extend(toRes, parameters)
D
DCloud_LXH 已提交
164
}
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208

export function getGetDeviceType(fromRes: any, model: string) {
  // deviceType
  let deviceType = fromRes.deviceType || 'phone'
  if (__PLATFORM__ !== 'mp-baidu') {
    type DeviceTypeMapsKeys = keyof typeof deviceTypeMaps
    const deviceTypeMaps = {
      ipad: 'pad',
      windows: 'pc',
      mac: 'pc',
    }
    const deviceTypeMapsKeys = Object.keys(
      deviceTypeMaps
    ) as DeviceTypeMapsKeys[]
    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
}

export function getDeviceBrand(
  brand: string,
  model: string,
  isQuickApp: boolean = false
) {
  // deviceModel
  let deviceBrand = model.split(' ')[0].toLocaleLowerCase()
  if (
    __PLATFORM__ === 'mp-toutiao' ||
    __PLATFORM__ === 'mp-lark' ||
    isQuickApp
  ) {
    deviceBrand = brand.toLocaleLowerCase()
  } else {
    deviceBrand = _getDeviceBrand(deviceBrand)
  }
  return deviceBrand
}