微信小程序授权管理
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(); } }) }) }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
2021-04-07 前端计算精确度问题处理JS