小程序中新版本的获取用户头像与昵称:bind:chooseavatar

本文详细介绍了微信小程序中在官方规则调整后,如何获取用户头像和昵称,包括代码实现、步骤及注意事项,重点讲解了文件大小限制和状态管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言:

        自从微信官方把获取用户昵称与头像的功能改动以后,给我们开发和用户的操作又增加了很多负担,但是没办法,只能使用最新的使用方法了。

小程序用户头像昵称获取规则调整公告

新版实现效果:

注意,真机的效果,昵称会有所不同

 

新版使用步骤:

1、index.wxml

获取头像核心代码:

open-type="chooseAvatar" bind:chooseavatar="onChooseAvatar"
<view class="user-content">
     <button class="avatar-wrapper" open-type="chooseAvatar" bind:chooseavatar="onChooseAvatar"
        style="border-radius: {{needBorderRadius?'6px':'0'}};"
        >
       <image wx:if="{{avatarUrl}}" class="avatar-img" mode="aspectFill" src="{{avatarUrl}}"></image>
       <image wx:else class="default-avatar-img" mode="widthFix" src="{{defaultAvatarUrl}}"></image>
    </button> 
    <input type="nickname" class="nickname-input" placeholder="请输入昵称" 
      adjust-position="{{false}}"
      bindfocus="focusNickNameInput"
      bindblur="blurNickNameInput"
      bindchange="changeNickeNameInput"
      bindinput="inputNickName"
      model:value="{{nickName}}"
      bindconfirm="confirmNickNameInput"
      bindkeyboardheightchange="bindkeyboardheightchange"
      maxlength="10"
      style="border-radius: {{needBorderRadius?'8px':'0'}};"
    />
</view>

<view>
    <button class="user-confirm-btn user-confirm-btn--{{canSaveUser?'enabled':'disabled'}}"
        catchtap="handleInputAvatarNameAfter"
        style="border-radius: {{needBorderRadius?'50rpx':'0'}};"
        >
        授权头像和昵称
    </button>
</view>

2、index.js

   defaultBorderAvatar.png

data: {
    avatarUrl:"",//头像
    defaultAvatarUrl:"/images/defaultBorderAvatar.png", 
    canSaveUser:false,
    nickName:'' //昵称
}    
// 选择头像
    onChooseAvatar(e){
      const { avatarUrl } = e.detail
      promisify(wx.getFileSystemManager().getFileInfo)({  filePath: avatarUrl })
      .then((fileInfo) => {
        let fileSize = fileInfo.size;
        // 文件大小 kb
        fileSize = Math.ceil(fileSize / 1024).toFixed(2);
        if(fileSize > (1024 * 5)){
          wx.showToast({
            title: '头像大小请不要超过5M',
            icon:"none"
          })
          return Promise.reject();
        }
        return fileInfo;
      }).then(()=>{
        console.log("------成功设置头像------");
        this.setData({
          avatarUrl,
        })
        this.observerCanSaveUser();
      }).catch(err=>{
        console.log(err);
      })
    },
// 昵称进入焦点
focusNickNameInput(e){},
// 昵称输入框失焦点
blurNickNameInput(e){},

//实际只有这个方法用到了
changeNickeNameInput(e){
   this.observerCanSaveUser();
},
inputNickName(e){},

// 确认昵称
confirmNickNameInput(){},

// 昵称输入框 软件盘高度改变
bindkeyboardheightchange(e){
   if(this.data.preInputBottom && this.data.inputBottom&& this.data.preInputBottom === this.data.inputBottom){
      return;
  }
  if(!this.data.inputBottom){
     this.setData({inputBottom:e.detail.height})
  }else{
     this.setData({
        preInputBottom: this.data.inputBottom,  
        inputBottom:e.detail.height
     })
  }
},
observerCanSaveUser(){
  if(this.data.nickName && String(this.data.nickName).trim() && this.data.avatarUrl){
     this.setData({
        canSaveUser:true
     })
 }else{
    this.setData({
      canSaveUser:false
    })
 }
},

// 头像、昵称同时输入完后才保存,保存成功再关闭
handleInputAvatarNameAfter(){
       //使用  wx.uploadFile  方法,把头像上传并保存起来
        ...
    
}

3、index.wxss

.user-content{
  display: flex;
  flex-direction: column;
  align-items: center;
}
.avatar-wrapper {
  height: 112rpx;
  width: 112rpx;
  margin: 0;
  background-color: 0;
  background: 0;
  padding:0;
  border-radius: 0;
  border-radius: 0;
  border: 0;

  overflow: hidden;
}
.avatar-wrapper::after{ border: none; }
.avatar-wrapper image{
  width: 100%;
  height: 100%;
}

.container {
  display: flex;
}

.nickname-input {
  width: 608rpx;
  height: 108rpx;
  line-height: 112rpx;
  border-radius: 0;
  opacity: 1;
  box-sizing: border-box;

  font-size: 34rpx; 
  font-weight: 400;
  color: #000000;

  text-align: center;
  margin-bottom: 24rpx;
}

