findme-miniprogram-frontend/utils/system-info.js
2025-12-27 17:16:03 +08:00

128 lines
3.2 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 系统信息工具类 - 使用新API替换废弃的wx.getSystemInfoSync
class SystemInfoUtil {
constructor() {
this.systemInfo = null;
this.windowInfo = null;
this.deviceInfo = null;
this.appBaseInfo = null;
}
// 初始化系统信息
async init() {
try {
// 使用新的API获取系统信息
const [windowInfo, deviceInfo, appBaseInfo] = await Promise.all([
this.getWindowInfo(),
this.getDeviceInfo(),
this.getAppBaseInfo()
]);
this.windowInfo = windowInfo;
this.deviceInfo = deviceInfo;
this.appBaseInfo = appBaseInfo;
// 合并为兼容的systemInfo格式
this.systemInfo = {
...windowInfo,
...deviceInfo,
...appBaseInfo
};
return this.systemInfo;
} catch (error) {
console.error('系统信息初始化失败:', error);
// 兜底使用旧API
this.systemInfo = wx.getSystemInfoSync();
return this.systemInfo;
}
}
// 获取窗口信息
getWindowInfo() {
return new Promise((resolve, reject) => {
try {
const windowInfo = wx.getWindowInfo();
resolve(windowInfo);
} catch (error) {
reject(error);
}
});
}
// 获取设备信息
getDeviceInfo() {
return new Promise((resolve, reject) => {
try {
const deviceInfo = wx.getDeviceInfo();
resolve(deviceInfo);
} catch (error) {
reject(error);
}
});
}
// 获取应用基础信息
getAppBaseInfo() {
return new Promise((resolve, reject) => {
try {
const appBaseInfo = wx.getAppBaseInfo();
resolve(appBaseInfo);
} catch (error) {
reject(error);
}
});
}
// 获取系统适配信息
getSystemAdaptInfo() {
if (!this.systemInfo) {
console.warn('系统信息未初始化使用同步API');
this.systemInfo = wx.getSystemInfoSync();
}
const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
// 状态栏高度
const statusBarHeight = this.systemInfo.statusBarHeight || 0;
// 胶囊按钮信息
const menuButtonHeight = menuButtonInfo.height;
const menuButtonTop = menuButtonInfo.top;
const menuButtonBottom = menuButtonInfo.bottom;
// 导航栏高度 = 胶囊按钮底部 + 胶囊按钮顶部到状态栏的距离
const navBarHeight = menuButtonBottom + menuButtonTop - statusBarHeight;
// 窗口高度
const windowHeight = this.systemInfo.windowHeight || this.systemInfo.screenHeight;
// 安全区域
const safeAreaBottom = this.systemInfo.safeArea ?
this.systemInfo.screenHeight - this.systemInfo.safeArea.bottom : 0;
return {
systemInfo: this.systemInfo,
statusBarHeight,
menuButtonHeight,
menuButtonTop,
navBarHeight,
windowHeight,
safeAreaBottom
};
}
// 获取性能信息
getPerformanceInfo() {
return {
platform: this.systemInfo?.platform || 'unknown',
version: this.systemInfo?.version || 'unknown',
SDKVersion: this.systemInfo?.SDKVersion || 'unknown',
benchmarkLevel: this.systemInfo?.benchmarkLevel || 0
};
}
}
// 创建单例
const systemInfoUtil = new SystemInfoUtil();
module.exports = systemInfoUtil;