upload project
This commit is contained in:
commit
06961cae04
422 changed files with 110626 additions and 0 deletions
247
utils/system-info-helper.js
Normal file
247
utils/system-info-helper.js
Normal file
|
|
@ -0,0 +1,247 @@
|
|||
/**
|
||||
* 系统信息工具类
|
||||
* 使用新API替换废弃的wx.getSystemInfoSync
|
||||
*/
|
||||
|
||||
class SystemInfoHelper {
|
||||
constructor() {
|
||||
this.cachedInfo = null;
|
||||
this.cacheTime = 0;
|
||||
this.cacheExpiry = 5 * 60 * 1000; // 5分钟缓存
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取完整的系统信息(新API版本)
|
||||
*/
|
||||
async getSystemInfo() {
|
||||
// 检查缓存
|
||||
if (this.cachedInfo && (Date.now() - this.cacheTime) < this.cacheExpiry) {
|
||||
return this.cachedInfo;
|
||||
}
|
||||
|
||||
try {
|
||||
// 并行获取各种系统信息
|
||||
const [windowInfo, deviceInfo, appBaseInfo] = await Promise.all([
|
||||
this.getWindowInfo(),
|
||||
this.getDeviceInfo(),
|
||||
this.getAppBaseInfo()
|
||||
]);
|
||||
|
||||
// 🔥 合并所有信息 - 使用Object.assign替代扩展运算符,避免Babel依赖问题
|
||||
const systemInfo = Object.assign({}, windowInfo, deviceInfo, appBaseInfo, {
|
||||
// 保持向后兼容的字段名
|
||||
windowHeight: windowInfo.windowHeight,
|
||||
windowWidth: windowInfo.windowWidth,
|
||||
statusBarHeight: windowInfo.statusBarHeight,
|
||||
safeArea: windowInfo.safeArea,
|
||||
platform: deviceInfo.platform,
|
||||
system: deviceInfo.system,
|
||||
model: deviceInfo.model,
|
||||
brand: deviceInfo.brand,
|
||||
version: appBaseInfo.version,
|
||||
SDKVersion: appBaseInfo.SDKVersion,
|
||||
language: appBaseInfo.language,
|
||||
theme: appBaseInfo.theme
|
||||
});
|
||||
|
||||
// 缓存结果
|
||||
this.cachedInfo = systemInfo;
|
||||
this.cacheTime = Date.now();
|
||||
|
||||
return systemInfo;
|
||||
} catch (error) {
|
||||
console.warn('获取系统信息失败,使用兜底方案:', error);
|
||||
return this.getFallbackSystemInfo();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 同步获取系统信息(用于替换wx.getSystemInfoSync)
|
||||
*/
|
||||
getSystemInfoSync() {
|
||||
// 如果有缓存,直接返回
|
||||
if (this.cachedInfo && (Date.now() - this.cacheTime) < this.cacheExpiry) {
|
||||
return this.cachedInfo;
|
||||
}
|
||||
|
||||
try {
|
||||
// 尝试使用新的同步API
|
||||
const windowInfo = wx.getWindowInfo();
|
||||
const deviceInfo = wx.getDeviceInfo();
|
||||
const appBaseInfo = wx.getAppBaseInfo();
|
||||
|
||||
const systemInfo = {
|
||||
...windowInfo,
|
||||
...deviceInfo,
|
||||
...appBaseInfo,
|
||||
// 保持向后兼容
|
||||
windowHeight: windowInfo.windowHeight,
|
||||
windowWidth: windowInfo.windowWidth,
|
||||
statusBarHeight: windowInfo.statusBarHeight,
|
||||
safeArea: windowInfo.safeArea,
|
||||
platform: deviceInfo.platform,
|
||||
system: deviceInfo.system,
|
||||
model: deviceInfo.model,
|
||||
brand: deviceInfo.brand,
|
||||
version: appBaseInfo.version,
|
||||
SDKVersion: appBaseInfo.SDKVersion,
|
||||
language: appBaseInfo.language,
|
||||
theme: appBaseInfo.theme
|
||||
};
|
||||
|
||||
// 缓存结果
|
||||
this.cachedInfo = systemInfo;
|
||||
this.cacheTime = Date.now();
|
||||
|
||||
return systemInfo;
|
||||
} catch (error) {
|
||||
console.warn('新API获取系统信息失败,使用兜底方案:', error);
|
||||
return this.getFallbackSystemInfo();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取窗口信息
|
||||
*/
|
||||
getWindowInfo() {
|
||||
return new Promise((resolve) => {
|
||||
try {
|
||||
const windowInfo = wx.getWindowInfo();
|
||||
resolve(windowInfo);
|
||||
} catch (error) {
|
||||
console.warn('获取窗口信息失败:', error);
|
||||
resolve({
|
||||
windowHeight: 667,
|
||||
windowWidth: 375,
|
||||
statusBarHeight: 44,
|
||||
safeArea: { top: 44, bottom: 667 }
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取设备信息
|
||||
*/
|
||||
getDeviceInfo() {
|
||||
return new Promise((resolve) => {
|
||||
try {
|
||||
const deviceInfo = wx.getDeviceInfo();
|
||||
resolve(deviceInfo);
|
||||
} catch (error) {
|
||||
console.warn('获取设备信息失败:', error);
|
||||
resolve({
|
||||
platform: 'unknown',
|
||||
system: 'unknown',
|
||||
model: 'unknown',
|
||||
brand: 'unknown',
|
||||
benchmarkLevel: 1
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取应用基础信息
|
||||
*/
|
||||
getAppBaseInfo() {
|
||||
return new Promise((resolve) => {
|
||||
try {
|
||||
const appBaseInfo = wx.getAppBaseInfo();
|
||||
resolve(appBaseInfo);
|
||||
} catch (error) {
|
||||
console.warn('获取应用信息失败:', error);
|
||||
resolve({
|
||||
version: '1.0.0',
|
||||
SDKVersion: '2.0.0',
|
||||
language: 'zh_CN',
|
||||
theme: 'light'
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 兜底系统信息
|
||||
*/
|
||||
getFallbackSystemInfo() {
|
||||
try {
|
||||
// 最后的兜底方案:使用旧API
|
||||
return wx.getSystemInfoSync();
|
||||
} catch (error) {
|
||||
console.error('所有获取系统信息的方法都失败了:', error);
|
||||
// 返回默认值
|
||||
return {
|
||||
windowHeight: 667,
|
||||
windowWidth: 375,
|
||||
statusBarHeight: 44,
|
||||
safeArea: { top: 44, bottom: 667 },
|
||||
platform: 'unknown',
|
||||
system: 'unknown',
|
||||
model: 'unknown',
|
||||
brand: 'unknown',
|
||||
version: '1.0.0',
|
||||
SDKVersion: '2.0.0',
|
||||
language: 'zh_CN',
|
||||
theme: 'light'
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除缓存
|
||||
*/
|
||||
clearCache() {
|
||||
this.cachedInfo = null;
|
||||
this.cacheTime = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取菜单按钮信息
|
||||
*/
|
||||
getMenuButtonBoundingClientRect() {
|
||||
try {
|
||||
return wx.getMenuButtonBoundingClientRect();
|
||||
} catch (error) {
|
||||
console.warn('获取菜单按钮信息失败:', error);
|
||||
return {
|
||||
width: 87,
|
||||
height: 32,
|
||||
top: 48,
|
||||
right: 365,
|
||||
bottom: 80,
|
||||
left: 278
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算导航栏高度
|
||||
*/
|
||||
getNavBarHeight(systemInfo) {
|
||||
try {
|
||||
const menuButton = this.getMenuButtonBoundingClientRect();
|
||||
const statusBarHeight = systemInfo.statusBarHeight || 44;
|
||||
|
||||
// 导航栏高度 = 状态栏高度 + 胶囊按钮高度 + 额外间距
|
||||
const navBarHeight = statusBarHeight + menuButton.height +
|
||||
(menuButton.top - statusBarHeight) * 2;
|
||||
|
||||
return navBarHeight;
|
||||
} catch (error) {
|
||||
console.warn('计算导航栏高度失败:', error);
|
||||
return 88; // 默认导航栏高度
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 创建全局实例
|
||||
const systemInfoHelper = new SystemInfoHelper();
|
||||
|
||||
module.exports = {
|
||||
SystemInfoHelper,
|
||||
systemInfoHelper,
|
||||
// 向后兼容的函数
|
||||
getSystemInfo: () => systemInfoHelper.getSystemInfo(),
|
||||
getSystemInfoSync: () => systemInfoHelper.getSystemInfoSync()
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue