eleven
小程序onload为每次加载数据显示
onshow展示页面显示
——————————————————————————————
这里是关于微信小程序获取用户openid,并且判断是否存在
首先是获取用户信息
点击登录按钮将获取的头像和名字存入数据库
my.wxml
wx.getUserProfile({
desc: '用于授权信息',
success(res){
console.log(res.userInfo)
var user=res.userInfo
app.globalData.userInfo=user
that.setData({
userInfo:user
})
创建数据库
获取信息并写入数据库
my.js
将内容显示到页面上
获取用户openid
创建getOpenid云函数,默认内容即可
判断数据库是否存在openid
app.js
login整体代码
login(){
var that=this;
wx.getUserProfile({
desc: '用于授权信息',
success(res){
console.log(res.userInfo)
var user=res.userInfo
app.globalData.userInfo=user
that.setData({
userInfo:user
})
//用户数据表
//检查是否已经授权登录
wx.cloud.database().collection('userID').where({
_openid:app.globalData.openid
}).get({
success(res){
console.log(res)
if(res.data.length==0){
wx.cloud.database().collection('userID').add({
data:{
avatarUrl :user.avatarUrl,
nickName: user.nickName
},
success(res){
console.log(res)
wx.showToast({
title: '登陆成功',
})
}
})
}else{
that.setData({
userInfo:res.data[0],
hasUserInfo:true
})
app.globalData.userInfo=res.data[0]
}
}
})
},
})
},