微信小程序用戶拒絕授權的官方推薦處理方法

小程序開發中,現在一般都需要獲取微信用戶信息,如頭像/名字等.這樣在用戶第一次進入小程序時,微信端會彈出一個是否同意授權的消息提示框.但是如果用戶第一時間點擊了拒絕,或者用戶手誤點擊了拒絕,如果沒有了後續的操作,可能你的小程序就不能使用了,也就會失去這樣一位用戶.所以,微信官方推薦了一個方法,就是在用戶第一次拒絕授權的時候,再給用戶一個選擇的機會.這樣能很好的解決上訴問題.下面以用戶需要授權兩個權限爲例,方法如下:

在 APP.JS 先設置兩個全局變量 .用作記錄用戶是否授權

//判斷地理位置是否請求成功
var locationBool;
//判斷用戶信息是否請求成功
var userInfoBool;

的 APP({})中

1.獲取用戶當前位置信息(獲取成功後將數據存入緩存,方便後面使用)

    //獲取地理位置
    wxGetlocation: function() {
        console.log('微信獲取地理')
        wx.getLocation({
            type: 'wgs84',
            //請求成功
            success: function(res) {
                locationBool = true;
                wx.setStorage({
                    key: 'locationWx',
                    data: {
                        applatitude: res.latitude,
                        applongitude: res.longitude
                    }
                })
            },
            fail: function() {
                locationBool = false;
            }
        })
    },

2.獲取用戶資料信息:

這裏需要注意的是:  現在一般開發小程序都需要做服務器登錄.由於後端的解密方式的選擇問題,後端可以只用用戶code 解密,也有可能需要用到 encryptedData/iv 這兩個參數拿去後臺解密,換取用戶token 來做更多的操作. 如需獲取encryptedData/iv 這兩個參數, 必須在微信wx.login({})之後再去獲取 否則這兩個參數可能會出現問題

    //獲取用戶信息(獲取之前必須login)
    wxGetUserInfo: function() {
        console.log('微信獲取用戶信息')
        wx.getUserInfo({
            //請求成功
            withCredentials: true,
            success: function(data) {
                userInfoBool = true;
                wx.setStorage({
                    key: 'UserInfoWx',
                    data: data
                })
            },
            fail: function() {
                userInfoBool = false;
            }
        })
    },
    logInWx: function() {
        var _this = this;
        wx.login({
            success: res => {
                console.log('微信登錄')
                console.log(res)
                //如果登錄成功
                var usercode = res.code; //用戶code
                if (res.code) {
                    wx.setStorage({
                        key: 'usercode',
                        data: usercode
                    })                    
                    //請求用戶信息設置緩存(獲得nickname/avatarurl/gender/iv/encryptedData)
                    _this.wxGetUserInfo();
                }
            }
        })
    }

在小程序中,微信提供了這樣一個方法wx.openSetting({}) 他可以彈出小程序設置授權的頁面.下面即是 用戶拒絕授權之後的處理方法.

    getPromission: function() {
        var _this = this;
        // 位置信息,用戶信息中其中一個數據沒有獲取到(--->彈出設置界面)
        if (locationBool == false || userInfoBool == false) {
            // 顯示提示彈窗(重新授權)
            wx.showModal({
                title: '用戶未授權',
                content: '請開啓相應的權限哦~',
                showCancel: false,
                success: res => {
                    if (res.confirm) {
                        //點擊取消,重新獲取授權
                        wx.openSetting({
                            success: data => {
                                console.log(data)
                                if (data) {
                                    if (data.authSetting["scope.userInfo"] == true && data.authSetting["scope.userLocation"] == false) {
                                        //再次獲取用戶信息
                                        console.log('只獲取用戶信息')
                                        _this.wxGetUserInfo()
                                    } else if (data.authSetting["scope.userInfo"] == false && data.authSetting["scope.userLocation"] == true) {
                                        //再次獲取位置信息
                                        _this.wxGetlocation()
                                        console.log('只獲取位置信息')
                                    } else if (data.authSetting["scope.userInfo"] == true && data.authSetting["scope.userLocation"] == true) {
                                        //再次獲取用戶和位置信息
                                        _this.wxGetUserInfo()
                                        _this.wxGetlocation()
                                        console.log('獲取全部信息')
                                    }
                                }
                            },
                            fail: function() {
                                console.info("設置頁面失敗");
                            }
                        });
                    }
                }
            });
        }
    },

然後在app.js onlaunch中

    onLaunch: function(e) {
        var _this = this
        _this.wxGetlocation();
        //微信登錄
        //如果沒有獲取數據成功.
        var timer = setInterval(function() {
            if (locationBool != undefined && userInfoBool != undefined) {
                clearInterval(timer)
                _this.getPromission();
            }
        }, 200)   
    },


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章