| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- /**
- * 设备信息工具
- */
- const DEVICE_ID_KEY = 'marketing_device_id'
- /**
- * 生成设备ID
- */
- function generateDeviceId(): string {
- return `device_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`
- }
- /**
- * 获取设备ID
- */
- export function getDeviceId(): string {
- let deviceId = uni.getStorageSync(DEVICE_ID_KEY) || ''
- if (!deviceId) {
- deviceId = generateDeviceId()
- uni.setStorageSync(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
- }
- /**
- * 获取运行时小程序 appid
- */
- export function getRuntimeMiniAppId(): string {
- // #ifdef MP-WEIXIN
- try {
- const accountInfo = uni.getAccountInfoSync()
- return accountInfo?.miniProgram?.appId || ''
- } catch {
- return ''
- }
- // #endif
- return ''
- }
- /**
- * 与《移动端接口规范》对齐的请求头: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()
- const headers: Record<string, string> = {
- 'X-Platform': xPlatform,
- 'X-Version': version || '1.0.0'
- }
- const miniAppId = getRuntimeMiniAppId()
- if (miniAppId) {
- headers.APPID = miniAppId
- }
- return headers
- }
- /**
- * 获取系统信息
- */
- export function getSystemInfo(): UniApp.GetSystemInfoResult {
- return uni.getSystemInfoSync()
- }
- /**
- * 获取应用版本
- */
- export function getAppVersion(): string {
- // #ifdef APP-PLUS
- // @ts-ignore
- return plus.runtime.version
- // #endif
- // #ifndef APP-PLUS
- return ''
- // #endif
- }
- /**
- * 小程序 / 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
- // #ifdef APP-PLUS
- try {
- // @ts-ignore
- const v = plus.runtime.version
- if (v) version = String(v).trim()
- // @ts-ignore
- const c = plus.runtime.versionCode
- if (typeof c === 'number' && !Number.isNaN(c)) versionCode = c
- } catch {
- /* ignore */
- }
- // #endif
- return { version, versionCode }
- }
- /**
- * 获取网络类型
- */
- export function getNetworkType(): Promise<string> {
- return new Promise((resolve, reject) => {
- uni.getNetworkType({
- success: (res) => {
- resolve(res.networkType)
- },
- fail: reject
- })
- })
- }
|