// 云函数模板
// 部署:在 cloud-functions/login 文件夹右击选择 “上传并部署”
const cloud = require('wx-server-sdk')
// 初始化 cloud
cloud.init({
// API 调用都保持和云函数当前所在环境一致
env: "anjianteam-7gwp9t1fd42848b0"
})
const db = cloud.database()
const loguser = "Ts51102186"
const logpwd = "Ts51102186"
/** * 这个示例将经自动鉴权过的小程序用户 openid 返回给小程序端 *
* event 参数包含小程序端调用传入的 data* */
exports.main = async (event, context) => {
const wxContext = cloud.getWXContext()
const openid =wxContext.OPENID //存储openid
let usInfo = event.userInfo //存储参数userinfo
console.log(wxContext.OPENID)
if (event.loguser === loguser && event.logpwc === logpwd) { //校验用户名密码
console.log('第2步')
//添加userInfo数据开始
// let userInfo = event.userInfo
const _ = db.command
let searchres = await db.collection('userInfo').where({
_id: openid
}).get();
console.log('打印查询数',searchres) //判断是否存在
if (searchres.data.length === 0) {
console.log('不存在该用户add')
db.collection('userInfo').add({
data: {
_id: openid,
userInfo: usInfo
},
success: function (res) {
console.log(res)
return openid
}
})
}
}
}