uniapp处理用户拒绝权限请求

在使用用户位置时,需要用户提供权限,如果用户拒绝,那么进入fail()失败的回调函数中,
一般搭配uni.getSetting和uni.openSetting使用,官方文档中明确指出从2.3.0版本开始,当用户发生点击行为后才可以跳转打开设置页,不允许在用户无任何操作的情况下进行跳转,
报错 openSetting:fail can only be invoked by user TAP gesture.这种情况下需要添加一个弹出框 uni.showModal({}),通过提示框的按钮点击,触发opensetting

//这一段代码 复用率高可封装为函数来调用
    // 获取用户当前位置 并打开地图
                uni.chooseLocation({
                    success: res=>{
                        // 返回用户当前选择的位置信息;
                        // 可进行保存data 回显在表单上/提交后台
                        if (state === 'start') {
                            console.log(this.formDate)
                            this.formDate.startDestination = res.name
                        } else {
                            this.formDate.endDestination = res.name
                        };
                        console.log('位置名称:' + res.name);
                        console.log('详细地址:' + res.address);
                        console.log('纬度:' + res.latitude);
                        console.log('经度:' + res.longitude);
                    }
                });

            mapPlace(state) {

                // uni.authorize
                // 提前向用户发起授权请求。
                // 调用后会立刻弹窗询问用户是否同意授权小程序使用某项功能或获取用户的某些数据
                
                // #ifdef H5
                // 获取用户当前位置 并打开地图
                uni.chooseLocation({
                    success: res=>{
                        // 返回用户当前选择的位置信息;
                        // 可进行保存data 回显在表单上/提交后台
                        if (state === 'start') {
                            console.log(this.formDate)
                            this.formDate.startDestination = res.name
                        } else {
                            this.formDate.endDestination = res.name
                        };
                        console.log('位置名称:' + res.name);
                        console.log('详细地址:' + res.address);
                        console.log('纬度:' + res.latitude);
                        console.log('经度:' + res.longitude);
                    }
                });

                //#endif    
                // #ifdef MP-WEIXIN

                uni.authorize({
                            scope: 'scope.userLocation',
                            // 成功
                            success() {
                                // 1 用户允许授权
                                // 2 用户之前已经同意授权,则不会出现弹窗,直接返回成功
                                //  以上两种情况都会进入到success回调中

                                // 获取用户当前位置 并打开地图
                                uni.chooseLocation({
                                    success: res => {
                                        // 返回用户当前选择的位置信息;
                                        // 可进行保存data 回显在表单上/提交后台
                                        if (state === 'start') {
                                            this.formDate.startDestination = res.name
                                        } else {
                                            this.formDate.endDestination = res.name
                                        };
                                        console.log('位置名称:' + res.name);
                                        console.log('详细地址:' + res.address);
                                        console.log('纬度:' + res.latitude);
                                        console.log('经度:' + res.longitude);
                                    }
                                });
                            },
                            // 失败
                            fail() {
                                // 1 用户拒绝授权
                                // 2 用户之前拒绝了授权,此接口会直接进入失败回调
                                //  以上两种情况都会进入到fail回调中
                                // 一般搭配uni.getSetting和uni.openSetting使用

                                // 官方文档中明确指出从2.3.0版本开始,当用户发生点击行为后才可以跳转打开设置页,不允许在用户无任何操作的情况下进行跳转
                                // so报错 openSetting:fail can only be invoked by user TAP gesture.

                                // 解决没办法 可通过提示框的按钮点击 触发opensetting
                                //弹出框
                                    uni.showModal({
                                        title: '提示',
                                        content: '您拒绝了位置的授权,不可使用地图选择功能',
                                        showCancel:false,
                                        success: function (res) {
                                            //打开设置
                                            uni.openSetting({
                                              success(res) {
                                                let userLocation = res.authSetting['scope.userLocation'];
                                                if(userLocation){
                                                    // 继续进行授权成功后的操作
                                                    //用户开启位置权限
                                                    uni.chooseLocation({
                                                        success: res => {
                                                            // 返回用户当前选择的位置信息;
                                                            // 可进行保存data 回显在表单上/提交后台
                                                            if (state === 'start') {
                                                                this.formDate.startDestination = res.name
                                                            } else {
                                                                this.formDate.endDestination = res.name
                                                            };
                                                            console.log('位置名称:' + res.name);
                                                            console.log('详细地址:' + res.address);
                                                            console.log('纬度:' + res.latitude);
                                                            console.log('经度:' + res.longitude);
                                                        }
                                                    });
                                                }else{
                                                    // 用户拒接授权 给提示
                                                    uni.showToast({
                                                        title: '您已拒绝位置授权',
                                                        duration: 2000
                                                    });
                                                }
                                              }
                                            });
                                        }
                                    });
                                    
                                }
                            })
                        //#endif
                    },

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容