//判断是否有相应权限,true则直接执行,false需要跳转设置页
//name取值例:
/*scope.userInfo wx.getUserInfo 用户信息
scope.userLocation wx.getLocation, wx.chooseLocation 地理位置
scope.address wx.chooseAddress 通讯地址
scope.invoiceTitle wx.chooseInvoiceTitle 发票抬头
scope.invoice wx.chooseInvoice 获取发票
scope.werun wx.getWeRunData 微信运动步数
scope.record wx.startRecord 录音功能
scope.writePhotosAlbum wx.saveImageToPhotosAlbum, wx.saveVideoToPhotosAlbum 保存到相册
scope.camera <camera /> 组件 摄像头
*/
getAuth:function(name,callback){
wx.getSetting({
success(res) {
if (res.authSetting[‘scope.’ + name] != undefined && res.authSetting[‘scope.’ + name] != true) {
//用户主动取消过
callback(false);
} else if (res.authSetting[‘scope.’ + name] == undefined) {
//第一次向用户获取
callback(true);
} else {
//用户已授权
callback(true);
}
}
})
},
//以上是我将所有权限进行了封装在app.js里面,下面举个例(获取微信步数)调用:
const app=getApp();
app.getAuth(‘werun’, function (res) {
if (res)
wx.getWeRunData({
success(res) {
//这个里面是表示成功直接处理业务
// 拿 encryptedData 到开发者后台解密开放数据
const encryptedData = res.encryptedData
// 或拿 cloudID 通过云调用直接获取开放数据
const iv = res.iv
console.log(res)
that.getRunData(encryptedData, iv)//这个请无视,业务函数
}
})
else
wx.showModal({
title: ‘获取权限’,
content: ‘请前往开启微信步数权限’,
success: function (res) {
if (res.confirm) {
wx.openSetting()
} else if (res.cancel) {
console.log(‘用户点击取消’)
}
}
})
})