微信小程序 获取用户的昵称和头像
<open-data type="userAvatarUrl"></open-data> <open-data type="userNickName"></open-data>
下面说的都是需要获取头像地址和昵称字符串的方法
方法一: wx.getUserInfo
wx.getUserInfo需要授权后才能得到数据 在新版本中 wx.getUserInfo 需要搭配 button 让用户主动授权
html:
js:
Page({
data: {
canIUse: wx.canIUse(button.open-type.getUserInfo) // 这个是兼容
},
onLoad: function() {
// 查看是否授权
wx.getSetting({
success (res){
if (res.authSetting[scope.userInfo]) {
// 已经授权,可以直接调用 getUserInfo 获取头像昵称
wx.getUserInfo({
success: function(res) {
console.log(res.userInfo)
}
})
}
}
})
}
方法二: button + bindgetuserinfo
html:
js:
Page({
data: {
canIUse: wx.canIUse(button.open-type.getUserInfo) // 这个是兼容
},
onLoad: function() {
},
bindGetUserInfo (e) {
console.log(e.detail.userInfo);
}
})
上一篇:
uniapp开发微信小程序-2.页面制作
下一篇:
微信公众号接入百度天气API接口
