微信小程序受權登陸取消受權從新受權處理方法 附可用代碼

微信小程序受權登陸基本是小程序的標配了,可是官方的demo,取消受權後,就不能再從新點擊登陸,除非從新加載小程序才能夠,這下怎麼辦?小程序

咱們能夠先在首頁引導用戶點擊,而後跳轉到一個新的頁面,在新的頁面進行受權,而後新的頁面受權成功,立馬跳回首頁,顯示用戶信息。微信小程序

GIF.gif

話很少說,直接上代碼

代碼結構:
image.png微信

index是首頁
login是受權頁xss

首頁代碼

index.wxml函數

<!-- 未受權,只顯示一個受權按鈕 -->
<view wx:if="{{result==false}}">
  <button bindtap="getinfo" class="loginbtn"> 受權登陸 </button>
</view>

<!-- 受權後只顯示頭像和暱稱 -->
<view elif="{{result==true}}" class="info">
  <image src="{{head}}" class="headimg"></image>
  <text class="nickname">{{name}}</text>
</view>

index.wxssthis

/**index.wxss**/
.loginbtn{
  width: 150px;
  height: 45px;
  background: #06C05F;
  margin:100px auto 0;
  line-height: 45px;
  font-size: 15px;
  color: #fff;
}

.info{
  width: 80px;
  height: 100px;
  margin:50px auto 0;
}

.info .headimg{
  width: 80px;
  height: 80px;
  border-radius: 100%;
}

.info .nickname{
  text-align: center;
}

index.jsurl

//index.js
Page({
  data: {
    userInfo: {},
    hasUserInfo: false
  },

  //事件處理函數
  getinfo: function () {
    wx.navigateTo({
      url: '../login/index'
    })
  },

  onLoad: function (e) {
    let that = this;
    // 獲取用戶信息
    wx.getSetting({
      success(res) {
        // console.log("res", res)
        if (res.authSetting['scope.userInfo']) {
          console.log("已受權")
          // 已經受權,能夠直接調用 getUserInfo 獲取頭像暱稱
          wx.getUserInfo({
            success(res) {
              console.log("獲取用戶信息成功", res)
              that.setData({
                name: res.userInfo.nickName,
                head: res.userInfo.avatarUrl,
                result: true
              })
            },
            fail(res) {
              console.log("獲取用戶信息失敗", res)
              that.setData({
                result: "取消受權"
              })
            }
          })
        } else {
          console.log("未受權")
          that.setData({
            result: false
          })
        }
      }
    })
  }
})

受權頁代碼

index.wxmlspa

<!--index.wxml-->
<button open-type="getUserInfo" bindgetuserinfo="getUserInfo"> 受權獲取用戶信息 </button>

index.jscode

//index.js
Page({
  data: {
    userInfo: {},
    hasUserInfo: false
  },

  getUserInfo: function (e) {
    let that = this;
    // 獲取用戶信息
    wx.getSetting({
      success(res) {
        // console.log("res", res)
        if (res.authSetting['scope.userInfo']) {
          console.log("已受權=====")
          // 已經受權,能夠直接調用 getUserInfo 獲取頭像暱稱
          wx.getUserInfo({
            success(res) {
              console.log("獲取用戶信息成功", res)
              that.setData({
                name: res.userInfo.nickName,
                head: res.userInfo.avatarUrl
              })
              wx.reLaunch({
                url: '../index/index'
              })
            },
            fail(res) {
              console.log("獲取用戶信息失敗", res)
            }
          })
        } else {
          console.log("未受權=====")
        }
      }
    })
  }
})

不懂能夠諮詢我

WeChat:face6009
Web:http:likeyunba.com
Date:2019-10-17
Author:TANKINGxml

相關文章
相關標籤/搜索