微信小程序獲得微信頭像和暱稱

{
  wx.login({
    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;
    },

clipboard.png

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