Browse Source

实现商品查看次数统计功能

pull/12/head
徐飞洋 2 months ago
parent
commit
5485e76aff
  1. 9
      pages/goods-detail/goods-detail.js
  2. 2
      pages/index/index.wxml
  3. 95
      server-example/server-mysql.js
  4. 6
      utils/api.js

9
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);

2
pages/index/index.wxml

@ -192,7 +192,7 @@
<view wx:if="{{item.status === 'sold_out'}}" class="status-tag item-count">已售:{{item.originalTotalStock || 0}}件</view>
</view>
<view class="product-meta">
<text class="sales-count">已有{{item.reservedCount || 0}}人收藏</text>
<text class="sales-count"> 已有{{item.frequency || 0}}人浏览</text>
<text class="product-location">{{item.region || ''}}</text>
</view>
</view>

95
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' }

6
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 }) {

Loading…
Cancel
Save