微信小程序wx.getUserProfile接口获取用户昵称、头像

网友投稿 464 2023-10-29

wx.getUserProfile接口用于微信小程序获取用户信息,当页面产生点击事件(例如 button 上 bindtap 的回调中)后才可调用,每次请求都会弹出授权窗口,用户同意后返回 userInfo。下面一起来看看具体的实现方法吧:

getUserInfo.js

Page({

data: {

userInfo: {},

hasUserInfo: false,

canIUseGetUserProfile: false,

},

onLoad() {

if (wx.getUserProfile) {

this.setData({

canIUseGetUserProfile: true

})

}

},

getUserProfile(e) {

// 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认

// 开发者妥善保管用户快速填写的头像昵称,避免重复弹窗

wx.getUserProfile({

desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写

success: (res) => {

this.setData({

userInfo: res.userInfo,

hasUserInfo: true

})

console.log(res.userInfo)

}

})

},

getUserInfo(e) {

// 不推荐使用getUserInfo获取用户信息,预计自2021年4月13日起,getUserInfo将不再弹出弹窗,并直接返回匿名的用户个人信息

console.log(e.detail.userInfo)

this.setData({

userInfo: e.detail.userInfo,

hasUserInfo: true,

})

},

})

getUserInfo.wxml

{{userInfo.nickName}}

</view>

微信小程序wx.getUserProfile接口获取用户昵称、头像具体信息,小程序开发者可以参考官方文档:https://developers.weixin.qq.com/miniprogram/dev/api/open-api/user-info/wx.getUserProfile.html

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:如何用app小程序商城引爆你的销售额?
下一篇:企业建造app遇到的5大难题!
相关文章

 发表评论

暂时没有评论,来抢沙发吧~