微信小程序授权管理
options = { scope: 'scope.record', content: '使用您的麦克风' }
scope
微信小程序权限管理
https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/authorize.html
content
已经关闭授权了 弹窗提示重新打开授权
getSettingRecord(options) {
var self = this;
return new Promise((resolve, reject) => {
wx.getSetting({
success: (res) => {
let auth = res.authSetting[options.scope]
console.warn('scope.record=', auth, typeof auth)
if (auth === true) { // 用户已经同意授权
resolve(true)
}
else if (auth === undefined) {// 首次发起授权
wx.authorize({
scope: options.scope,
success() {
resolve(true)
},
fail(res) {
}
})
}
else if (auth === false) { // 非首次发起授权,用户拒绝过 => 弹出提示对话框
wx.showModal({
title: '授权提示',
content: options.content,
success: (tipRes) => {
if (tipRes.confirm) {
wx.openSetting({
success: (settingRes) => {
if (settingRes.authSetting[options.scope]) {
resolve(true)
}
console.warn('settingRes', settingRes)
},
})
}
}
})
}
},
})
})
},
案例 麦克风
microphone(){ return new Promise((resolve, reject) => {
//权限设置 var microphone = { scope: 'scope.record', content: '使用您的麦克风' }
//调用权限管理设置 var promise1 = this.getSettingRecord(microphone) Promise.all([promise1]).then(res => { console.warn('Promise.all', res); if (res[0] && res[1]) { console.warn('获取权限成功')
wx.showToast({ title: '获取麦克风', icon: 'none' }) resolve(); } }) }) }