收藏
回答

请问getuserprofile怎么写入云数据库?

这是我以前用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);
  
      });
    }
  
  }
回答关注问题邀请回答
收藏

4 个回答

  • 默
    2021-03-22
    我是看的陈玉明老哥的文章 参考地址
    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;
        }
      },
    


    2021-03-22
    有用 2
    回复 1
    • 炸鸡
      炸鸡
      发表于移动端
      2021-03-22
      谢谢!
      2021-03-22
      回复
  • 陈宇明
    陈宇明
    2021-03-20

    添加的方式是一样的,主要是获取用户的信息。

    具体可见:https://developers.weixin.qq.com/community/develop/doc/000cacfa20ce88df04cb468bc52801

    2021-03-20
    有用 1
    回复 1
    • 炸鸡
      炸鸡
      发表于移动端
      2021-03-20
      能否上一段 wxml button组件的代码🙏
      2021-03-20
      回复
  • 小程序
    小程序
    发表于小程序端
    2021-03-20

    看着头疼,openid呢?

    2021-03-20
    有用
    回复 2
    • less
      less
      2021-03-20
      openid 字段系统会自动添加
      2021-03-20
      1
      回复
    • 炸鸡
      炸鸡
      2021-03-20回复less
      兄弟能上一段写入数据库的代码,或者上面的代码有什么修改的吗?我是初学者已经卡在这里两天了,感谢!!!
      2021-03-20
      回复
  • 炸鸡
    炸鸡
    2021-03-20

    感谢各位!!!

    2021-03-20
    有用
    回复 2
    • 浅忆草
      浅忆草
      2021-04-21
      请问解决了吗
      2021-04-21
      回复
    • 炸鸡
      炸鸡
      发表于移动端
      2021-05-11回复浅忆草
      解决啦
      2021-05-11
      回复
登录 后发表内容
问题标签