微信小程序官方已废弃 wx.getSystemInfoSync API,推荐使用以下新API替代:
- wx.getSystemSetting - 获取系统设置
- wx.getAppAuthorizeSetting - 获取授权设置
- wx.getDeviceInfo - 获取设备信息
- wx.getWindowInfo - 获取窗口信息
- wx.getAppBaseInfo - 获取基础信息
在 app/utils/common.js 中新增 getSystemInfo() 函数:
/**
* 获取系统信息(兼容新旧API)
* 替代已废弃的uni.getSystemInfoSync()
* @returns {Object} 系统信息对象
*/
export function getSystemInfo() {
// 微信小程序使用新API
#ifdef MP-WEIXIN
try {
const windowInfo = uni.getWindowInfo()
const deviceInfo = uni.getDeviceInfo()
const appBaseInfo = uni.getAppBaseInfo()
return {
...windowInfo,
...deviceInfo,
...appBaseInfo,
// 兼容旧字段名
windowHeight: windowInfo.windowHeight,
windowWidth: windowInfo.windowWidth,
// ... 更多字段
}
} catch (e) {
// 降级使用旧API
return uni.getSystemInfoSync()
}
#endif
// 其他平台继续使用旧API
#ifndef MP-WEIXIN
return uni.getSystemInfoSync()
#endif
}
已修改以下项目文件,统一使用新的工具函数:
| 文件路径 | 修改内容 |
|---|---|
app/pages/mine/avatar/index.vue |
替换 uni.getSystemInfoSync() 为 getSystemInfo() |
app/pages/mine/index.vue |
在computed中使用 getSystemInfo() |
app/pages/mine/setting/index.vue |
在data中使用 getSystemInfo() |
以下 uni_modules 中的第三方组件仍使用旧API,需等待官方更新:
- uni-datetime-picker
- uni-fab
- uni-load-more
- uni-nav-bar
- uni-notice-bar
- uni-popup
- uni-table
这些组件的警告不影响功能使用,可忽略。
import { getSystemInfo } from '@/utils/common'
// 获取窗口高度
const windowHeight = getSystemInfo().windowHeight
// 获取设备平台
const platform = getSystemInfo().platform
// 获取屏幕宽度
const screenWidth = getSystemInfo().screenWidth
uni.getSystemInfoSync()getSystemInfo() 工具函数修改时间: 2025-10-26
修改人: AI Assistant
影响范围: 微信小程序端系统信息获取