/** * @file @/common/util/system.js * @description UniApp 系统信息工具类:封装平台判断、设备信息获取、屏幕适配等通用能力 * @author 适配全平台(APP/小程序/H5/快应用等) */ // 缓存系统信息(避免重复调用 uni.getSystemInfoSync,提升性能) let systemInfoCache = null; /** * 1. 核心方法:获取设备系统信息(带缓存) * @returns {Object} 系统信息完整对象 * @property {string} platform - 运行平台(android/ios/mp-weixin/mp-alipay/h5/quickapp等) * @property {number} screenWidth - 屏幕宽度(px) * @property {number} screenHeight - 屏幕高度(px) * @property {number} statusBarHeight - 状态栏高度(px,用于导航栏适配) * @property {string} system - 系统版本(如 "Android 13"、"iOS 16.1") * @property {string} model - 设备型号(如 "iPhone 13"、"MI 12") */ export function getSystemInfo() { // 若已缓存,直接返回 if (systemInfoCache) return systemInfoCache; try { // 同步获取系统信息(UniApp 原生 API,支持全平台) systemInfoCache = uni.getSystemInfoSync(); return systemInfoCache; } catch (error) { console.error('获取系统信息失败:', error); // 异常时返回默认值,避免业务崩溃 return { platform: 'unknown', screenWidth: 375, screenHeight: 667, statusBarHeight: 20, system: 'Unknown', model: 'Unknown Device' }; } } /** * 2. 平台判断:是否为 APP 端(安卓/iOS) * @returns {boolean} true=APP端,false=其他平台 */ export function isApp() { const { platform } = getSystemInfo(); return platform === 'android' || platform === 'ios'; } /** * 3. 平台判断:是否为小程序端(支持微信/支付宝/百度/字节等) * @param {string} [specificMiniType] 可选:指定小程序类型(如 "mp-weixin" 仅判断微信小程序) * @returns {boolean} true=小程序端,false=其他平台 * @example isMini() → 判断所有小程序;isMini('mp-alipay') → 仅判断支付宝小程序 */ export function isMini(specificMiniType = '') { const { platform } = getSystemInfo(); // 小程序平台标识均以 "mp-" 开头(如 mp-weixin/mp-alipay/mp-baidu) const isMiniPlatform = platform.startsWith('mp-'); // 若指定了具体小程序类型(如仅判断微信),则进一步匹配 if (specificMiniType) { return isMiniPlatform && platform === specificMiniType; } return isMiniPlatform; } /** * 4. 平台判断:是否为 H5 端(浏览器) * @returns {boolean} true=H5端,false=其他平台 */ export function isH5() { const { platform } = getSystemInfo(); return platform === 'h5'; } /** * 5. 平台判断:是否为快应用端 * @returns {boolean} true=快应用端,false=其他平台 */ export function isQuickApp() { const { platform } = getSystemInfo(); return platform === 'quickapp'; } /** * 6. 设备判断:是否为 iOS 设备(仅 APP 端有效) * @returns {boolean} true=iOS设备,false=安卓或其他平台 */ export function isIOS() { const { platform } = getSystemInfo(); return platform === 'ios'; } /** * 7. 设备判断:是否为安卓设备(仅 APP 端有效) * @returns {boolean} true=安卓设备,false=iOS或其他平台 */ export function isAndroid() { const { platform } = getSystemInfo(); return platform === 'android'; } /** * 8. 屏幕适配:获取导航栏高度(状态栏 + 导航栏内容区,用于自定义导航栏) * @returns {number} 导航栏总高度(px) */ export function getNavBarHeight() { const { statusBarHeight, platform } = getSystemInfo(); // 适配规则:iOS 导航栏内容区高度 44px,安卓 48px(UniApp 通用标准) const navContentHeight = platform === 'ios' ? 44 : 48; return statusBarHeight + navContentHeight; } /** * 9. 屏幕适配:px 转 rpx(UniApp 样式适配,750rpx = 屏幕宽度) * @param {number} px - 设计稿中的 px 值(基于 750px 宽度设计稿) * @returns {number} 转换后的 rpx 值 */ export function px2rpx(px) { const { screenWidth } = getSystemInfo(); // 公式:rpx = px * (750 / 屏幕宽度) return Math.floor(px * (750 / screenWidth)); } /** * 10. 屏幕适配:rpx 转 px(用于动态计算元素尺寸) * @param {number} rpx - 样式中的 rpx 值 * @returns {number} 转换后的 px 值 */ export function rpx2px(rpx) { const { screenWidth } = getSystemInfo(); // 公式:px = rpx * (屏幕宽度 / 750) return Math.floor(rpx * (screenWidth / 750)); } /** * 11. 获取设备唯一标识(注意:部分平台有隐私限制) * @returns {string} 设备标识(失败时返回空字符串) */ export function getDeviceId() { try { const { deviceId } = getSystemInfo(); // 部分平台(如 iOS 14+)可能返回空,需兼容 return deviceId || ''; } catch (error) { console.error('获取设备ID失败:', error); return ''; } }