场景
最近,Windows 微信的更新了可以访问小程序的功能,但还不完善。如:尝试播放激励视频时,会直接跳过视频并作为已完成处理等。身为开发者的我们可能需要做一些处理,禁止pc的微信访问小程序,以达到减小损失的目的。
提要
App onLaunch时,通过wx.getSystemInfo
接口获取设备信息,判定设备异常(不是ios、android、devtools),则触发死循环代码,阻止小程序正常运行。
代码
app.js
中:
配置:
const normalPlatformList = ['ios','android','devtools'],
获取设备信息:
let self = this
wx.getSystemInfo({
success: res => {
console.log('设备信息', res)
self.tryToReportAbnormalDevice(res)
},
fail(e) {
console.error(e)
wx.showToast({
title: '获取设备信息失败',
icon: 'none',
})
},
死循环方法:
tryToReportAbnormalDevice(res) {
if (!this.isNormalDevice(res)) {
while (true) {
console.error('error')
}
}
},
判定设备是否异常的方法:
isNormalDevice(deviceInfo) {
return normalPlatformList.includes(deviceInfo.platform)
},
核心代码如上,项目中我的normalPlatformList
是抽离了放在项目的config中的,为了阅读方便提取了出来。
三段代码如何组织看各位喜好,后两段我是直接挂在了App下,第一段是插在了App下一个公用的获取设备信息的方法内。