.user-confirm-btn{
  width: 608rpx;
  height: 72rpx;
  line-height: 72rpx;
  background: #B2B2B2;
  border-radius: 0px 0px 0px 0px;
  opacity: 1;
  border-radius: 0;
  border: 0;

  font-size: 28rpx;
  font-weight: 400;
  color: #FFFFFF;
  }
.user-confirm-btn::after{ border: none; }
.user-confirm-btn--disabled{
  background: #B2B2B2;
}
.user-confirm-btn--enabled{
  background: #000000;
}
.close-image{
  width: 34rpx;
  height: 34rpx;
  position: absolute;
  top: 40rpx;
  right: 40rpx;
}

<think>好的,我现在需要解决用户关于微信小程序中点击button按钮不显示微信头像的问题。首先,我需要回忆一下微信小程序近期的接口变化。根据提供的引用,wx.getUserProfile已经被废弃,导致获取用户信息时返回默认头像昵称,比如“微信用户”和灰色头像用户现在遇到的是点击按钮后无法显示正确的头像,这可能是因为他们还在使用旧的接口方法。 接下来,我需要确认当前有效的替代方案。引用中提到了需要使用新的方式,比如让用户主动点击按钮,分别授权头像昵称,可能还需要使用表单组件。例如,解决方案一提到需要用户分别授权头像昵称,并且需要跳转到新页面填写表单。这可能涉及到使用<button open-type="chooseAvatar">来获取头像,以及单独的输入框来获取昵称,然后通过表单提交。 然后,我需要检查用户当前的代码结构。如果用户还在button的点击事件中调用wx.getUserProfile或wx.getUserInfo,这已经不再有效,必须更换为新的方法。比如,使用open-type="chooseAvatar"来触发头像选择,并通过bindchooseavatar事件来获取头像URL。昵称部分可能需要用户手动输入,或者通过另一个按钮触发昵称授权,但现在可能需要用户主动填写,因为直接获取昵称的接口可能也被限制了。 另外,需要注意基础库的版本,引用2提到基础库2.27.1及以上版本不支持旧方法,所以需要确保用户小程序基础库版本适配新的接口。同时,头像昵称获取需要分开处理,用户可能需要分别授权,这可能涉及到多个步骤,比如先获取头像,再填写昵称,或者反之。 然后,我需要构建一个示例代码,展示如何正确使用新的API。比如,在wxml中使用带有open-type的button来获取头像,并在js中处理返回的avatarUrl。对于昵称,可能需要使用input组件,并让用户手动输入,或者通过其他授权方式。但根据引用4,现在需要用户主动提交表单,可能需要在表单中分别获取头像昵称,并绑定提交事件。 最后,需要提醒用户测试时使用真机调试,因为开发者工具可能有不同的表现,同时检查基础库版本是否更新,确保兼容性。总结下来,解决方案包括使用新的open-type按钮、分开处理头像昵称授权、使用表单提交,并更新相关的事件处理函数。</think>针对微信小程序点击按钮不显示用户微信头像的问题,解决方案如下: ### 原因分析 由于微信官方调整用户信息获取规则,2022年后已废弃${\tt wx.getUserProfile}$接口[^1][^2],强制使用新授权流程。旧方案会导致以下问题: 1. 返回默认灰色头像和"微信用户"昵称[^3] 2. 需要用户主动触发授权操作[^4] ### 解决方案 #### 步骤1:使用新的头像授权方式 ```wxml <!-- 正确写法 --> <button open-type="chooseAvatar" bind:chooseavatar="onChooseAvatar"> 选择头像 </button> ``` #### 步骤2:处理头像选择事件 ```javascript Page({ onChooseAvatar(e) { const { avatarUrl } = e.detail this.setData({ userAvatar: avatarUrl // 存储到用户信息中 }) } }) ``` #### 步骤3:获取昵称(需单独处理) ```wxml <input type="nickname" placeholder="请输入昵称" bindblur="onNickNameInput" /> ``` #### 步骤4:完整示例代码 ```wxml <!-- index.wxml --> <view class="container"> <button open-type="chooseAvatar" bind:chooseavatar="onChooseAvatar" class="avatar-btn" > <image src="{{userInfo.avatarUrl || '/images/default-avatar.png'}}" mode="aspectFill" /> </button> <input type="nickname" placeholder="请输入微信昵称" bindblur="onNickNameBlur" value="{{userInfo.nickName}}" /> </view> ``` ```javascript // index.js Page({ data: { userInfo: { avatarUrl: '', nickName: '' } }, onChooseAvatar(e) { this.setData({ 'userInfo.avatarUrl': e.detail.avatarUrl }) }, onNickNameBlur(e) { this.setData({ 'userInfo.nickName': e.detail.value }) } }) ``` ### 注意事项 1. 必须使用${\tt <button>}$组件触发授权,不能通过API直接调用[^4] 2. 头像昵称需分开获取,不能合并操作 3. 需要配置${\tt input}$的${\tt type="nickname"}$属性才能调起昵称键盘 4. 真机调试时需使用最新版微信客户端(8.0.30+) ### 效果验证 1. 用户点击头像按钮 → 弹出系统级头像选择器 2. 用户点击昵称输入框 → 自动弹出微信昵称键盘 3. 数据保存后可通过${\tt wx.setStorageSync}$存储用户信息
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浩星

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值