在用uni-app开发小程序的时候我们经常会遇到,需要授权的时候,用户没有点击确认按钮,然后用户再次点击的时候,系统默认执行失败回调函数,权限获取无法再次被挂起。

1、授权登录

先调取uni.getUserProfile  设置desc: 'Wexin'

再调去uni.login

然后用返回的code码向接口换取open-id

再用open-id向接口请求userid完成登录

2、获取麦克风授权

直接上一个示例点击函数

startRecord() {
                this.ti = '00:00'
                this.minutes = 0
                this.seconds = 0;
                recorderManager.stop();
                // console.log('开始录音');
                let that = this
                uni.authorize({
                    scope: 'scope.record',
                    success() { //1.1 允许授权
                        that.opening = false,
                            that.closeing = true,
                            that.timer = setInterval(that.startTimer, 1000),
                        recorderManager.start()
                    },
                    fail() { //1.2 拒绝授权
                        uni.showModal({
                            content: '检测到您没打开录音功能权限,是否去设置打开?',
                            confirmText: "确认",
                            cancelText: '取消',
                            success: (res) => {
                                if (res.confirm) {
                                    uni.openSetting({
                                        success: (res) => {
                                            // console.log(res.authSetting);
                                            if (res.authSetting == true) {
                                                that.opening = false,
                                                    that.closeing = true,
                                                that.timer = setInterval(that.startTimer,
                                                        1000),
                                                    recorderManager.start()
                                            } else {
                                                // console.log("什么也不做");
                                            }

                                        }
                                    })
                                } else {
                                    // console.log('取消');
                                    return false;
                                }
                            }
                        })
                        return false;
                    }
                })
            },

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