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