小程序功能(一)自动授权登录

**注意:一定要先写wx.getSetting再写wx.login否则会出现IllegalIv变量未定义情况

##html
//小程序授权必须是按钮(点击授权)
<button
    class="authorized-collection"
    open-type="getUserInfo"
    lang="zh_CN"
    @getuserinfo="bindGetUserInfo"
>授权领取礼包</button>
##js
  bindGetUserInfo(e) {
      console.log(e);
       if (e.mp.detail.userInfo) {
            //用户按了允许授权按钮
            var that = this;
            // 获取到用户的信息了,打印到控制台上看下
            console.log("用户的信息如下:");
             that.isLogin=false;
             that.isLogins=true;
            console.log(e.mp.detail.userInfo);
            that.getToken();
            //授权成功后,通过改变 isHide 的值,让实现页面显示出来,把授权页面隐藏起来
        } else {
            //用户按了拒绝按钮
            wx.showModal({
                title: '警告',
                content: '您点击了拒绝授权,将无法进入小程序,请授权之后再进入!!!',
                showCancel: false,
                confirmText: '返回授权',
                success: function(res) {
                    // 用户没有授权成功,不需要改变 isHide 的值
                    if (res.confirm) {
                        console.log('用户点击了“返回授权”');
                    }
                }
            });
        }

    },
 getToken() {
      var that = this;
       var iv = "";
      var encryptedData = "";
        wx.getSetting({
            success: function(res) {
                if (res.authSetting['scope.userInfo']) {
                    wx.getUserInfo({
                        success: function(res) {
                           that.isLogin = false;
                           that.isLogins = true;
                           var userInfo = res.userInfo;
                              encryptedData = res.encryptedData;
                              iv = res.iv;
                              var nickName = userInfo.nickName;
                              var avatarUrl = userInfo.avatarUrl;
                              var gender = userInfo.gender; //性别 0:未知、1:男、2:女
                              var province = userInfo.province;
                              var city = userInfo.city;
                              var country = userInfo.country;
                            // 用户已经授权过,不需要显示授权页面,所以不需要改变 isHide 的值
                            // 根据自己的需求有其他操作再补充
                            // 我这里实现的是在用户授权成功后,调用微信的 wx.login 接口,从而获取code
                            wx.login({
                                success: res => {
                                    // 获取到用户的 code 之后:res.code
                                    console.log("用户的code:" + res.code);
                                     if (res.code) {
                                      loginH5({
                                        code: res.code,
                                          iv: iv,
                                        encryptedData:  encryptedData
                                      }).then(res => {
                                        if (res.code == 200) {
                                         
                                          wx.setStorageSync("token", res.data);
                                         
                                        } else {
                                          //重新获取数据
                                          that.getToken();
                                        }
                                      });
                                    } 
                                   
                                    // 可以传给后台,再经过解析获取用户的 openid
                                    // 或者可以直接使用微信的提供的接口直接获取 openid ,方法如下:
                                    // wx.request({
                                    //     // 自行补上自己的 APPID 和 SECRET
                                    //     url: 'https://api.weixin.qq.com/sns/jscode2session?												appid=自己的APPID&secret=自己的SECRET&js_code=' + 											res.code + '&grant_type=authorization_code',
                                    //     success: res => {
                                    //         // 获取到用户的 openid
                                    //         console.log("用户的openid:" + res.data.openid);
                                    //     }
                                    // });
                                }
                            });
                        }
                    });
                } else {
                   that.isLogin = true;
                    // 用户没有授权
                    // 改变 isHide 的值,显示授权页面
                   
                }
            }
        });

      
    }
  • 18
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值