|
|
@ -4145,6 +4145,84 @@ app.post('/api/products/delete', async (req, res) => { |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
// 下架商品 - 修改label字段为1
|
|
|
|
|
|
app.post('/api/products/unpublish', async (req, res) => { |
|
|
|
|
|
console.log('收到下架商品请求:', req.body); |
|
|
|
|
|
try { |
|
|
|
|
|
const { productId } = req.body; |
|
|
|
|
|
|
|
|
|
|
|
if (!productId) { |
|
|
|
|
|
console.error('下架商品失败: 缺少productId参数'); |
|
|
|
|
|
return res.status(400).json({ |
|
|
|
|
|
success: false, |
|
|
|
|
|
code: 400, |
|
|
|
|
|
message: '缺少productId参数' |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 查找商品
|
|
|
|
|
|
const product = await Product.findOne({ |
|
|
|
|
|
where: { productId } |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
if (!product) { |
|
|
|
|
|
console.error('下架商品失败: 商品不存在'); |
|
|
|
|
|
return res.status(404).json({ |
|
|
|
|
|
success: false, |
|
|
|
|
|
code: 404, |
|
|
|
|
|
message: '商品不存在' |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
console.log('准备下架商品,productId:', productId, ',当前label:', product.label); |
|
|
|
|
|
|
|
|
|
|
|
// 直接使用商品实例更新label字段
|
|
|
|
|
|
product.label = 1; |
|
|
|
|
|
product.updated_at = getBeijingTime(); |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
await product.save(); |
|
|
|
|
|
console.log('下架商品成功(使用save方法):', { productId: product.productId, newLabel: product.label }); |
|
|
|
|
|
} catch (saveError) { |
|
|
|
|
|
console.error('使用save方法更新失败,尝试使用update方法:', saveError); |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
const updateResult = await Product.update( |
|
|
|
|
|
{ label: 1, updated_at: getBeijingTime() }, |
|
|
|
|
|
{ where: { productId } } |
|
|
|
|
|
); |
|
|
|
|
|
console.log('下架商品成功(使用update方法):', { productId, updateResult }); |
|
|
|
|
|
} catch (updateError) { |
|
|
|
|
|
console.error('使用update方法也失败:', updateError); |
|
|
|
|
|
throw new Error('下架商品失败: ' + updateError.message); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 重新查询商品以确保返回最新状态
|
|
|
|
|
|
const updatedProduct = await Product.findOne({ |
|
|
|
|
|
where: { productId } |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
res.json({ |
|
|
|
|
|
success: true, |
|
|
|
|
|
code: 200, |
|
|
|
|
|
message: '下架商品成功', |
|
|
|
|
|
product: { |
|
|
|
|
|
productId: updatedProduct.productId, |
|
|
|
|
|
label: updatedProduct.label |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
} catch (error) { |
|
|
|
|
|
console.error('下架商品失败:', error); |
|
|
|
|
|
res.status(500).json({ |
|
|
|
|
|
success: false, |
|
|
|
|
|
code: 500, |
|
|
|
|
|
message: '下架商品失败', |
|
|
|
|
|
error: error.message |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
// 测试路由:用于调试购物车数据格式 - 增强版,包含完整的数量处理测试
|
|
|
// 测试路由:用于调试购物车数据格式 - 增强版,包含完整的数量处理测试
|
|
|
app.post('/api/cart/test-format', async (req, res) => { |
|
|
app.post('/api/cart/test-format', async (req, res) => { |
|
|
try { |
|
|
try { |
|
|
|