為您解碼網(wǎng)站建設(shè)的點(diǎn)點(diǎn)滴滴
發(fā)表日期:2019-11 文章編輯:小燈 瀏覽次數(shù):12552
微信小程序授權(quán)登錄基本是小程序的標(biāo)配了,但是官方的demo,取消授權(quán)后,就不能再重新點(diǎn)擊登錄,除非重新加載小程序才可以,這下怎么辦?
我們可以先在首頁引導(dǎo)用戶點(diǎn)擊,然后跳轉(zhuǎn)到一個(gè)新的頁面,在新的頁面進(jìn)行授權(quán),然后新的頁面授權(quán)成功,立馬跳回首頁,顯示用戶信息。
代碼結(jié)構(gòu):
index是首頁
login是授權(quán)頁
index.wxml
<!-- 未授權(quán),只顯示一個(gè)授權(quán)按鈕 -->
<view wx:if="{{result==false}}">
<button bindtap="getinfo" class="loginbtn"> 授權(quán)登錄 </button>
</view>
<!-- 授權(quán)后只顯示頭像和昵稱 -->
<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
},
//事件處理函數(shù)
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("已授權(quán)")
// 已經(jīng)授權(quán),可以直接調(diào)用 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: "取消授權(quán)"
})
}
})
} else {
console.log("未授權(quán)")
that.setData({
result: false
})
}
}
})
}
})
index.wxml
<!--index.wxml-->
<button open-type="getUserInfo" bindgetuserinfo="getUserInfo"> 授權(quán)獲取用戶信息 </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("已授權(quán)=====")
// 已經(jīng)授權(quán),可以直接調(diào)用 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("未授權(quán)=====")
}
}
})
}
})
WeChat:face6009
Web:http:likeyunba.com
Date:2019-10-17
Author:TANKING
日期:2019-11 瀏覽次數(shù):5690
日期:2019-11 瀏覽次數(shù):12138
日期:2019-11 瀏覽次數(shù):4514
日期:2019-11 瀏覽次數(shù):5554
日期:2019-11 瀏覽次數(shù):5394
日期:2019-11 瀏覽次數(shù):7411
日期:2019-11 瀏覽次數(shù):5313
日期:2019-11 瀏覽次數(shù):15942
日期:2019-11 瀏覽次數(shù):4884
日期:2019-11 瀏覽次數(shù):6688
日期:2019-11 瀏覽次數(shù):5558
日期:2019-11 瀏覽次數(shù):4724
日期:2019-11 瀏覽次數(shù):11017
日期:2019-11 瀏覽次數(shù):8520
日期:2019-11 瀏覽次數(shù):5235
日期:2019-11 瀏覽次數(shù):4483
日期:2019-11 瀏覽次數(shù):9147
日期:2019-11 瀏覽次數(shù):4816
日期:2019-11 瀏覽次數(shù):5012
日期:2019-11 瀏覽次數(shù):5023
日期:2019-11 瀏覽次數(shù):4687
日期:2019-11 瀏覽次數(shù):5177
日期:2019-11 瀏覽次數(shù):10462
日期:2019-11 瀏覽次數(shù):5616
日期:2019-11 瀏覽次數(shù):5623
日期:2019-11 瀏覽次數(shù):5061
日期:2019-11 瀏覽次數(shù):12551
日期:2019-11 瀏覽次數(shù):7531
日期:2019-11 瀏覽次數(shù):8101
日期:2019-11 瀏覽次數(shù):5020
Copyright ? 2013-2018 Tadeng NetWork Technology Co., LTD. All Rights Reserved.