https://developers.weixin.qq.com/community/develop/doc/00022c683e8a80b29bed2142b56c01?page=4
官方文档说了 接口将被收回,新发布小程序的接口获取用户头像将统一返回默认灰色头像,昵称将统一返回 “微信用户”
代码如下:
<view class="container" v-if="!userShow">
<image src="@/static/images/index/logo.png" mode=""></image>
<view class="title">xx社区</view>
<button class="login_btn" type="Primary" open-type="chooseAvatar" @chooseavatar="mpGetUserInfo">授权进入xx社区</button>
</view>
<view class="container" v-else>
<image :src="userInfo.avatarUrl" mode=""></image>
<input @blur="inputBlur" class="title" type="nickname" placeholder="请输入昵称"/>
<button class="login_btn" type="Primary" open-type="getPhoneNumber"
@getphonenumber="getPhoneNumber">微信用户一键登录</button>
</view>
主要是这两个地方:
一、
获取用户头像回调 open-type=“chooseAvatar”
获取用户头像,可以从@chooseavatar回调中获取到头像信息 微信小程序2.21.2版本+
<button class="login_btn" type="Primary" open-type="chooseAvatar" @chooseavatar="mpGetUserInfo">授权进入xx社区</button>
二、
昵称变为输入触发获取
nickname 昵称输入键盘
<input @blur="inputBlur" class="title" type="nickname" placeholder="请输入昵称"/>
三、
获取用户头像
mpGetUserInfo(e) {
var that = this;
if(!that.pitchType) {
uni.showToast({
title: '请阅读并勾选底部协议',
icon: 'none'
})
return
}
console.log(e, '用户信息result')
that.userInfo = e.detail // 新规则只能获取头像,昵称需要另外获取
that.userShow = true
that.$store.commit('setUserInfo', that.userInfo) // vuex更新数据
},
四、
获取用户昵称
inputBlur(e) {
let userInfo = this.$store.state.UserInfo
userInfo.nickName = e.target.value
this.$store.commit('setUserInfo', userInfo)
},
这里有个注意地方:微信开发者工具不显示value, 真机是正常显示的!
微信开发者工具:
真机调试:
最终效果