美文网首页
小程序内授权地理位置、相册等

小程序内授权地理位置、相册等

作者: 银河系小前端 | 来源:发表于2019-10-02 10:50 被阅读0次

    换了工作,一直在加班,好不容易放假了,终于有机会写写自己开发时踩的坑了。

    开发过程中,我发现小程序如果第一次授权位置的时候选择了拒绝,就再也无法拉起原生的授权对话框了,后来才知道,可以用wx.modal引导用户拉起设置页面,然后手动授权。

    首先定义一个公共方法,用来检查是否授权过地理位置,我是写在app.js里面的,

     //   检查有没有授权相应的功能
        getSetting: function (authName, onSuccess, onFail = {}) {
            wx.getSetting({
                success: res => {
                    const authCheck = res.authSetting[authName];
                    if (typeof authCheck === 'undefined' || authCheck) {
                        onSuccess()
                    } else {
                        console.log('没有授权,使用modal调起授权===========')
                        this.modalToSetting(authName, onSuccess, onFail)
                    }
                },
                fail: res => {
                    console.log('appgetsettin    失败=======', res)
                }
            })
        },
    

    这个里面判断如果授权项是undefined或者授权成功,则执行成功的回调,可能有的朋友要问了,undefined为什么要执行成功的回调呢?如果从来没有授权过地理位置,那么你在设置页面是看不到地理位置这一项的,你可以直接执行微信的获取位置的方法,拉起微信原生的授权对话框。如果授权项有,并且是失败的,就需要wx.modal引导用户打开设置页面,这个方法我也是写在app里的:

    // 使用modal再次唤起授权提示
        modalToSetting: function (authName, onSuccess, onFail) {
            let tip = '需要您的授权'
            if (authName.indexOf('writePhotosAlbum') > -1) {
                tip = '需要您授权相册'
            } else if (authName.indexOf('userLocation') > -1) {
                tip = '需要您授权位置信息'
            }
            wx.showModal({
                title: '授权提示',
                content: tip,
                success: res => {
                    if (res.confirm) {
                        wx.openSetting({
                            success: res => {
                                if (res.authSetting[authName]) {
                                    console.log('授权成功,执行回调')
                                    onSuccess && onSuccess();
                                } else {
                                    wx.showToast({
                                        title: '获取权限失败',
                                        icon: 'none',
                                    });
                                }
                            }
                        })
                    } else {
                        onFail && onFail();
                        console.log('二次授权拒绝');
                    }
                }
            })
        },
    

    接下来就是在页面中使用了,直接执行checkSetting方法:

    // 检查授权情况
            checkSetting: function() {
                const that = this
                app.getSetting('scope.userLocation', 
                    function() {
                        console.log('--------------检查地理位置授权,成功,开始获取定位-----------');
                        that.getLocation();
                        that.chooseLocation();
                    },
                    function(){}
                )
            },
    
    // 调用微信接口,获取当前坐标
            getLocation: function() {
                let that = this
                wx.getLocation({
                    success: res => {
                        console.log('当前位置坐标:',res)
                        this.setData({
                            latitude: res.latitude,
                            longitude: res.longitude
                        })
                    },
                    fail: res => {
                        console.log('getlocation接口调用失败:')
                        that.checkSetting()
                    }
                })
            },
    // 在地图上选择位置
            chooseLocation: function() {
                let that = this
                wx.chooseLocation({
                    success: function(res){
                        console.log('商家选择的地址=======', res)
                        that.setData({
                            location: res
                        })
                    },
                    fail: function(res) {
                        console.log('选择地图失败===', res)
                        that.checkSetting()
                    }
                })
            },
    

    就是这么简单啦,如果要获取相册授权,也是一样的,把授权名称改一下就好了。

    完整代码请大家移步我的GitHub

    相关文章

      网友评论

          本文标题:小程序内授权地理位置、相册等

          本文链接:https://www.haomeiwen.com/subject/ctwqyctx.html