微信小程序之登錄態的探索
{
wx.getSetting({
success: res => {
if (res.authSetting && res.authSetting['scope.userInfo']) {
// 已經授權,可以直接調用 getUserInfo 獲取頭像昵稱
wx.getUserInfo({
success: function (data) {
console.log(data.userInfo);
// {
// avatarUrl: '微信頭像img文件path'
// nickname: '微信昵稱'
// }
}
});
} else {
this.showShouquan = true; // 打開模態框進行授權
}
}
});
}
<template>
<div>
<!-- 這里采用vantui進行彈框,只不過把彈出框的button類型設置成了"getUserInfo",原理就是下面這個button
<button wx:if="{{showShouquan}}" open-type="getUserInfo" bindgetuserinfo="getUserInfoFromWx">授權登錄</button> -->
<van-dialog use-slot async-close :show="showShouquan" show-cancel-button confirm-button-open-type="getUserInfo"
@close="cancelShouquan" @getuserinfo="getUserInfoFromWx">
<h4 class="wx-shouquan-title">微信授權</h4>
<p class="wx-shouquan-content">
xxxxxxx小程序將獲得您的微信頭像、昵稱等公開消息
</p>
</van-dialog>
<div>
</template>
getUserInfoFromWx (e) {
this.$store.state.user_userinfo = e.mp.detail.userInfo;
this.showShouquan = false;
},
cancelShouquan () {
this.showShouquan = false;
},

來源:https://segmentfault.com/a/1190000017496137