From be8b182e821b90956755ef73e23c3941f003564b Mon Sep 17 00:00:00 2001 From: dzl <786316265@qq.com> Date: Wed, 22 Oct 2025 17:26:50 +0800 Subject: [PATCH] =?UTF-8?q?bug=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- routes/cart.js | 132 ++++++++++++++++++++++++----------------------- routes/orders.js | 32 +++++++----- 2 files changed, 86 insertions(+), 78 deletions(-) diff --git a/routes/cart.js b/routes/cart.js index 6cb109f..39365d4 100644 --- a/routes/cart.js +++ b/routes/cart.js @@ -473,71 +473,6 @@ router.put('/:id', auth, async (req, res) => { } }); -/** - * @swagger - * /api/cart/{id}: - * delete: - * summary: 删除购物车商品 - * tags: [Cart] - * security: - * - bearerAuth: [] - * parameters: - * - in: path - * name: id - * required: true - * schema: - * type: integer - * description: 购物车项ID - * responses: - * 200: - * description: 删除购物车商品成功 - * content: - * application/json: - * schema: - * type: object - * properties: - * success: - * type: boolean - * message: - * type: string - * 401: - * description: 未授权 - * 404: - * description: 购物车项不存在 - * 500: - * description: 服务器错误 - */ -router.delete('/:id', auth, async (req, res) => { - try { - const cartItemId = req.params.id; - const userId = req.user.id; - - // 检查购物车项是否存在且属于当前用户 - const [cartItems] = await getDB().execute( - 'SELECT id FROM cart_items WHERE id = ? AND user_id = ?', - [cartItemId, userId] - ); - - if (cartItems.length === 0) { - return res.status(404).json({ success: false, message: '购物车项不存在' }); - } - - // 删除购物车项 - await getDB().execute( - 'DELETE FROM cart_items WHERE id = ?', - [cartItemId] - ); - - res.json({ - success: true, - message: '删除购物车商品成功' - }); - } catch (error) { - console.error('删除购物车商品失败:', error); - res.status(500).json({ success: false, message: '删除购物车商品失败' }); - } -}); - /** * @swagger * /api/cart/batch: @@ -654,6 +589,7 @@ router.delete('/clear', auth, async (req, res) => { success: true, message: '清空购物车成功' }); + console.log(11111111111111) } catch (error) { console.error('清空购物车失败:', error); res.status(500).json({ success: false, message: '清空购物车失败' }); @@ -932,4 +868,70 @@ router.post('/checkout', auth, async (req, res) => { } }); +/** + * @swagger + * /api/cart/{id}: + * delete: + * summary: 删除购物车商品 + * tags: [Cart] + * security: + * - bearerAuth: [] + * parameters: + * - in: path + * name: id + * required: true + * schema: + * type: integer + * description: 购物车项ID + * responses: + * 200: + * description: 删除购物车商品成功 + * content: + * application/json: + * schema: + * type: object + * properties: + * success: + * type: boolean + * message: + * type: string + * 401: + * description: 未授权 + * 404: + * description: 购物车项不存在 + * 500: + * description: 服务器错误 + */ +router.delete('/:id', auth, async (req, res) => { + try { + console.log(111111111) + const cartItemId = req.params.id; + const userId = req.user.id; + + // 检查购物车项是否存在且属于当前用户 + const [cartItems] = await getDB().execute( + 'SELECT id FROM cart_items WHERE id = ? AND user_id = ?', + [cartItemId, userId] + ); + + if (cartItems.length === 0) { + return res.status(404).json({ success: false, message: '购物车项不存在' }); + } + + // 删除购物车项 + await getDB().execute( + 'DELETE FROM cart_items WHERE id = ?', + [cartItemId] + ); + + res.json({ + success: true, + message: '删除购物车商品成功' + }); + } catch (error) { + console.error('删除购物车商品失败:', error); + res.status(500).json({ success: false, message: '删除购物车商品失败' }); + } +}); + module.exports = router; \ No newline at end of file diff --git a/routes/orders.js b/routes/orders.js index 0fdbd09..278d8da 100644 --- a/routes/orders.js +++ b/routes/orders.js @@ -496,20 +496,23 @@ router.post('/create-from-cart', auth, async (req, res) => { totalAmount += finalPrice * item.quantity; // 根据支付方式计算积分和融豆需求 - const hasPoints = productPaymentMethods.includes('points') || productPaymentMethods.includes('points_rongdou'); - const hasRongdou = productPaymentMethods.includes('rongdou') || productPaymentMethods.includes('points_rongdou'); + // const hasPoints = productPaymentMethods.includes('points') || productPaymentMethods.includes('points_rongdou'); + // const hasRongdou = productPaymentMethods.includes('rongdou') || productPaymentMethods.includes('points_rongdou'); - if (hasPoints && !hasRongdou) { - // 仅积分支付:按10000积分=1融豆计算 - totalPoints += finalRongdouPrice * item.quantity * 10000; - totalRongdou += finalRongdouPrice * item.quantity; - } else if (!hasPoints && hasRongdou) { - // 仅融豆支付 - totalRongdou += finalRongdouPrice * item.quantity; - } else { - // 组合支付或默认:记录融豆价格,前端可选择支付方式 - totalRongdou += finalRongdouPrice * item.quantity; - } + // if (hasPoints && !hasRongdou) { + // // 仅积分支付:按10000积分=1融豆计算 + // totalPoints += finalRongdouPrice * item.quantity * 10000; + // totalRongdou += finalRongdouPrice * item.quantity; + // } else if (!hasPoints && hasRongdou) { + // // 仅融豆支付 + // totalRongdou += finalRongdouPrice * item.quantity; + // } else { + // // 组合支付或默认:记录融豆价格,前端可选择支付方式 + // totalRongdou += finalRongdouPrice * item.quantity; + // } + totalPoints += item.points_price * item.quantity; + totalRongdou += item.rongdou_price * item.quantity; + // console.log(1111,item) } // 去重支付方式 @@ -1176,6 +1179,8 @@ router.post('/confirm-payment', auth, async (req, res) => { } user.balance = Math.abs(user.balance); + console.log(123456,paymentMethod) + // 开始扣钱 switch (paymentMethod) { case 'points': @@ -1184,6 +1189,7 @@ router.post('/confirm-payment', auth, async (req, res) => { await connection.rollback(); return res.status(400).json({ success: false, message: '积分不足' }); } + console.log(1234567,paymentMethod,userId,order.total_points) await connection.execute( 'UPDATE users SET points = points - ? WHERE id = ?', [order.total_points, userId]