Get user information
This is click the login button to get user information
<image src="{
{userInfo.avatarUrl}}"></image>
<view wx:if="{
{userInfo.openid}}">
{
{UserInfo.nickname}}
</view>
<Button wx: else open-type = "getuserinfo" bindgetUserInfo = "ONEGETUSERINFO"> Login </Button>
On the corresponding page JS file:
Page({
data: {
userInfo: {}
},
onGetUserInfo(e){
console.log('onGetUserInfo',e);
if(e.detail.userInfo){
this.setData({
avatarUrl:e.detail.userInfo.avatarUrl,
userInfo:e.detail.userInfo
})
}
}
})
Here you need to keep the function of obtaining information in the app.js:
/App.js
App ({{
onlaunch: function () {
// Run the cloud function to configure it
wx.cloud.init ();
// Log in
wx.login ({{
Success: res => {
// Send res.code to the background in exchange for openid, sessionkey, unionid
}
})
// Get user information
wx.getSetting ({{
Success: res => {
if (res.authSetting ['scope.userinfo'] {) {
// I have been authorized, you can directly call getuserinfo to get the avatar nickname, you will not play the frame
wx.getUserInfo ({{
Success: res => {
// You can send the Res to the background to decode the unionid
this.globaldata.userinfo = res.userinfo
// Since getuserinfo is a network request, it may be returned after pay.onload
// So add callback here to prevent this
if (this.userinForeADYCALLLLLK) {
this.userinForeADYCALLLLK (res)
}
}
})
}
}
})
},
globaldata: {
userInfo: null
}
})