微信小程序授权管理

复制代码
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(); } }) }) }

 
复制代码

 

posted @   web格调  阅读(428)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 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
点击右上角即可分享
微信分享提示