From 69a445cebdcd2d5304e8ae0ef472039993328248 Mon Sep 17 00:00:00 2001 From: dzl <786316265@qq.com> Date: Tue, 14 Oct 2025 16:25:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E4=BC=98=E6=83=A0=E5=8D=B7?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E5=92=8C=E5=9B=BE=E7=89=87=E4=B8=8A=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- routes/category.js | 10 +--------- routes/coupon.js | 33 ++++++++++++++++++++++++++++++++ routes/products.js | 47 +++++++++++++++++++++++++++++++++------------- 3 files changed, 68 insertions(+), 22 deletions(-) diff --git a/routes/category.js b/routes/category.js index 4e18eea..d3a08dd 100644 --- a/routes/category.js +++ b/routes/category.js @@ -7,25 +7,17 @@ router.get('/', async (req, res) => { const db = await getDB(); const [firstCategory] = await db.query('SELECT * FROM category WHERE level = 1'); - console.log(firstCategory); for (const category of firstCategory) { const [secondCategories] = await db.query('SELECT * FROM category WHERE parent_id = ?', [category.id]); category.relative = secondCategories; } - - - - - - - - res.json({ success: true, data: firstCategory.map(category => ({ name: category.category_name, relative: category.relative.map(secondCategory => ({ name: secondCategory.category_name, + img: secondCategory.image, })) }))}); } catch (error) { diff --git a/routes/coupon.js b/routes/coupon.js index a150107..898f997 100644 --- a/routes/coupon.js +++ b/routes/coupon.js @@ -68,4 +68,37 @@ router.get('/:id', async (req, res) => { } }); +router.get('/user/:id', async (req, res) => { + try { + const db = getDB(); + const userId = req.params.id; + + const query = ` + SELECT * FROM coupon_use WHERE user_id = ? + ` + const [coupon] = await db.query(query, [userId]); + + for (const item of coupon) { + const query = ` + SELECT * FROM coupon_products WHERE id = ? + ` + const [couponInfo] = await db.query(query, [item.coupon_id]); + item.couponInfo = couponInfo[0]; + item.couponInfo.products = []; + for (const product of item.couponInfo.products_id) { + const query = ` + SELECT name FROM products WHERE id = ? + ` + const [productInfo] = await db.query(query, [product]); + item.couponInfo.products.push(productInfo[0]); + } + } + + res.json({success: true, coupon}); + } catch (error) { + console.log(error); + res.status(500).json({ error: '加载优惠券失败' }); + } +}) + module.exports = router; \ No newline at end of file diff --git a/routes/products.js b/routes/products.js index 42b5cc4..aaa8924 100644 --- a/routes/products.js +++ b/routes/products.js @@ -973,7 +973,6 @@ router.get('/:id/recommended', async (req, res) => { SELECT * FROM products_category WHERE category_id IN (${categoryIds.map(() => '?').join(',')}) `; const [recommendProductIds] = await getDB().execute(recommendId, categoryIds); - console.log(1234, recommendProductIds); filteredRecommendProductIds = [...new Set(recommendProductIds.map(item => item.product_id))]; @@ -993,20 +992,42 @@ router.get('/:id/recommended', async (req, res) => { // 如果同类别商品不足,补充其他热门商品 if (recommendedProducts.length < 6) { - console.log(123456, filteredRecommendProductIds); - // const remainingCount = 6 - recommendedProducts.length; - const additionalQuery = ` - SELECT id, name, price, points_price as points, - stock, image_url as image, description - FROM products - WHERE id != 3 + + const recommendQuery = ` + SELECT products_id FROM recommend_product + WHERE products_id NOT IN (${filteredRecommendProductIds.map(() => '?').join(',')}) ORDER BY RAND() LIMIT ${6 - recommendedProducts.length} - `; - const [additionalProducts] = await getDB().execute( - additionalQuery, - ); - recommendedProducts.push(...additionalProducts); + ` + const [recommendProductIds] = await getDB().execute(recommendQuery, [...filteredRecommendProductIds]); + filteredRecommendProductIds.push(...recommendProductIds.map(item => item.products_id)); + for (const item of recommendProductIds) { + const recommendQuery = ` + SELECT id, name, price, points_price as points, + stock, image_url as image, description + FROM products + WHERE id = ? + `; + const [recommendProduct] = await getDB().execute(recommendQuery, [item.products_id]); + recommendedProducts.push(recommendProduct[0]); + } + if (recommendProductIds.length + recommendedProducts.length < 6) { + // 补充其他热门商品 + const additionalQuery = ` + SELECT id, name, price, points_price as points, + stock, image_url as image, description + FROM products + WHERE id NOT IN (${filteredRecommendProductIds.map(() => '?').join(',')}) + AND id != 3 + ORDER BY RAND() + LIMIT ${6 - recommendedProducts.length} + `; + const [additionalProducts] = await getDB().execute( + additionalQuery, + filteredRecommendProductIds + ); + recommendedProducts.push(...additionalProducts); + } } res.json({