收藏
回答

关于授权登录的问题

第一次授权失败,


紧接着第二次授权然后就成功了,需要点两次授权按钮才行,但是这种情况又不是一直出现,如果短时间内授权过,重新授权一次就成功,如果隔一段时间重新授权,需要点两次


<view class="container">
    <form bindsubmit="bindSave">
    <image class="logo" src="/images/wx.png" mode="widthFix" />
    <view class="title">微信授权页面</view>
    <view class="profile">授权并同意使用微信账号登录当前小程序</view>
    <button type="primary" open-type="getUserInfo" bindgetuserinfo="bindGetUserInfo" class="weui-btn mini-btn">授权登录</button>
    </form>
</view>


bindGetUserInfo: function (e) {
    //console.log(e);
    if (e.detail.errMsg == 'getUserInfo:ok') {
      this.setData({
        encryptedData: e.detail.encryptedData,
        iv: e.detail.iv
      })
      wx.showLoading({
        title: '请稍后',
        duration: 2000
      })
      this.userlogin();//尝试再次登陆
    }
  },
//登录
  userlogin: function () {
    var that = this;
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
          wx.getUserInfo({
            success: res => {
              //console.log(res)
              wx.setStorageSync('nickName', res.userInfo.nickName);
              wx.setStorageSync('headpic', res.userInfo.avatarUrl);
              var en = res.encryptedData;
              var iv = res.iv;
              wx.login({
                success: r => {
                  var that = this;
                  if (r.code) {
                    //发起网络请求
                    //console.log(r.code);
                    util.req('/login', {
                      "code": r.code,
                      "encryptedData": en,
                      "iv": iv
                    }, function (rs) {
                      console.log(rs)
                      if (rs.code == 0) {
                        wx.setStorageSync('openid', rs.data.openid);
                        wx.setStorageSync('hyid', rs.data.id);//会员ID
                        wx.navigateBack({
                          delta: 1
                        })
                      }
                    })
                  } else {
                    wx.showToast({
                      title: '登录失败!',
                    })
                    // console.log('登录失败!' + r.errMsg)
                  }
                }
              })
            }
          })
        }
      }
    })
  },


回答关注问题邀请回答
收藏

1 个回答

  • 疯狂的小辣椒
    疯狂的小辣椒
    2019-07-15

    你好,麻烦提供出现问题的具体机型、微信版本号、系统版本号,以及能复现问题的代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)

    2019-07-15
    有用
    回复
登录 后发表内容