upload project
This commit is contained in:
commit
06961cae04
422 changed files with 110626 additions and 0 deletions
241
utils/system-info-modern.js
Normal file
241
utils/system-info-modern.js
Normal file
|
|
@ -0,0 +1,241 @@
|
|||
/**
|
||||
* 现代化系统信息工具类
|
||||
* 使用新的API替代已弃用的wx.getSystemInfoSync
|
||||
*/
|
||||
|
||||
class ModernSystemInfo {
|
||||
constructor() {
|
||||
this.systemInfo = null;
|
||||
this.isInitialized = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取完整的系统信息(推荐使用)
|
||||
*/
|
||||
getSystemInfo() {
|
||||
try {
|
||||
// 使用新的API获取系统信息
|
||||
const windowInfo = wx.getWindowInfo();
|
||||
const deviceInfo = wx.getDeviceInfo();
|
||||
const appBaseInfo = wx.getAppBaseInfo();
|
||||
|
||||
// 合并所有信息
|
||||
const systemInfo = {
|
||||
...windowInfo,
|
||||
...deviceInfo,
|
||||
...appBaseInfo,
|
||||
// 添加一些常用的计算属性
|
||||
isIOS: deviceInfo.platform === 'ios',
|
||||
isAndroid: deviceInfo.platform === 'android',
|
||||
isDevtools: deviceInfo.platform === 'devtools'
|
||||
};
|
||||
|
||||
this.systemInfo = systemInfo;
|
||||
this.isInitialized = true;
|
||||
|
||||
return systemInfo;
|
||||
|
||||
} catch (error) {
|
||||
console.error('获取系统信息失败,使用兜底方案:', error);
|
||||
return this.getFallbackSystemInfo();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 兜底方案:使用旧API
|
||||
*/
|
||||
getFallbackSystemInfo() {
|
||||
try {
|
||||
const systemInfo = wx.getSystemInfoSync();
|
||||
this.systemInfo = {
|
||||
...systemInfo,
|
||||
isIOS: systemInfo.platform === 'ios',
|
||||
isAndroid: systemInfo.platform === 'android',
|
||||
isDevtools: systemInfo.platform === 'devtools'
|
||||
};
|
||||
this.isInitialized = true;
|
||||
return this.systemInfo;
|
||||
} catch (error) {
|
||||
console.error('兜底方案也失败了:', error);
|
||||
// 返回默认值
|
||||
return {
|
||||
statusBarHeight: 44,
|
||||
windowHeight: 667,
|
||||
windowWidth: 375,
|
||||
screenHeight: 667,
|
||||
screenWidth: 375,
|
||||
platform: 'unknown',
|
||||
system: 'unknown',
|
||||
version: 'unknown',
|
||||
isIOS: false,
|
||||
isAndroid: false,
|
||||
isDevtools: false
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取导航栏相关信息
|
||||
*/
|
||||
getNavigationInfo() {
|
||||
try {
|
||||
const systemInfo = this.systemInfo || this.getSystemInfo();
|
||||
const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
|
||||
|
||||
const statusBarHeight = systemInfo.statusBarHeight;
|
||||
const menuButtonHeight = menuButtonInfo.height;
|
||||
const menuButtonTop = menuButtonInfo.top;
|
||||
const menuButtonBottom = menuButtonInfo.bottom;
|
||||
|
||||
// 导航栏高度计算
|
||||
const navBarHeight = menuButtonBottom + menuButtonTop - statusBarHeight;
|
||||
|
||||
return {
|
||||
statusBarHeight,
|
||||
menuButtonHeight,
|
||||
menuButtonTop,
|
||||
menuButtonBottom,
|
||||
navBarHeight,
|
||||
menuButtonInfo
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('获取导航栏信息失败:', error);
|
||||
return {
|
||||
statusBarHeight: 44,
|
||||
menuButtonHeight: 32,
|
||||
menuButtonTop: 6,
|
||||
menuButtonBottom: 38,
|
||||
navBarHeight: 88,
|
||||
menuButtonInfo: {}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取安全区域信息
|
||||
*/
|
||||
getSafeAreaInfo() {
|
||||
try {
|
||||
const systemInfo = this.systemInfo || this.getSystemInfo();
|
||||
const safeArea = systemInfo.safeArea || {};
|
||||
|
||||
return {
|
||||
safeAreaTop: safeArea.top || 0,
|
||||
safeAreaBottom: systemInfo.screenHeight ? systemInfo.screenHeight - safeArea.bottom : 0,
|
||||
safeAreaLeft: safeArea.left || 0,
|
||||
safeAreaRight: systemInfo.screenWidth ? systemInfo.screenWidth - safeArea.right : 0,
|
||||
safeArea: safeArea
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('获取安全区域信息失败:', error);
|
||||
return {
|
||||
safeAreaTop: 0,
|
||||
safeAreaBottom: 0,
|
||||
safeAreaLeft: 0,
|
||||
safeAreaRight: 0,
|
||||
safeArea: {}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 一次性获取页面所需的所有系统信息
|
||||
*/
|
||||
getPageSystemInfo() {
|
||||
const systemInfo = this.getSystemInfo();
|
||||
const navigationInfo = this.getNavigationInfo();
|
||||
const safeAreaInfo = this.getSafeAreaInfo();
|
||||
|
||||
return {
|
||||
...systemInfo,
|
||||
...navigationInfo,
|
||||
...safeAreaInfo
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// 创建单例实例
|
||||
const modernSystemInfo = new ModernSystemInfo();
|
||||
|
||||
/**
|
||||
* 简化的页面系统信息初始化函数
|
||||
* 用于快速替换页面中的 wx.getSystemInfoSync() 调用
|
||||
*/
|
||||
function initPageSystemInfo() {
|
||||
try {
|
||||
const windowInfo = wx.getWindowInfo();
|
||||
const deviceInfo = wx.getDeviceInfo();
|
||||
const appBaseInfo = wx.getAppBaseInfo();
|
||||
const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
|
||||
|
||||
// 合并系统信息
|
||||
const systemInfo = {
|
||||
...windowInfo,
|
||||
...deviceInfo,
|
||||
...appBaseInfo
|
||||
};
|
||||
|
||||
// 计算导航栏相关信息
|
||||
const statusBarHeight = windowInfo.statusBarHeight;
|
||||
const menuButtonHeight = menuButtonInfo.height;
|
||||
const menuButtonTop = menuButtonInfo.top;
|
||||
const menuButtonBottom = menuButtonInfo.bottom;
|
||||
const navBarHeight = menuButtonBottom + menuButtonTop - statusBarHeight;
|
||||
const windowHeight = windowInfo.windowHeight;
|
||||
const safeAreaBottom = windowInfo.safeArea ? windowInfo.screenHeight - windowInfo.safeArea.bottom : 0;
|
||||
|
||||
return {
|
||||
systemInfo,
|
||||
statusBarHeight,
|
||||
menuButtonHeight,
|
||||
menuButtonTop,
|
||||
navBarHeight,
|
||||
windowHeight,
|
||||
safeAreaBottom,
|
||||
menuButtonInfo
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('现代API获取系统信息失败,使用兜底方案:', error);
|
||||
// 兜底方案
|
||||
try {
|
||||
const systemInfo = wx.getSystemInfoSync();
|
||||
const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
|
||||
|
||||
const statusBarHeight = systemInfo.statusBarHeight;
|
||||
const menuButtonHeight = menuButtonInfo.height;
|
||||
const menuButtonTop = menuButtonInfo.top;
|
||||
const menuButtonBottom = menuButtonInfo.bottom;
|
||||
const navBarHeight = menuButtonBottom + menuButtonTop - statusBarHeight;
|
||||
const windowHeight = systemInfo.windowHeight;
|
||||
const safeAreaBottom = systemInfo.safeArea ? systemInfo.screenHeight - systemInfo.safeArea.bottom : 0;
|
||||
|
||||
return {
|
||||
systemInfo,
|
||||
statusBarHeight,
|
||||
menuButtonHeight,
|
||||
menuButtonTop,
|
||||
navBarHeight,
|
||||
windowHeight,
|
||||
safeAreaBottom,
|
||||
menuButtonInfo
|
||||
};
|
||||
} catch (fallbackError) {
|
||||
console.error('兜底方案也失败了:', fallbackError);
|
||||
return {
|
||||
systemInfo: {},
|
||||
statusBarHeight: 44,
|
||||
menuButtonHeight: 32,
|
||||
menuButtonTop: 6,
|
||||
navBarHeight: 88,
|
||||
windowHeight: 667,
|
||||
safeAreaBottom: 0,
|
||||
menuButtonInfo: {}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
modernSystemInfo,
|
||||
initPageSystemInfo
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue