2019-04-25 08:23:30 +08:00

60 lines
1.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
//新建商品返回新商品id
var addProduct = async (event, wxContext) => {
var res = await db.collection("sM_productInfo").add({
data: {
userInfo: {
name: event.detailInfo.userName,
avatar: event.detailInfo.avatar,
adds: event.adds,
id: event.detailInfo._id,
},
time: Date.now(),
productName: event.productName,
details: event.details,
price: event.price,
isfree: event.isfree,
tags: event.tags,
viewNum: 0,
wantUser: [],
photos: event.photos,
comment: [],
warning: false,
}
}).then(res => {
return res
}).catch(err => {
return err
})
return res._id;
}
//把这个新商品id和名称push到用户信息里
var rewriteUserInfo = async (id, wxContext,event) => {
var res = await db.collection('sM_userInfo').doc(event.detailInfo._id).update({
data: {
issue: db.command.push({
id:id,
name: event.productName
})
}
}).then(res => {
return res;
}).catch(err => {
return err;
})
return res;
}
// 云函数入口函数
exports.main = async (event, context) => {
const wxContext = cloud.getWXContext()
var newId = await addProduct(event, wxContext);
await rewriteUserInfo(newId, wxContext, event);
var res =await db.collection('sM_userInfo').doc(event.detailInfo._id).get();
return res;
}