You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.2 KiB

// 云函数publishSecondhandGoods
const cloud = require('wx-server-sdk')
// 初始化cloud
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV
})
const db = cloud.database()
exports.main = async (event, context) => {
const wxContext = cloud.getWXContext()
const openid = wxContext.OPENID
// 获取请求参数
const {
title,
description,
price,
condition,
category,
images = []
} = event
// 验证必填字段
if (!title || !price || !category) {
return {
success: false,
message: '请填写所有必填字段'
}
}
try {
// 添加新商品
const result = await db.collection('secondhand_goods').add({
data: {
openid,
title,
description,
price: parseFloat(price),
condition,
category,
images,
status: 'available', // 状态:可购买
createTime: db.serverDate(),
updateTime: db.serverDate(),
publishTime: new Date().toISOString()
}
})
return {
success: true,
goodsId: result._id,
message: '商品发布成功'
}
} catch (error) {
return {
success: false,
message: '商品发布失败',
error: error.message
}
}
}