{ 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; },