微信授权与拒绝授权的弹窗处理

说明:微信授权页面一经用户拒绝过一次后后续就不会再弹出 只能手动点击右上角打开设置去授权,此方法解决了不需要用户手动打开设置授权 只要用户拒绝过授权 下次进来就会以弹窗的形式询问是否需要授权 是的话自动跳转到设置页面

1. 新建公共的js文件 commonAuth.js



let setting = null;
const dealAuth = (authSetting, key, content, callback) => {
    if (authSetting?.[key] === true) callback?.(true)
    if (authSetting?.[key] === false) {
        wx.showModal({
            title: '提示',
            content,
            success(res) {
                if (res.confirm) {
                    wx.openSetting({
                        withSubscriptions: true,
                        success(res) {
                            res.authSetting[key] && callback?.(true)
                            !res.authSetting[key] && callback?.(false)
                        },
                        // fail() { callback?.(false) }
                    })
                } else if (res.cancel) callback?.(false)
            }
        })
    }
    if (authSetting?.[key] === undefined) {
        wx.authorize({
            scope: key,
            success() { callback?.(true) },
            fail() { callback?.(false) }
        })
    }
}
module.exports.commonAuth = (key, content, callback) => {
    !setting && wx.getSetting({
        success(res) {
            setting = res
            dealAuth(res.authSetting, key, content, callback)
        }
    })
    setting && dealAuth(setting.authSetting, key, content, callback)
}

2. 使用方式

const  { commonAuth } = require('@/common/utils/commonAuth')

commonAuth('scope.record', '打开录音授权页面吗?', (result) => {
// result true授权成功 可以使用对应api  false拒绝授权
    // 授权打开后开始调用相关api
        wx.startRecord()
 })
  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值