From 5485e76affd3c3c947b5093659b43d72d396c642 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E9=A3=9E=E6=B4=8B?= <15778543+xufeiyang6017@user.noreply.gitee.com> Date: Tue, 13 Jan 2026 09:41:25 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E5=95=86=E5=93=81=E6=9F=A5?= =?UTF-8?q?=E7=9C=8B=E6=AC=A1=E6=95=B0=E7=BB=9F=E8=AE=A1=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/goods-detail/goods-detail.js | 9 +++ pages/index/index.wxml | 2 +- server-example/server-mysql.js | 95 +++++++++++++++++++++++++++++- utils/api.js | 6 ++ 4 files changed, 109 insertions(+), 3 deletions(-) diff --git a/pages/goods-detail/goods-detail.js b/pages/goods-detail/goods-detail.js index 8ccc31d..2858639 100644 --- a/pages/goods-detail/goods-detail.js +++ b/pages/goods-detail/goods-detail.js @@ -1338,6 +1338,15 @@ Page({ this.checkAndExtractVideoCover(); }, 500); + // 增加商品点击查看次数 + API.incrementProductFrequency({ productId: productIdStr }) + .then(res => { + console.log('增加商品点击次数成功:', res); + }) + .catch(err => { + console.error('增加商品点击次数失败:', err); + }); + // 只有当没有预加载的收藏状态时,才从服务器加载 if (!preloadedData || preloadedData.isFavorite === undefined) { this.loadGoodsFavoriteStatus(productIdStr); diff --git a/pages/index/index.wxml b/pages/index/index.wxml index bda5bda..1705ecf 100644 --- a/pages/index/index.wxml +++ b/pages/index/index.wxml @@ -192,7 +192,7 @@ 已售:{{item.originalTotalStock || 0}}件 - 已有{{item.reservedCount || 0}}人收藏 + 已有{{item.frequency || 0}}人浏览 {{item.region || ''}} diff --git a/server-example/server-mysql.js b/server-example/server-mysql.js index 25327ae..9ef1429 100644 --- a/server-example/server-mysql.js +++ b/server-example/server-mysql.js @@ -816,6 +816,13 @@ Product.init({ allowNull: false, comment: '已有几人想要' }, + // 新增查看次数字段 + frequency: { + type: DataTypes.INTEGER, + defaultValue: 0, + allowNull: false, + comment: '商品查看次数' + }, created_at: { type: DataTypes.DATE, defaultValue: Sequelize.NOW @@ -2008,7 +2015,8 @@ app.post('/api/product/list', async (req, res) => { 'supplyStatus', 'category', 'producting', - 'description' + 'description', + 'frequency' ], order: [['created_at', 'DESC']], limit: pageSize, @@ -3862,6 +3870,89 @@ function cleanTempFiles(filePaths) { } } +// 增加商品查看次数 +app.post('/api/products/increment-frequency', async (req, res) => { + try { + const { productId } = req.body; + + if (!productId) { + return res.status(400).json({ + success: false, + code: 400, + message: '缺少productId参数' + }); + } + + // 增加商品查看次数 + const result = await Product.increment('frequency', { + where: { + productId, + status: { [Sequelize.Op.not]: 'hidden' } + } + }); + + console.log('Product.increment result:', result); + console.log('Product.increment result[0]:', result[0]); + console.log('Product.increment result type:', typeof result); + + // 检查是否找到并更新了商品 + let updatedCount = 0; + if (Array.isArray(result)) { + updatedCount = result[0] || 0; + } else if (result && result[Sequelize.Op.increment]) { + // 处理Sequelize 6+的返回格式 + updatedCount = result[Sequelize.Op.increment] || 0; + } + + console.log('Updated count:', updatedCount); + + if (updatedCount === 0) { + return res.status(404).json({ + success: false, + code: 404, + message: '商品不存在或已被隐藏' + }); + } + + // 查询更新后的商品信息 + console.log('查询更新后的商品信息,productId:', productId); + const updatedProduct = await Product.findOne({ + attributes: ['productId', 'productName', 'frequency'], + where: { productId } + }); + + console.log('查询到的更新后商品信息:', updatedProduct); + + if (!updatedProduct) { + // 这种情况不应该发生,因为我们已经检查了increment操作是否成功 + return res.status(500).json({ + success: false, + code: 500, + message: '服务器错误,无法获取更新后的商品信息' + }); + } + + return res.status(200).json({ + success: true, + code: 200, + message: '商品查看次数增加成功', + data: { + productId: updatedProduct.productId, + productName: updatedProduct.productName, + frequency: updatedProduct.frequency + } + }); + } catch (error) { + console.error('增加商品查看次数失败:', error); + return res.status(500).json({ + success: false, + code: 500, + message: '服务器错误,增加商品查看次数失败', + error: error.message + }); + } +}); + // 获取商品详情 app.post('/api/products/detail', async (req, res) => { try { @@ -3877,7 +3968,7 @@ app.post('/api/products/detail', async (req, res) => { // 查询商品详情 - 排除hidden状态商品,直接使用Product表中的reservedCount字段 const product = await Product.findOne({ - attributes: ['productId', 'productName', 'price', 'quantity', 'grossWeight', 'imageUrls', 'created_at', 'specification', 'yolk', 'sourceType', 'supplyStatus', 'producting', 'product_contact', 'contact_phone', 'region', 'freshness', 'costprice','description'], + attributes: ['productId', 'productName', 'price', 'quantity', 'grossWeight', 'imageUrls', 'created_at', 'specification', 'yolk', 'sourceType', 'supplyStatus', 'producting', 'product_contact', 'contact_phone', 'region', 'freshness', 'costprice','description', 'frequency'], where: { productId, status: { [Sequelize.Op.not]: 'hidden' } diff --git a/utils/api.js b/utils/api.js index cadead6..871061b 100644 --- a/utils/api.js +++ b/utils/api.js @@ -3622,6 +3622,12 @@ module.exports = { return data; }); }, + + // 增加商品点击查看次数 + incrementProductFrequency: function ({ productId }) { + console.log('API.incrementProductFrequency - productId:', productId); + return request('/api/products/increment-frequency', 'POST', { productId: productId }); + }, // 预约商品 reserveProduct: function ({ id }) {