以录音授权为例,对应授权修改scope即可,也可以封装成一个方法!
wx.getSetting({ // 获取当前授权信息
success(res) {
if (!res.authSetting['scope.record']) { // 判断是否授权录
wx.authorize({ // 弹出授权界面
scope: 'scope.record',
success(e) { // 用户同意授权功能,进行业务处理
console.log("authorize success", e)
// TODO TODO TODO ......
}, fail(e) {
if (e.errMsg.indexOf("-12006") >= 0) { // 首次授权拒绝后,第二次再授权会返回-12006,但是不会弹出授权界面!!!!
console.log("authorize fail and ready openSetting", e)
wx.showModal({ // 因为不会弹出授权界面,所以需要弹出设置界面,否则拒绝后,会一直授权不了!!!
content: '检测到您没打开此小程序的录音权限,是否去设置打开?',
confirmText: "确认",
cancelText: "取消",
success: function (res) {
console.log(res);
//点击“确认”时打开设置页面
if (res.confirm) {
console.log('openSetting')
wx.openSetting({ success: (res) => { } })
} else {
console.log('refuse openSetting')
}
}
});
} else {
// 用户首次拒绝授权
console.log("refuse authorize", e)
}
}
})
} else {
// 用户已授权,进行业务处理
console.log("authorized")
// TODO TODO TODO ......
}
}
});