129 lines
No EOL
3.3 KiB
JavaScript
129 lines
No EOL
3.3 KiB
JavaScript
// 系统信息工具类 - 使用新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
|
||
};
|
||
|
||
console.log('系统信息初始化完成:', this.systemInfo);
|
||
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;
|