尝试把wx.getUserProfile写在catch里,代码不生效。 想请教一下,这里的逻辑应该如何写?
handleLogin() {
// 显示loading提示框
wx.showLoading({
title: '',
});
// 调用云函数获取openId
wx.cloud.callFunction({
name: 'getOpenId',
})
.then((res) => {
// 修改openId标记,记录openId
this.setData({
haveGetOpenId: true,
openId: res.result.openid
});
// 把openId保存到缓存
wx.setStorageSync('openId', res.result.openid)
wx.hideLoading();
})
// 查询用户是否在数据库中
.then(() => {
// 在数据库中查询openId
db.collection('users').doc(this.data.openId).get()
.then(res => {
// res.data 包含该记录的数据
console.log(res.data)
this.setData({
haveFindUser: true
})
})
.catch((err) => {
// 没有查到openId, 小程序将获取头像、昵称
console.log(err);
})
})
.then(() => {
// 如果没有找到OpenId ,就调用wx.getUserProfile,请问这个逻辑应该怎么写?
// if (!this.data.haveFindUser) {
// console.log('xxxx没找到', this.data.haveFindUser)
// } else {
// console.log('找到了', this.data.haveFindUser)
// }
// 用云函数将获取到的openId和用户信息写入数据库
// this.userRegister()
})
.catch((err) => {
wx.hideLoading();
console.log(err)
});
},
1、wx.getUserProfile要过期了,不要研究它了;
2、wx.getUserProfile和openid没有毛线关系;也不要让他们有关系。你应该判断数据库里没有nickName或avatarUrl时,才执行“授权昵称和头像”的代码,