微信登录获取用户信息,报错
微信登录获取用户信息,报错
Cannot read property ‘avatarUrl’ of undefined;at page/home/index onLoad function;at api getStorage success callback function
TypeError: Cannot read property ‘avatarUrl’ of undefined
这是怎么回事?
6 回复
new_getUserInfo: function (cb) {
var that = this;
//调用登录接口
wx.login({
success: function (res_login) {
var code = res_login.code
that.globalData.code = code;
wx.getUserInfo({
success: function (res) {
var encryptedData = encodeURIComponent(res.encryptedData);
var iv = res.iv;
// 登录信息
wx.request({
url: getApp().globalData.denglu_url + "?issource=" + getApp().globalData.mark,
data: {
code: code,
iv: iv,
encryptedData: encryptedData,
source: "SmallProgram"
},
method: "GET",
header: {
'content-type': 'application/json'
},
success: function (all) {
var res_data = all.data;
that.globalData.userInfo = res_data;
var json=all.data;
var authkey=json.authkey;
wx.setStorageSync('key', json);
}
})
}
})
}
});
},
onLaunch: function () {
// 调用API从本地缓存中获取数据
// wx.setStorageSync('key', null)
var that = this;
wx.getStorage({
key: 'key',
success: function(res) {
var authkey = res.data;
if(authkey==""||authkey==null){
that.getUserInfo(function (e) {});
}else{
}
},
fail:function(e){
that.getUserInfo(function (e) {});
}
})
},
getUserInfo: function (cb) {
var that = this;
if (this.globalData.userInfo) {
typeof cb == "function" && cb(this.globalData.userInfo)
} else {
//调用登录接口
wx.login({
success: function (res_login) {
var code = res_login.code
that.globalData.code = code;
wx.getUserInfo({
success: function (res) {
var encryptedData = encodeURIComponent(res.encryptedData);
var iv = res.iv;
// 登录信息
wx.request({
url: getApp().globalData.denglu_url + "?issource=" + getApp().globalData.mark,
data: {
code: code,
iv: iv,
encryptedData: encryptedData,
source: "SmallProgram"
},
method: "GET",
header: {
'content-type': 'application/json'
},
success: function (all) {
console.log(all)
var res_data = all.data;
that.globalData.userInfo = res_data;
var json=all.data;
var authkey=json.authkey;
wx.setStorage({
key:"key",
data:json
});
wx.setStorage({
key: "mobbile",
data: json.userInfo.mobile
})
var userInfo=json.userInfo;
that.globalData.avatarUrl=userInfo.avatarUrl;
that.globalData.city=userInfo.city;
that.globalData.country=userInfo.country;
that.globalData.gender=userInfo.gender;
that.globalData.id=userInfo.id;
that.globalData.language=userInfo.language;
that.globalData.mobile=userInfo.mobile;
that.globalData.nickName=userInfo.nickName;
that.globalData.openId=userInfo.openId;
that.globalData.province=userInfo.province;
that.globalData.appid=userInfo.watermark.appid;
that.globalData.timestamp=userInfo.watermark.timestamp;
}
})
// 登录信息
typeof cb == "function" && cb(that.globalData.userInfo)
}
})
}
});
}
},
new_getUserInfo: function (cb) {
var that = this;
//调用登录接口
wx.login({
success: function (res_login) {
var code = res_login.code
that.globalData.code = code;
wx.getUserInfo({
success: function (res) {
var encryptedData = encodeURIComponent(res.encryptedData);
var iv = res.iv;
// 登录信息
wx.request({
url: getApp().globalData.denglu_url + "?issource=" + getApp().globalData.mark,
data: {
code: code,
iv: iv,
encryptedData: encryptedData,
source: "SmallProgram"
},
method: "GET",
header: {
'content-type': 'application/json'
},
success: function (all) {
var res_data = all.data;
that.globalData.userInfo = res_data;
var json=all.data;
var authkey=json.authkey;
wx.setStorageSync('key', json);
}
})
}
})
}
});
},