Houjie
2 天以前 30590221cd34912a61f4734ea474859a0df98be6
1
2
3
4
5
6
7
8
9
10
11
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
 * @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 '';
  }
}