好久没更新博客了,最近收到微信官方的消息推送,显示获取用户信息的方式从
wx.getUserInfo 变为 wx.getUserProfile,于是查了一下文档,就来写下这篇博客,以供他人参考
首先,该文档链接为: wx.getUserProfile文档地址
根据我亲手实验后,发现要使用 wx.getUserProfile,需要满足以下几个条件:
- 开发者工具需要升级到 1.05以上
- 调试基础库需要在 14.1 以上
- 该事件必须发生在 Tap 事件函数内
具体写法为:
wxml新旧写法:
<!-- 旧写法
<button open-type="getUserInfo" bindgetuserinfo="getUserInfo">授权</button>
-->
<button bindtap="getUserInfo">授权</button>
JS新旧写法:
// 获取用户信息:本地(初始)
getUserInfo(e) {
/** 旧写法
const { userInfo } = e.detail
console.log(userInfo)
**/
wx.getUserProfile({
desc: '获取头像和昵称', // 必填,声明当前获取的使用途径
success: res => {
const { userInfo } = res
this.setData({ user })
this.bindUserInfo(user)
}
})
},
本文介绍了微信小程序中获取用户信息接口的变化,从wx.getUserInfo更新为wx.getUserProfile。开发者需要确保工具和基础库版本符合要求,并在Tap事件内调用新接口。详细阐述了新接口的使用方法,包括WXML和JS的代码示例,旨在帮助开发者顺利迁移和理解这一更新。

357

被折叠的 条评论
为什么被折叠?



