这是我以前用js写的插入云数据库的代码,求问大家应该怎么修改?
bindGetUserInfo(ev);{
//console.log(ev);
let userInfo =ev.detail.userInfo;
if(!this.data.logged && userInfo){//后面是对的,前面不知道
db.collection('user').add({
data: {
userPhoto:userInfo.avatarUrl,
nickName: userInfo.nickName,
signature:'',
phoneNumber:'',
weixinNumber:'',
links:0,
time:new Data()
}
}).then((res)=>{
console.log(res);
});
}
}
我是看的陈玉明老哥的文章 参考地址 https://developers.weixin.qq.com/community/develop/article/doc/0004e2c427c86036fccaa038b56813 注释:mcm开头的都是我自己封装出来的 你自行切换成微信的API就行 wxml: <view class=""> <button catchtap="clickBugs" data-clitype="getUserProfile">微信登录</button> </view> js page外面需要声明这些 let userInfo_obj = {}; const db = wx.cloud.database(); //点击事件公交总线 clickBugs(ev){ let that = this; let openid = wx.getStorageSync('openid'); let unionid = wx.getStorageSync('unionid'); let clitype = ev.currentTarget.dataset.clitype; switch(clitype){ case "getUserProfile": //新用户微信授权 mcmUtils.mcmGetUserProfile((res)=>{ console.log(res,userInfo_obj); userInfo_obj = { ...res, openid, unionid, time:new Data() } mcmUtils.mcmShowToast("登录中","none",true); console.log(userInfo_obj,"0000000000000"); db.collection('users').where({ _openid: openid // 填入当前用户 openid }).get().then(res => { if (!res.data.length) { // 新用户 db.collection('users').add({ data: { userInfo: userInfo_obj } }).then(res => { mcmUtils.mcmShowToast("登录成功","none",true,2000); wx.setStorageSync('id', res._id); mcmUtils.mcmSetTimeout("loginTimeName",2000,(res)=>{ mcmUtils.mcmHideToast(); console.log("延迟了2000,登录成功,开始跳转回首页"); mcmUtils.mcmReLaunch("/pages/index/index"); }) }) } else { // 老用户 db.collection('users').doc(wx.getStorageSync('id')).update({ data: { userInfo: userInfo_obj } }).then(res=>{ console.log(res); mcmUtils.mcmShowToast("登录成功","none",true,2000); mcmUtils.mcmSetTimeout("loginTimeName",2000,(res)=>{ mcmUtils.mcmHideToast(); console.log("延迟了2000,登录成功,开始跳转回首页"); mcmUtils.mcmReLaunch("/pages/index/index"); }) }) } }) }) break; } },
添加的方式是一样的,主要是获取用户的信息。
具体可见:https://developers.weixin.qq.com/community/develop/doc/000cacfa20ce88df04cb468bc52801
看着头疼,openid呢?
感谢各位!!!