我写的登陆流程是只有获取了授权才能调用后台获取用户信息的接口,意思就是只要后台能获取用户的信息,那么一定能获取用户的头像和昵称
问题是 后台中获取到的微信有个别的是头像空白,昵称不正常
appid:wx12b846af7a7ffd3b
app.js
getUserInfo: function (cb) {
var that = this
var value = wx.getStorageSync('sessionCode')
wx.login({
success: function (r) {
that.globalData.code = r.code;//登录凭证
var code = that.globalData.code
console.log("这是code")
console.log(r)
console.log("这是code")
if (code) {
//2、调用获取用户信息接口
wx.getUserInfo({
success: function (res) {
console.log("这是登录方法")
console.log(res)
console.log("这是登录方法")
that.globalData.userInfo = res.userInfo
typeof cb == "function" && cb(that.globalData.userInfo);
console.log({ encryptedData: res.encryptedData, iv: res.iv, code: code })
//3.请求自己的服务器,解密用户信息 获取unionId等加密信息
wx.request({
url: HOST + 'signseparate/wchat/login',//自己的服务接口地址
method: 'POST',
header: {
'content-type': 'application/x-www-form-urlencoded'
},
data: {
encryptedData: res.encryptedData,
iv: res.iv,
jsCode: code,
userimg: res.userInfo.avatarUrl,
city: res.userInfo.city,
gender: res.userInfo.gender,
province: res.userInfo.province,
nickName: res.userInfo.nickName,
county: res.userInfo.county
},
success: function (data) {
console.log("登陆session码")
console.log(data)
console.log("登陆session码")
wx.clearStorageSync()
wx.setStorageSync('sessionCode', data.data.userId)
wx.setStorageSync('tx', data.data.userimg)
that.globalData.tx = data.data.userimg
cb(data)
console.log(data.data.userId)
console.log("登陆码")
},
fail: function () {
console.log('不能返回code')
}
})
},
fail: function (res) {
console.log("拒绝授权")
console.log(res)
wx.getSetting({
success: (res) => {
console.log("getSetting")
console.log(res)
if (res.authSetting['scope.userInfo']) {
console.log("res2")
console.log(res.userInfo.avatarUrl)
console.log("res2")
wx.request({
url: HOST + 'signseparate/wchat/login',//自己的服务接口地址
method: 'POST',
header: {
'content-type': 'application/x-www-form-urlencoded'
},
data: {
encryptedData: res.encryptedData,
iv: res.iv,
jsCode: code,
userimg: res.userInfo.avatarUrl,
city: res.userInfo.city,
gender: res.userInfo.gender,
province: res.userInfo.province,
nickName: res.userInfo.nickName,
county: res.userInfo.county
},
success: function (data) {
console.log("登陆session码")
console.log(data)
console.log("登陆session码")
wx.clearStorageSync()
wx.setStorageSync('sessionCode', data.data.userId)
wx.setStorageSync('tx', data.data.userimg)
that.globalData.tx = data.data.userimg
cb(data)
console.log(data.data.userId)
console.log("登陆码")
},
fail: function () {
console.log('不能返回code')
}
})
}
else {
wx.openSetting({
success: (res) => {
}
})
}
}
})
cb(res)
console.log('获取用户信息失败')
}
})
}
else {
console.log('获取用户登录session失败!' + r.errMsg)
}
},
fail: function () {
console.log('登陆失败')
}
})
},
index.js
onShow: function () {
console.log("res")
console.log("去登陆")
app.getUserInfo(function (data) {
})
},