135 lines
4.3 KiB
JavaScript
135 lines
4.3 KiB
JavaScript
import { config } from '../../config/index';
|
|
|
|
/** 获取商品详情页评论数 */
|
|
function mockFetchGoodDetailsCommentsCount(spuId = 0) {
|
|
const { delay } = require('../_utils/delay');
|
|
const {
|
|
getGoodsDetailsCommentsCount,
|
|
} = require('../../model/detailsComments');
|
|
return delay().then(() => getGoodsDetailsCommentsCount(spuId));
|
|
}
|
|
|
|
/** 获取商品详情页评论数 */
|
|
export function getGoodsDetailsCommentsCount(spuId = 0) {
|
|
if (config.useMock) {
|
|
return mockFetchGoodDetailsCommentsCount(spuId);
|
|
}
|
|
|
|
return new Promise((resolve, reject) => {
|
|
wx.request({
|
|
url: `${config.apiBase}/products/${spuId}/reviews/count`,
|
|
method: 'GET',
|
|
header: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
success: (res) => {
|
|
if (res.statusCode === 200) {
|
|
// 转换后端数据格式为前端需要的格式
|
|
const data = {
|
|
commentCount: res.data.data.total || 0,
|
|
goodRate: res.data.data.good_rate || '100',
|
|
badCount: res.data.data.bad_count || 0,
|
|
goodCount: res.data.data.good_count || 0,
|
|
middleCount: res.data.data.middle_count || 0,
|
|
hasImageCount: res.data.data.has_image_count || 0,
|
|
};
|
|
resolve(data);
|
|
} else {
|
|
console.error('获取评论统计失败:', res);
|
|
// 失败时返回默认数据
|
|
resolve({
|
|
commentCount: 0,
|
|
goodRate: '100',
|
|
badCount: 0,
|
|
goodCount: 0,
|
|
middleCount: 0,
|
|
hasImageCount: 0,
|
|
});
|
|
}
|
|
},
|
|
fail: (error) => {
|
|
console.error('获取评论统计请求失败:', error);
|
|
// 网络错误时返回默认数据
|
|
resolve({
|
|
commentCount: 0,
|
|
goodRate: '100',
|
|
badCount: 0,
|
|
goodCount: 0,
|
|
middleCount: 0,
|
|
hasImageCount: 0,
|
|
});
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
/** 获取商品详情页评论 */
|
|
function mockFetchGoodDetailsCommentList(spuId = 0) {
|
|
const { delay } = require('../_utils/delay');
|
|
const { getGoodsDetailsComments } = require('../../model/detailsComments');
|
|
return delay().then(() => getGoodsDetailsComments(spuId));
|
|
}
|
|
|
|
/** 获取商品详情页评论 */
|
|
export function getGoodsDetailsCommentList(spuId = 0, page = 1, limit = 10) {
|
|
if (config.useMock) {
|
|
return mockFetchGoodDetailsCommentList(spuId);
|
|
}
|
|
|
|
return new Promise((resolve, reject) => {
|
|
wx.request({
|
|
url: `${config.apiBase}/products/${spuId}/reviews`,
|
|
method: 'GET',
|
|
data: {
|
|
page: page,
|
|
limit: limit,
|
|
},
|
|
header: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
success: (res) => {
|
|
if (res.statusCode === 200) {
|
|
// 转换后端数据格式为前端需要的格式
|
|
const reviews = res.data.data.list || [];
|
|
const transformedData = reviews.map(review => ({
|
|
spuId: review.product_id?.toString() || spuId.toString(),
|
|
skuId: review.sku_id?.toString() || '0',
|
|
specInfo: review.spec_info || '',
|
|
commentContent: review.content || '',
|
|
commentResources: review.images ? review.images.map(img => ({
|
|
src: img,
|
|
type: 'image'
|
|
})) : [],
|
|
commentScore: review.rating || 5,
|
|
uid: review.user_id?.toString() || '',
|
|
userName: review.user_name || '匿名用户',
|
|
userHeadUrl: review.user_avatar || 'https://tdesign.gtimg.com/miniprogram/template/retail/avatar/avatar1.png',
|
|
isAnonymity: review.is_anonymous || false,
|
|
commentTime: review.created_at ? new Date(review.created_at).getTime().toString() : Date.now().toString(),
|
|
isAutoComment: false,
|
|
sellerReply: review.reply || '',
|
|
goodsDetailInfo: review.spec_info || '',
|
|
}));
|
|
|
|
resolve({
|
|
homePageComments: transformedData
|
|
});
|
|
} else {
|
|
console.error('获取评论列表失败:', res);
|
|
// 失败时返回空数组
|
|
resolve({
|
|
homePageComments: []
|
|
});
|
|
}
|
|
},
|
|
fail: (error) => {
|
|
console.error('获取评论列表请求失败:', error);
|
|
// 网络错误时返回空数组
|
|
resolve({
|
|
homePageComments: []
|
|
});
|
|
}
|
|
});
|
|
});
|
|
}
|