From 486d3179d35a861622fc5da64d619ffb7067e1fc Mon Sep 17 00:00:00 2001 From: dzl <786316265@qq.com> Date: Tue, 14 Oct 2025 09:17:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=8E=A8=E8=8D=90=E5=95=86?= =?UTF-8?q?=E5=93=81=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- routes/products.js | 53 ++++++++++++++++------------------------------ 1 file changed, 18 insertions(+), 35 deletions(-) diff --git a/routes/products.js b/routes/products.js index 89e2536..42b5cc4 100644 --- a/routes/products.js +++ b/routes/products.js @@ -965,6 +965,7 @@ router.get('/:id/recommended', async (req, res) => { categoryIds.push(...secondCategoryIds.map(item => item.id)); let recommendedProducts = []; + let filteredRecommendProductIds = []; // 如果有分类ID,先获取同类商品 if (categoryIds.length > 0) { @@ -974,8 +975,7 @@ router.get('/:id/recommended', async (req, res) => { const [recommendProductIds] = await getDB().execute(recommendId, categoryIds); console.log(1234, recommendProductIds); - const filteredRecommendProductIds = [...new Set(recommendProductIds.map(item => item.product_id))]; - console.log(12345, filteredRecommendProductIds); + filteredRecommendProductIds = [...new Set(recommendProductIds.map(item => item.product_id))]; // 获取同类别的其他商品作为推荐 if (filteredRecommendProductIds.length > 0) { @@ -991,40 +991,23 @@ router.get('/:id/recommended', async (req, res) => { } } - // 如果同类别商品不足,补充其他热门商品 - // if (recommendedProducts.length < 6) { - // 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 NOT IN (${filteredRecommendProductIds.map(() => '?').join(',')}) - // ORDER BY RAND() - // LIMIT ? - // `; - // } // 如果同类别商品不足,补充其他热门商品 - // if (recommendedProducts.length < 6) { - // const remainingCount = 6 - recommendedProducts.length; - // console.log(11111,typeof remainingCount,remainingCount); - // if (remainingCount > 0) { - // const additionalQuery = ` - // SELECT id, name, price, points_price as points, - // stock, image_url as image, description - // FROM products - // WHERE id != ? - // ORDER BY RAND() - // LIMIT ? - // `; - - // const [additionalProducts] = await getDB().execute( - // additionalQuery, - // [id, remainingCount] // 现在有两个占位符:id 和 limit - // ); - - // recommendedProducts.push(...additionalProducts); - // } - // } + 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 + ORDER BY RAND() + LIMIT ${6 - recommendedProducts.length} + `; + const [additionalProducts] = await getDB().execute( + additionalQuery, + ); + recommendedProducts.push(...additionalProducts); + } res.json({ success: true,