自 2022 年 10 月 25 日 24 时后(以下统称 “生效期” ),用户头像昵称获取规则将进行如下调整:
对于上述 3,wx.getUserProfile 接口、wx.getUserInfo 接口、头像昵称填写能力的基础库版本支持能力详细对比见下表:

以上是微信小程序对用户头像昵称更改的公告,从上文不能看出之前用到的wx.getUserProfile、wx.getUserInfo将被弃用,不在返回正确的用户昵称及头像信息。emm......完全看不懂为什么微信小程序总是揪着这个功能不放,一而再再而三的改动...
头像昵称填写
从基础库 2.21.2 开始支持。
需要将 button 组件 open-type 的值设置为 chooseAvatar,当用户选择需要使用的头像之后,可以通过 bindchooseavatar,uniApp使用@chooseavatar事件回调获取到头像信息的临时路径。
项目实战以uniApp为例
- <template>
- <view class="containar">
- <view class="avatarUrl">
- <button type="balanced" open-type="chooseAvatar" @chooseavatar="onChooseavatar">
- <image :src="avatarUrl" class="refreshIcon">image>
- button>
- view>
- <view class="nickname">
- <text>昵称:text>
- <input type="nickname" class="weui-input" :value="nickName" @blur="bindblur"
- placeholder="请输入昵称" @input="bindinput"/>
- view>
-
- <view class="btn">
- <view class="btn-sub" @click="onSubmit">保存view>
- view>
- view>
- template>
- export default {
- data() {
- return {
- avatarUrl: '',
- nickName: ''
- };
- },
- onLoad(option) {},
- methods: {
- bindblur(e) {
- this.nickName = e.detail.value; // 获取微信昵称
- },
- bindinput(e){
- this.nickName = e.detail.value; //这里要注意如果只用blur方法的话用户在输入玩昵称后直接点击保存按钮,会出现修改不成功的情况。
- },
- onChooseavatar(e) {
- let self = this;
- let {
- avatarUrl
- } = e.detail;
- uni.showLoading({
- title: '加载中'
- });
- uni.uploadFile({
- url: '后台uploadFile接口',
- filePath: avatarUrl,
- name: 'file',
- header: {
- token: '自己的token',
- },
- success: uploadFileRes => {
- // 注意:这里返回的uploadFileRes.data 为JSON 需要自己去转换
- let data = JSON.parse(uploadFileRes.data);
- if (data.code === 0) {
- this.avatarUrl = data.data;
- }
- },
- fail: (error) => {
- uni.showToast({
- title: error,
- duration: 2000
- });
- },
- complete: () => {
- uni.hideLoading();
- }
- });
- },
- onSubmit() {
- if(this.nickName === ''){
- uni.showToast({
- icon: 'none',
- title: '请输入昵称'
- })
- return false;
- }
- // 这里做自己公司的存储逻辑
- }
- }
- };
- .containar {
- .avatarUrl {
- padding: 80rpx 0 40rpx;
- background: #fff;
- button {
- background: #fff;
- line-height: 80rpx;
- height: auto;
- width: auto;
- padding: 20rpx 30rpx;
- margin: 0;
- display: flex;
- justify-content: center;
- align-items: center;
- .refreshIcon {
- width: 160rpx;
- height: 160rpx;
- border-radius: 50%;
- }
- .jt{
- width: 14rpx;
- height: 28rpx;
- }
- }
- }
- .nickname{
- background: #fff;
- padding: 20rpx 30rpx 80rpx;
- display: flex;
- align-items: center;
- justify-content: center;
- .weui-input{
- padding-left: 60rpx;
- }
- }
- .btn{
- width: 100%;
- .btn-sub{
- width: 670rpx;
- margin: 80rpx auto 0;
- height: 90rpx;
- background: #DF8585;
- border-radius: 45rpx;
- line-height: 90rpx;
- text-align: center;
- font-size: 36rpx;
- color: #fff;
- }
- }
- }
效果图:


这次分享就到这里了。希望能帮助到你。如果有需要做小程序的可以私信我哦。