84 lines
1.9 KiB
JavaScript
84 lines
1.9 KiB
JavaScript
// 云函数入口文件
|
|
const cloud = require('wx-server-sdk')
|
|
|
|
cloud.init()
|
|
const db = cloud.database()
|
|
|
|
//评论
|
|
var comment = async (event) => {
|
|
var res = await db.collection("sM_productInfo").doc(event.prodectId).update({
|
|
data:{
|
|
comment: db.command.push({
|
|
name:event.detailInfo.userName,
|
|
avatar:event.detailInfo.avatar,
|
|
userid:event.detailInfo._id,
|
|
time: Date.now(),
|
|
msg: event.msg,
|
|
reply: []
|
|
})
|
|
}
|
|
}).then(res => {
|
|
return res;
|
|
}).catch(err => {
|
|
return err;
|
|
})
|
|
return res;
|
|
}
|
|
|
|
//回复
|
|
var reply = async(event) => {
|
|
//取出原有的comment加以修改
|
|
var oldInfo = await db.collection("sM_productInfo").doc(event.prodectId).get();
|
|
oldInfo.data.comment[event.replyNum].reply.push({
|
|
name:event.detailInfo.userName,
|
|
avatar: event.detailInfo.avatar,
|
|
userId: event.detailInfo._id,
|
|
msg: event.msg,
|
|
time: Date.now(),
|
|
})
|
|
//放回去
|
|
await db.collection("sM_productInfo").doc(event.prodectId).update({
|
|
data:{
|
|
comment: db.command.set(oldInfo.data.comment)
|
|
}
|
|
})
|
|
return oldInfo.data;
|
|
}
|
|
|
|
//通知
|
|
var notice = async (event) => {
|
|
//取出,修改
|
|
var userInfo = await db.collection('sM_userInfo').doc(event.replyId).get();
|
|
//没有就初始化
|
|
if(!userInfo.data.newMsg.reply) userInfo.data.newMsg.reply = [];
|
|
userInfo.data.newMsg.reply.push({
|
|
prodectId:event.prodectId,
|
|
prodectName:event.prodectName,
|
|
time: Date.now(),
|
|
})
|
|
//放回去
|
|
await db.collection("sM_userInfo").doc(event.replyId).update({
|
|
data:{
|
|
newMsg: db.command.set(userInfo.data.newMsg)
|
|
}
|
|
})
|
|
return ;
|
|
}
|
|
|
|
// 云函数入口函数
|
|
exports.main = async (event, context) => {
|
|
const wxContext = cloud.getWXContext()
|
|
if (event.iscomment) {
|
|
var res = await comment(event);
|
|
} else {
|
|
var res = await reply(event);
|
|
}
|
|
notice(event);
|
|
return res;
|
|
// return {
|
|
// event,
|
|
// openid: wxContext.OPENID,
|
|
// appid: wxContext.APPID,
|
|
// unionid: wxContext.UNIONID,
|
|
// }
|
|
} |