155 lines
4.0 KiB
JavaScript
155 lines
4.0 KiB
JavaScript
const express = require('express');
|
|
const router = express.Router();
|
|
const dbConnector = require('../utils/dbConnector');
|
|
|
|
// 获取用户购物车
|
|
router.get('/', async (req, res) => {
|
|
try {
|
|
const userId = req.user.id;
|
|
|
|
const cartItems = await dbConnector.query(`
|
|
SELECT ci.*, p.name as product_name, p.price, p.image as product_image, p.stock
|
|
FROM cart_items ci
|
|
JOIN products p ON ci.product_id = p.id
|
|
WHERE ci.user_id = ?
|
|
`, [userId]);
|
|
|
|
const totalAmount = cartItems.reduce((sum, item) => sum + (item.price * item.quantity), 0);
|
|
const totalQuantity = cartItems.reduce((sum, item) => sum + item.quantity, 0);
|
|
|
|
res.json({
|
|
code: 200,
|
|
message: '获取成功',
|
|
data: {
|
|
items: cartItems,
|
|
total_amount: totalAmount,
|
|
total_quantity: totalQuantity
|
|
}
|
|
});
|
|
} catch (error) {
|
|
console.error('获取购物车失败:', error);
|
|
res.status(500).json({
|
|
code: 500,
|
|
message: '服务器内部错误',
|
|
error: error.message
|
|
});
|
|
}
|
|
});
|
|
|
|
// 添加商品到购物车
|
|
router.post('/items', async (req, res) => {
|
|
try {
|
|
const { product_id, quantity } = req.body;
|
|
const userId = req.user.id;
|
|
|
|
// 检查商品是否存在
|
|
const product = await dbConnector.query('SELECT * FROM products WHERE id = ? AND status = 1', [product_id]);
|
|
if (product.length === 0) {
|
|
return res.status(404).json({
|
|
code: 404,
|
|
message: '商品不存在'
|
|
});
|
|
}
|
|
|
|
// 检查购物车是否已有该商品
|
|
const existingItem = await dbConnector.query(
|
|
'SELECT * FROM cart_items WHERE user_id = ? AND product_id = ?',
|
|
[userId, product_id]
|
|
);
|
|
|
|
if (existingItem.length > 0) {
|
|
// 更新数量
|
|
await dbConnector.query(
|
|
'UPDATE cart_items SET quantity = quantity + ?, updated_at = NOW() WHERE id = ?',
|
|
[quantity, existingItem[0].id]
|
|
);
|
|
} else {
|
|
// 新增商品
|
|
await dbConnector.query(
|
|
'INSERT INTO cart_items (user_id, product_id, quantity, created_at, updated_at) VALUES (?, ?, ?, NOW(), NOW())',
|
|
[userId, product_id, quantity]
|
|
);
|
|
}
|
|
|
|
res.json({
|
|
code: 200,
|
|
message: '添加成功',
|
|
data: {
|
|
cart_item_id: existingItem.length > 0 ? existingItem[0].id : null
|
|
}
|
|
});
|
|
} catch (error) {
|
|
console.error('添加购物车失败:', error);
|
|
res.status(500).json({
|
|
code: 500,
|
|
message: '服务器内部错误',
|
|
error: error.message
|
|
});
|
|
}
|
|
});
|
|
|
|
// 更新购物车商品数量
|
|
router.put('/items/:id', async (req, res) => {
|
|
try {
|
|
const { id } = req.params;
|
|
const { quantity } = req.body;
|
|
const userId = req.user.id;
|
|
|
|
// 检查购物车项是否存在
|
|
const cartItem = await dbConnector.query(
|
|
'SELECT * FROM cart_items WHERE id = ? AND user_id = ?',
|
|
[id, userId]
|
|
);
|
|
|
|
if (cartItem.length === 0) {
|
|
return res.status(404).json({
|
|
code: 404,
|
|
message: '购物车项不存在'
|
|
});
|
|
}
|
|
|
|
await dbConnector.query(
|
|
'UPDATE cart_items SET quantity = ?, updated_at = NOW() WHERE id = ?',
|
|
[quantity, id]
|
|
);
|
|
|
|
res.json({
|
|
code: 200,
|
|
message: '更新成功'
|
|
});
|
|
} catch (error) {
|
|
console.error('更新购物车失败:', error);
|
|
res.status(500).json({
|
|
code: 500,
|
|
message: '服务器内部错误',
|
|
error: error.message
|
|
});
|
|
}
|
|
});
|
|
|
|
// 删除购物车商品
|
|
router.delete('/items/:id', async (req, res) => {
|
|
try {
|
|
const { id } = req.params;
|
|
const userId = req.user.id;
|
|
|
|
await dbConnector.query(
|
|
'DELETE FROM cart_items WHERE id = ? AND user_id = ?',
|
|
[id, userId]
|
|
);
|
|
|
|
res.json({
|
|
code: 200,
|
|
message: '删除成功'
|
|
});
|
|
} catch (error) {
|
|
console.error('删除购物车失败:', error);
|
|
res.status(500).json({
|
|
code: 500,
|
|
message: '服务器内部错误',
|
|
error: error.message
|
|
});
|
|
}
|
|
});
|
|
|
|
module.exports = router; |