deviceInfo.js 1.43 KB
import store from '@/store'

const warnLocal = message => {
  try {
    const localWarn = uni.$logger?.local?.warn
    if (typeof localWarn === 'function') {
      localWarn.call(uni.$logger.local, message)
    } else {
      console.warn(message)
    }
  } catch (error) {
    console.warn(message)
  }
}

export const hasPrivacyConsent = () => Boolean(store.getters.hasPrivacyConsent)

export const getDeviceId = () => {
  if (!hasPrivacyConsent()) return ''

  try {
    return uni.getDeviceInfo()?.deviceId || ''
  } catch (error) {
    warnLocal(`获取设备编码失败:${error?.message || error}`)
    return ''
  }
}

export const getSystemInfo = () => {
  if (!hasPrivacyConsent()) return null

  try {
    return uni.getSystemInfoSync()
  } catch (error) {
    warnLocal(`获取系统信息失败:${error?.message || error}`)
    return null
  }
}

export const getOAID = () => {
  if (!hasPrivacyConsent()) return Promise.resolve('')

  return new Promise(resolve => {
    try {
      if (typeof plus === 'undefined' || typeof plus.device?.getOAID !== 'function') {
        resolve(getDeviceId())
        return
      }

      plus.device.getOAID({
        success: e => {
          resolve(hasPrivacyConsent() ? e?.oaid || '' : '')
        },
        fail: () => {
          resolve(getDeviceId())
        }
      })
    } catch (error) {
      warnLocal(`获取OAID失败:${error?.message || error}`)
      resolve(getDeviceId())
    }
  })
}