微信小程序授权登录获取用户信息流程

1、在app.js中获取用户的openid

示例代码:

  onLaunch: function () {
    //调用API从本地缓存中获取数据
    // var logs = wx.getStorageSync('logs') || []
    // logs.unshift(Date.now())
    // wx.setStorageSync('logs', logs);
    //login
    this.getUserInfo();
  },
  getUserInfo:function(cb){
    var that = this
      //调用登录接口
      wx.login({
        success: function (res) {
          var code = res.code;
          that.getUserSessionKey(code);
        }
      });
  },

  getUserSessionKey:function(code){
    //用户的订单状态
    var that = this;
    wx.request({
      url: that.d.ceshiUrl + '/Api/Login/getsessionkey',
      method:'post',
      data: {
        code: code
      },
      header: {
        'Content-Type':  'application/x-www-form-urlencoded'
      },
      success: function (res) {    
        var data = JSON.parse(res.data);
        if(data.status==0){
          wx.showToast({
            title: data.err,
            duration: 2000
          });
          return false;
        }
        that.globalData.userInfo = [];
        that.globalData.userInfo['sessionId'] = data.session_key;
        that.globalData.userInfo['openid'] = data.openid;
        // that.setData({
        //    session_key:data.session_key,
        //    openid:data.openid
        // });
        // that.onLoginUser();
      },
      fail:function(e){
        wx.showToast({
          title: '网络异常!err:getsessionkeys',
          duration: 2000
        });
      },
    });
  },

在登录授权的页面(新的修改的弹窗是需要自己去制作的)

wxml弹窗的内容

<!-- 获取微信授权信息 -->
<view hidden="{{denglu}}">
   <view class='shouquanBox'>
      <view class='sqTitle'>获取微信授权信息</view>
      <image src='{{imgUrl}}zyzLogo.jpg' class='mainLogo'></image>
      <view class='sqContent'>
         微信登录需要获取你的用户信息
      </view>
      <button class='sqSet' open-type="getUserInfo" bindgetuserinfo="bindGetUserInfo">授权登录</button>
   </view>
   <view class='blankWrap'></view>
</view>
<!-- end -->

js中的内容

在onload中查看是否授权

         if (res.authSetting['scope.userInfo']) {
              //获取页面的信息
          }else{
            //未授权情况也加载信息(加载页面的信息)
              wx.request({
                  url: app.globalData.base + '/Home/Index/index,
                  method: 'post',
                  data: {},
                  header: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                  },
                  success: function (res) {
                    wx.hideLoading();
                    that.setData({
                      denglu:false,(出现登录的弹窗)
                      pagePosition: "fixed",//是否允许页面滚动
                      pageHeight: "100%",//页面高度
                    });
                  },
                  fail: function (e) {
                    wx.showToast({
                      title: '网络异常!',
                      duration: 2000
                    });
                  },
                });
          }

   // 第一次微信授权登录
   bindGetUserInfo: function (e) {
    var that = this;
    //获取用户信息
    var userInfo = e.detail.userInfo;
    if(typeof(userInfo)==="undefined"){
      wx.showModal({
        title: '提示',
        content: '请授权登录才能使用小程序',
        success: function(res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
      return false;
    }
    //隐藏登录窗口
    that.setData({
             denglu:true
            });
             var data = {
                  code: res.code,
                  gender:userInfo.gender,
                  avatarUrl:userInfo.avatarUrl,
                  nickName:userInfo.nickName,
                  scene:scene
                };
              wx.request({
                //后台接口地址
                url: app.globalData.base + '/Home/Login/login',
                data: data,
                method: 'POST',
                header: {
                  'content-type': 'application/x-www-form-urlencoded'
                },
                success: function (res) {
                  if (res.data.status == '1') {
                    wx.setStorageSync('openId', res.data.openid);
                    wx.setStorageSync('user_id', res.data.user_id);
                    wx.request({
                    url: app.globalData.base + '/Home/Index/index?user_id='+res.data.user_id,
                    method: 'post',
                    data: {},
                    header: {
                      'Content-Type': 'application/x-www-form-urlencoded'
                    },
                    success: function (res) {
                      //添加用户信息成功
                    },
                    fail: function (e) {
                      wx.showToast({
                        title: '网络异常!',
                        icon:'success', 
                        duration: 2000, 
                        image: '../../img/tips.png',
                      });
                    },
                  });
                  } else {
                    wx.showToast({
                      title: '网络异常!',
                        icon:'success', 
                        duration: 2000, 
                        image: '../../img/tips.png',
                    });
                  }
                }
              });
        }
      }, 
      fail: function (res) {
          console.log(res);
        }
    })
   },

发布了93 篇原创文章 · 获赞 9 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/bj123467/article/details/84717594