init
This commit is contained in:
75
miniprogram/services/comments/fetchCommentsCount.js
Normal file
75
miniprogram/services/comments/fetchCommentsCount.js
Normal file
@@ -0,0 +1,75 @@
|
||||
const { config } = require('../../config/index');
|
||||
|
||||
/** 获取商品评论数 */
|
||||
function mockFetchCommentsCount(ID = 0) {
|
||||
const { delay } = require('../_utils/delay');
|
||||
const { getGoodsCommentsCount } = require('../../model/comments');
|
||||
return delay().then(() => getGoodsCommentsCount(ID));
|
||||
}
|
||||
|
||||
/** 获取商品评论数 */
|
||||
function fetchCommentsCount(productId = 0) {
|
||||
console.log('[fetchCommentsCount] 接收到的productId:', productId, '类型:', typeof productId);
|
||||
|
||||
// 验证productId参数
|
||||
if (productId === null || productId === undefined || productId === '' || productId === 'undefined' || productId === 'null') {
|
||||
console.error('[fetchCommentsCount] 商品ID为空或无效:', productId);
|
||||
return Promise.reject(new Error('商品ID不能为空'));
|
||||
}
|
||||
|
||||
// 确保productId是数字
|
||||
const numericProductId = Number(productId);
|
||||
console.log('[fetchCommentsCount] 转换后的数字ID:', numericProductId);
|
||||
|
||||
if (isNaN(numericProductId) || numericProductId <= 0) {
|
||||
console.error('[fetchCommentsCount] 商品ID不是有效的正整数:', productId, '->', numericProductId);
|
||||
return Promise.reject(new Error('商品ID必须是有效的正整数'));
|
||||
}
|
||||
|
||||
if (config.useMock) {
|
||||
return mockFetchCommentsCount(numericProductId);
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
wx.request({
|
||||
url: `${config.apiBase}/comments/products/${numericProductId}/stats`,
|
||||
method: 'GET',
|
||||
success: (res) => {
|
||||
if (res.statusCode === 200 && res.data.code === 200) {
|
||||
// 转换数据格式以匹配前端期望的结构
|
||||
const stats = res.data.data;
|
||||
const result = {
|
||||
// 原始数据保留
|
||||
total_count: stats.total_count || 0,
|
||||
average_rating: stats.average_rating || 0,
|
||||
rating_1_count: stats.rating_1_count || 0,
|
||||
rating_2_count: stats.rating_2_count || 0,
|
||||
rating_3_count: stats.rating_3_count || 0,
|
||||
rating_4_count: stats.rating_4_count || 0,
|
||||
rating_5_count: stats.rating_5_count || 0,
|
||||
good_count: (stats.rating_4_count || 0) + (stats.rating_5_count || 0),
|
||||
medium_count: stats.rating_3_count || 0,
|
||||
bad_count: (stats.rating_1_count || 0) + (stats.rating_2_count || 0),
|
||||
image_count: stats.has_images_count || stats.image_count || 0,
|
||||
|
||||
// 页面期望的数据格式
|
||||
commentCount: String(stats.total_count || 0),
|
||||
goodCount: String((stats.rating_4_count || 0) + (stats.rating_5_count || 0)),
|
||||
middleCount: String(stats.rating_3_count || 0),
|
||||
badCount: String((stats.rating_1_count || 0) + (stats.rating_2_count || 0)),
|
||||
hasImageCount: String(stats.has_images_count || stats.image_count || 0),
|
||||
uidCount: '0', // 用户自己的评论数,需要单独获取
|
||||
};
|
||||
resolve(result);
|
||||
} else {
|
||||
reject(new Error(res.data.message || '获取评论统计失败'));
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
reject(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = { fetchCommentsCount };
|
||||
Reference in New Issue
Block a user