【微信小程序】获取用户信息
2021-04-09 本文已影响0人
辣子_
html:
<button class="registerBtn" v-if="canIUseGetUserProfile" @tap="getUserProfileFn"> 注册 </button>
<button class="registerBtn" v-else open-type="getUserInfo" bindgetuserinfo="getUserInfo"> 注册 </button>
js:
//判断运行环境
if (wx.getUserProfile) {
this.canIUseGetUserProfile = true
}
uni.login({
provider: 'weixin',
success: function(loginRes) {
console.log(loginRes)
that.model.code = loginRes.code;
}
});
getUserProfileFn(e) {
let that = this
// 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认
// 开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
wx.getUserProfile({
desc: '用于完善个人资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
success: (res) => {
that.model.nickname = res.userInfo.nickName;
that.model.avatarurl = res.userInfo.avatarUrl;
that.model.province = res.userInfo.province;
that.model.city = res.userInfo.city;
//注册逻辑
that.register()
}
})
},
getuserinfo(res) {
let that = this
this.model.nickName = res.detail.userInfo.nickName;
this.model.avatarUrl = res.detail.userInfo.avatarUrl;
this.model.province = res.detail.userInfo.province;
this.model.city = res.detail.userInfo.city;
//注册逻辑
that.register()
},
注意:获取用户信息前不能有任何其他代码!