uni.getSystemInfo({ success: function (res) { getApp().globalData.phoneInfo = res }
时间: 2024-05-31 08:12:23 浏览: 17
这是一个使用uni-app框架中的API获取用户设备信息的代码段。`uni.getSystemInfo`是一个异步方法,用于获取用户设备的系统信息,包括设备型号、操作系统版本、屏幕宽高等等。当获取成功后,会将信息存储在全局变量`getApp().globalData.phoneInfo`中。这样,其他模块就可以访问该变量来获取设备信息。
相关问题
uni.getSystemInfo
uni.getSystemInfo是一个uni-app提供的API,用于获取当前设备的系统信息。根据提供的引用内容,可以看出在uni-app项目中,通过点击按钮触发getSystemInfo接口来获取系统信息。具体的实现步骤如下:
1. 在index.vue文件中添加一个按钮,并通过点击事件触发get_system方法。代码如下:
```
<view style="height: 10rpx;"></view>
<button type="primary" style="width: 500rpx;" @click="get_system">获取系统信息</button>
```
2. 在methods中定义get_system方法,通过uni.getSystemInfo来获取系统信息。代码如下:
```
get_system: function() {
console.log("获取系统信息");
uni.getSystemInfo({
success(res) {
console.log(res);
}
})
},
```
通过以上步骤,当点击按钮时,会调用get_system方法,然后通过uni.getSystemInfo获取系统信息,并将结果打印到控制台中。
#### 引用[.reference_title]
- *1* *2* *3* [【uniapp】 获取系统信息](https://blog.csdn.net/qq_35230125/article/details/126480540)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
onLaunch: async function (options) { this.init() //var _this = this; let menuButtonObject = wx.getMenuButtonBoundingClientRect(); const res = await wx.cloud.callFunction({name: 'getServerTime'}); // 使用await等待异步请求结果 console.log('服务器时间:',res.result) const cloudTimeStamp = res.result;// 需要从res中取出result console.log('服务器时间:',cloudTimeStamp) // 获取小程序系统时间戳 const systemTimeStamp = Date.now(); console.log('运行环境系统时间:',systemTimeStamp) // 计算时间戳差值,单位为秒 const diff = Math.abs(cloudTimeStamp - systemTimeStamp) / 1000; console.log('时间差值:',diff) // 如果时间戳差值大于一定值,则认为小程序系统时间被篡改 if (diff > 100) { wx.showToast({ title: '小程序系统时间被篡改,请检查时间设置', icon: 'none', }); return; } wx.getSystemInfo({ success: res => { let statusBarHeight = res.statusBarHeight, navTop = menuButtonObject.top, navHeight = statusBarHeight + menuButtonObject.height + (menuButtonObject.top - statusBarHeight) * 2; this.globalData.navHeight = navHeight; this.globalData.navTop = navTop; this.globalData.windowHeight = res.windowHeight; }, fail(err) { console.log(err); } }) },怎么修改这个函数让diff大于100时,停止运行小程序
你可以在函数的开头部分添加一个判断,如果diff大于100则直接return,代码如下:
```
onLaunch: async function (options) {
this.init()
let menuButtonObject = wx.getMenuButtonBoundingClientRect();
const res = await wx.cloud.callFunction({name: 'getServerTime'});
const cloudTimeStamp = res.result;
const systemTimeStamp = Date.now();
const diff = Math.abs(cloudTimeStamp - systemTimeStamp) / 1000;
if (diff > 100) {
wx.showToast({
title: '小程序系统时间被篡改,请检查时间设置',
icon: 'none',
});
return; // 添加这行代码
}
wx.getSystemInfo({
success: res => {
let statusBarHeight = res.statusBarHeight,
navTop = menuButtonObject.top,
navHeight = statusBarHeight + menuButtonObject.height + (menuButtonObject.top - statusBarHeight) * 2;
this.globalData.navHeight = navHeight;
this.globalData.navTop = navTop;
this.globalData.windowHeight = res.windowHeight;
},
fail(err) {
console.log(err);
}
})
}
```
当diff大于100时,会弹出一个提示框,并且直接return,停止运行小程序。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)