2023年微信小程序API-设备-系统信息
作者: --时间: 2022-09-29
阅读量:
您是否想知道如何获取手机型号、屏幕宽高以及微信版本号等系统信息?微信小程序API中的设备-系统信息可以帮您实现这些功能。
1. wx.getSystemInfo(OBJECT)
通过调用wx.getSystemInfo()方法,我们可以获取到各种关于系统的信息。需要注意的是,为了保证取到数据,我们需要传递OBJECT参数,并在其中添加success、fail和complete回调函数。其中success回调函数可返回系统信息,常用数据如下:
| 参数 | 说明 |
|---|---|
| model | 手机型号 |
| pixelRatio | 设备像素比 |
| system | 操作系统版本 |
| version | 微信版本号 |
| screenWidth | 屏幕宽度 |
| screenHeight | 屏幕高度 |
| windowWidth | 可使用窗口宽度 |
| windowHeight | 可使用窗口高度 |
示例代码:
wx.getSystemInfo({
success: function(res) {
console.log(res.model);
console.log(res.pixelRatio);
console.log(res.windowWidth);
console.log(res.windowHeight);
console.log(res.version);
console.log(res.system);
}
});
2. wx.getSystemInfoSync()
与wx.getSystemInfo()的区别是,wx.getSystemInfoSync()为同步接口获取系统信息,无需传递回调函数。同样,返回的常用数据有:
| 参数 | 说明 |
|---|---|
| model | 手机型号 |
| pixelRatio | 设备像素比 |
| system | 操作系统版本 |
| version | 微信版本号 |
| screenWidth | 屏幕宽度 |
| screenHeight | 屏幕高度 |
| windowWidth | 可使用窗口宽度 |
| windowHeight | 可使用窗口高度 |
示例代码:
try {
var res = wx.getSystemInfoSync();
console.log(res.model);
console.log(res.pixelRatio);
console.log(res.windowWidth);
console.log(res.windowHeight);
console.log(res.version);
console.log(res.system);
} catch (e) {
// Do something when catch error
}
3. wx.canIUse(String)
通过调用wx.canIUse()方法,我们可以判断当前版本是否支持小程序API、回调、参数、组件等。需要注意的是,String参数需要以"${API}.${method}.${param}.${options}"或者"${component}.${attribute}.${option}"方式来调用。
举
上一篇:2023年微信小程序 设备
下一篇:2023年微信小程序API-设备- 网络状态

