微信小程序授權登錄取消授權重新授權處理方法 附可用代碼

微信小程序授權登錄基本是小程序的標配了,但是官方的demo,取消授權後,就不能再重新點擊登錄,除非重新加載小程序纔可以,這下怎麼辦?

我們可以先在首頁引導用戶點擊,然後跳轉到一個新的頁面,在新的頁面進行授權,然後新的頁面授權成功,立馬跳回首頁,顯示用戶信息。

GIF.gif

話不多說,直接上代碼

代碼結構:
image.png

index是首頁
login是授權頁

首頁代碼

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.wxss

/**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.js

//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.wxml

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

index.js

//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:TANKING

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章