| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- /**
- * 设备信息工具
- */
- import { getStorage, setStorage } from './storage'
- const DEVICE_ID_KEY = 'DEVICE_ID'
- /**
- * 生成设备ID
- */
- function generateDeviceId(): string {
- return `device_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`
- }
- /**
- * 获取设备ID
- */
- export function getDeviceId(): string {
- let deviceId = getStorage<string>(DEVICE_ID_KEY)
- if (!deviceId) {
- deviceId = generateDeviceId()
- setStorage(DEVICE_ID_KEY, deviceId)
- }
- return deviceId
- }
- /**
- * 获取平台类型
- */
- export function getPlatform(): 'android' | 'ios' | 'h5' | 'mp-weixin' | 'mp-alipay' {
- // #ifdef APP-PLUS
- // @ts-ignore
- const platform = plus.os.name.toLowerCase()
- return platform === 'ios' ? 'ios' : 'android'
- // #endif
- // #ifdef H5
- return 'h5'
- // #endif
- // #ifdef MP-WEIXIN
- return 'mp-weixin'
- // #endif
- // #ifdef MP-ALIPAY
- return 'mp-alipay'
- // #endif
- return 'h5'
- }
- /**
- * 获取APP类型
- */
- export function getAppType(): string {
- const platform = getPlatform()
- return platform === 'android' || platform === 'ios' ? 'app' : platform
- }
- /**
- * 请求头:X-Platform、X-Version(普通请求统一带上,便于后端与网关识别客户端来源)。
- */
- export function getMobileClientHeaders(): Record<string, string> {
- const platform = getPlatform()
- let xPlatform = 'H5'
- if (platform === 'mp-weixin' || platform === 'mp-alipay') {
- xPlatform = 'MINI'
- } else if (platform === 'android' || platform === 'ios') {
- xPlatform = 'APP'
- }
- const { version } = getRuntimeClientVersion()
- return {
- 'X-Platform': xPlatform,
- 'X-Version': version || '1.0.0'
- }
- }
- /**
- * 小程序 / App 运行时版本号与整型构建号(非 H5 静态写死;与 manifest 发布配置一致)。
- */
- export function getRuntimeClientVersion(): { version: string; versionCode: number } {
- let version = ''
- let versionCode = 0
- // #ifdef MP-WEIXIN
- try {
- const ac = uni.getAccountInfoSync()
- const mp = ac?.miniProgram as { version?: string; versionCode?: number | string } | undefined
- if (mp?.version) version = String(mp.version).trim()
- const raw = mp?.versionCode
- if (raw !== undefined && raw !== null && raw !== '') {
- const n = typeof raw === 'number' ? raw : parseInt(String(raw), 10)
- if (!Number.isNaN(n)) versionCode = n
- }
- } catch {
- /* ignore */
- }
- // #endif
- return { version, versionCode }
- }
|