微信小程序用户拒绝授权的处理方法详解

这篇文章主要介绍了微信小程序用户拒绝授权的处理方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

前言

小程序开发中,现在一般都需要获取微信用户信息,如头像/名字等.这样在用户第一次进入小程序时,微信端会弹出一个是否同意授权的消息提示框.但是如果用户第一时间点击了拒绝,或者用户手误点击了拒绝,如果没有了后续的操作,可能你的小程序就不能使用了,也就会失去这样一位用户.

所以,微信官方推荐了一个方法,就是在用户第一次拒绝授权的时候,再给用户一个选择的机会.这样能很好的解决上诉问题.下面以用户需要授权两个权限为例,方法如下:

在 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) },

以上就是微信小程序用户拒绝授权的处理方法详解的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » JavaScript 教程