// pages/login/login.js
Page({
  data:{
    userInfo:'',
    avatarUrl:'',
    nickName:''
  },
  onLoad(){
    /*
     let user=wx.getStorageSync('user')
     this.setData({
       userInfo:user
     })
     wx.switchTab({      
      url: '/pages/index/index'
    })
    */
  },
  login(){
    let that = this;
    console.log('点击事件执行了')
    wx.getUserProfile({
      desc: '必须授权才能使用',
      success:res=>{
        let user=res.userInfo
        let avatarUrl=res.userInfo.avatarUrl
        let nickName=res.userInfo.nickName
        
        wx.setStorageSync('user', user)
        wx.setStorageSync('avatarUrl', avatarUrl)
        wx.setStorageSync('nickName', nickName)
  console.log('成功',res)
  wx.cloud.database().collection('user').add({
    data: {
      userInfo:user,
      avatarUrl:user.avatarUrl,
      nickName:user.nickName
    }
  })
  },
      fall:res=>{
        console.log('失败',res)
      }
    })
    wx.switchTab({      
      url: '/pages/index/index'
    })
  },
  nologin(){
   this.setData({
     userInfo:''
   })
   wx.setStorageSync('user', null)
  },
})