版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
最近这个小程序项目中要求用户授权摄像头和麦克风权限,这里就要处理一下权限的问题。如果用户第一次Taro.authorize授权弹框如果拒绝之后就没有在第二次弹框的出现了会报一个error错误。这里的处理办法有两种:通过设置button按钮的opentype=openSetting然后用户去手动操作;第二种是通过Taro.openSetting这个api来主动吊起微信的权限界面
代码
通过Taro.getSetting获取到用户的权限列表,如果是没有授权或拒绝过某一权限就调用Taro.authorize弹框授权,如果是拒绝过授权那么就调用Taro.openSetting进入上图的页面进行授权。
return new Promise((resolve) => {
Taro.getSetting({
success: (res) => {
if (res.authSetting[itemScope] === undefined) {
Taro.authorize({
scope: itemScope,
success () {
resolve()
}
})
} else if (res.authSetting[itemScope] === false) {
Taro.showModal({
title: '授权请求',
content: `尚未允许使用${text},请点击确认进行授权`,
success (e) {
if (e.confirm) {
Taro.openSetting({
success (e) {
if (e.authSetting[itemScope]) {
resolve()
}
}
})
}
}
})
}
else {
resolve()
}
}
})
})
上边的api基本都可以传入成功、错误、api调用结束的回调方法