微信小程序获得微信头像和昵称

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

clipboard.png

微信小程序开发中,获取用户头像昵称的方法已经发生了变化。以前常用的方法如wx.getUserInfo和wx.getUserProfile已经不再支持获取用户头像昵称的功能。现在,为了保护用户隐私和提高用户体验,微信小程序需要通过用户授权才能获取头像昵称等敏感信息。 目前,获取用户头像昵称的常用方法是通过使用button组件,并设置open-type属性为getUserProfile来获取用户的头像昵称。当用户点击这个button时,会弹出授权框,用户可以选择是否授权给小程序获取头像昵称信息。一旦用户授权,就可以通过event.detail.userInfo获取到用户的头像昵称。 需要注意的是,为了保护用户隐私,微信小程序获取用户头像昵称的权限进行了限制。开发者需要在小程序管理后台进行相应的配置,包括设置业务域名和配置授权设置等。具体的配置步骤可以参考微信小程序的官方文档。 总结起来,微信小程序开发获取用户头像昵称的方法已经发生了变化,现在需要使用button组件并设置open-type属性为getUserProfile来获取用户授权,通过event.detail.userInfo来获取用户的头像昵称。但需要注意在小程序管理后台进行相应的配置以及保护用户隐私的要求。123 #### 引用[.reference_title] - *1* *2* [微信小程序获取用户头像昵称](https://blog.csdn.net/m0_61443432/article/details/130257744)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}} ] [.reference_item] - *3* [微信小程序授权获取头像昵称的最新形式——头像昵称填写](https://blog.csdn.net/qq_38970408/article/details/127754307)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值