栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

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

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

前言

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

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

在 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/i) {
     //点击取消,重新获取授权
     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)  
  },

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/73464.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号