// 云函数入口函数
const cloud = require('wx-server-sdk');
cloud.init()
const db = cloud.database()
const modecode = {
state: false,
msg: '调用失败'
}
exports.main = async(event, context) => {
var thisHistory = null
const openid = cloud.getWXContext().OPENID
console.log('访问者', openid)
console.log('访问者', event)
return await db.collection('history_list')
.where({
_openid: openid,
id: event.id
}).get().then(res=>{
if (res.data.length > 0) {
console.log('找到记录执行')
db.collection('history_list').doc(res.data[0]._id).update({
data: {
add_time: db.serverDate(),
episode: event.episode,
thumb: event.thumb,
note: event.note,
progress: event.progress
},
})
.then(res => {
modecode.state = true;
modecode.msg = '更新成功!'
return modecode, res
})
.catch(res => {
modecode.state = true;
modecode.msg = '更新失败!'
return modecode, res
})
} else {
console.log('未找到记录执行')
try {
db.collection('history_list').add({
data: {
add_time: db.serverDate(),
episode: event.episode,
id: event.id,
name: event.name,
note: event.note,
thumb: event.thumb,
progress: event.progress
}
})
.then(res => {
console.log('未找到记录执行成功',res)
modecode.state = true;
modecode.msg = '添加成功!'
return modecode,res
})
} catch (res) {
console.log('未找到记录执行失败',res)
modecode.state = true;
modecode.msg = '添加失败!'
return modecode, res
}
}
})
}
数据库日志