云开发快速从 getUserInfo 切换到 getUserProfile
getUserProfile 的事在社区里已经闹得沸沸扬扬。
我的项目用的是云开发,处理下来,发现不用调整太多,整体思路:
用 getUserProfile 拿到用户信息,用 getUserInfo 通过云函数拿到 openId
小程序端:
const fetchOpenId = (userInfo) => {
const callFunctionSuccess = (res) => {
// 到这一步处理用户登录
console.log({
...userInfo,
openId: res.openId
})
}
const getUserInfoSuccess = (res) => {
wx.cloud.callFunction({
name: 'getOpenData',
data: {
dataType: 'getOpenData',
weRunData: wx.cloud.CloudID(res.cloudID)
},
success: (r) => {
if (r.errMsg === 'cloud.callFunction:ok') {
callFunctionSuccess(r.result)
}
}
})
}
wx.getUserInfo({
success: getUserInfoSuccess
})
}
const fetchUserProfile = () => {
const success = (res) => {
fetchOpenId(res.userInfo)
}
wx.getUserProfile({
desc: '用于完善会员资料',
success
})
}
调用:
fetchUserProfile()
云函数 getOpenData
const cloud = require('wx-server-sdk')
cloud.init()
exports.main = async (event) => {
return event.weRunData.data
}
这样处理会有一个现象:如果用户退出了登录,每次重新登录都会弹出授权。
想了想,这样也挺合理,否则每次登录都没有感知。
记得做好本地存储。


