189 lines
5.5 KiB
JavaScript
189 lines
5.5 KiB
JavaScript
|
|
// pages/debug/api-test.js
|
||
|
|
const { fetchCommentsCount } = require('../../services/comments/fetchCommentsCount');
|
||
|
|
const { fetchComments } = require('../../services/comments/fetchComments');
|
||
|
|
|
||
|
|
Page({
|
||
|
|
data: {
|
||
|
|
spuId: 1, // 测试用的商品ID
|
||
|
|
testResults: {
|
||
|
|
statsApiCalled: false,
|
||
|
|
statsApiSuccess: false,
|
||
|
|
statsApiError: '',
|
||
|
|
statsRawData: null,
|
||
|
|
statsProcessedData: null,
|
||
|
|
|
||
|
|
listApiCalled: false,
|
||
|
|
listApiSuccess: false,
|
||
|
|
listApiError: '',
|
||
|
|
listRawData: null,
|
||
|
|
listProcessedData: null,
|
||
|
|
|
||
|
|
finalCommentsStatistics: null,
|
||
|
|
finalCommentsList: null,
|
||
|
|
shouldDisplay: false
|
||
|
|
}
|
||
|
|
},
|
||
|
|
|
||
|
|
onLoad: function (options) {
|
||
|
|
console.log('API测试页面加载');
|
||
|
|
if (options.spuId) {
|
||
|
|
this.setData({
|
||
|
|
spuId: parseInt(options.spuId)
|
||
|
|
});
|
||
|
|
}
|
||
|
|
},
|
||
|
|
|
||
|
|
// 测试评论统计API
|
||
|
|
async testCommentsStatistics() {
|
||
|
|
console.log('开始测试评论统计API...');
|
||
|
|
|
||
|
|
this.setData({
|
||
|
|
'testResults.statsApiCalled': true,
|
||
|
|
'testResults.statsApiSuccess': false,
|
||
|
|
'testResults.statsApiError': ''
|
||
|
|
});
|
||
|
|
|
||
|
|
try {
|
||
|
|
const result = await fetchCommentsCount(this.data.spuId);
|
||
|
|
console.log('评论统计API原始响应:', result);
|
||
|
|
|
||
|
|
// 模拟商品详情页的数据处理逻辑
|
||
|
|
const processedData = {
|
||
|
|
total_count: result.total_count || 0,
|
||
|
|
good_count: result.good_count || 0,
|
||
|
|
medium_count: result.medium_count || 0,
|
||
|
|
bad_count: result.bad_count || 0,
|
||
|
|
average_rating: result.average_rating || 0,
|
||
|
|
image_count: result.image_count || 0
|
||
|
|
};
|
||
|
|
|
||
|
|
const goodRate = processedData.total_count > 0 ?
|
||
|
|
Math.round((processedData.good_count / processedData.total_count) * 100) : 0;
|
||
|
|
|
||
|
|
const commentsStatistics = {
|
||
|
|
commentCount: processedData.total_count || 0,
|
||
|
|
goodCount: processedData.good_count || 0,
|
||
|
|
middleCount: processedData.medium_count || 0,
|
||
|
|
badCount: processedData.bad_count || 0,
|
||
|
|
goodRate: goodRate,
|
||
|
|
hasImageCount: processedData.image_count || 0,
|
||
|
|
};
|
||
|
|
|
||
|
|
this.setData({
|
||
|
|
'testResults.statsApiSuccess': true,
|
||
|
|
'testResults.statsRawData': result,
|
||
|
|
'testResults.statsProcessedData': processedData,
|
||
|
|
'testResults.finalCommentsStatistics': commentsStatistics,
|
||
|
|
'testResults.shouldDisplay': commentsStatistics.commentCount > 0
|
||
|
|
});
|
||
|
|
|
||
|
|
console.log('评论统计处理完成:', commentsStatistics);
|
||
|
|
|
||
|
|
} catch (error) {
|
||
|
|
console.error('评论统计API调用失败:', error);
|
||
|
|
this.setData({
|
||
|
|
'testResults.statsApiError': error.message || '未知错误'
|
||
|
|
});
|
||
|
|
}
|
||
|
|
},
|
||
|
|
|
||
|
|
// 测试评论列表API
|
||
|
|
async testCommentsList() {
|
||
|
|
console.log('开始测试评论列表API...');
|
||
|
|
|
||
|
|
this.setData({
|
||
|
|
'testResults.listApiCalled': true,
|
||
|
|
'testResults.listApiSuccess': false,
|
||
|
|
'testResults.listApiError': ''
|
||
|
|
});
|
||
|
|
|
||
|
|
try {
|
||
|
|
const result = await fetchComments({
|
||
|
|
productId: this.data.spuId,
|
||
|
|
page: 1,
|
||
|
|
pageSize: 3
|
||
|
|
});
|
||
|
|
console.log('评论列表API原始响应:', result);
|
||
|
|
|
||
|
|
// 模拟商品详情页的数据处理逻辑
|
||
|
|
let commentsList = [];
|
||
|
|
if (result.comments && Array.isArray(result.comments)) {
|
||
|
|
commentsList = result.comments.map(comment => ({
|
||
|
|
id: comment.id,
|
||
|
|
userName: comment.is_anonymous ? '匿名用户' : (comment.user_name || '用户'),
|
||
|
|
userHeadUrl: comment.user_avatar || 'https://tdesign.gtimg.com/miniprogram/template/retail/avatar/avatar1.png',
|
||
|
|
commentScore: comment.rating || 5,
|
||
|
|
commentContent: comment.content || '',
|
||
|
|
commentTime: comment.created_at ? this.formatTime(comment.created_at) : '',
|
||
|
|
specInfo: comment.product_spec || '',
|
||
|
|
commentResources: comment.images || [],
|
||
|
|
isAnonymity: comment.is_anonymous || false,
|
||
|
|
sellerReply: comment.reply_content || '',
|
||
|
|
goodsDetailInfo: ''
|
||
|
|
}));
|
||
|
|
}
|
||
|
|
|
||
|
|
this.setData({
|
||
|
|
'testResults.listApiSuccess': true,
|
||
|
|
'testResults.listRawData': result,
|
||
|
|
'testResults.listProcessedData': commentsList,
|
||
|
|
'testResults.finalCommentsList': commentsList
|
||
|
|
});
|
||
|
|
|
||
|
|
console.log('评论列表处理完成:', commentsList);
|
||
|
|
|
||
|
|
} catch (error) {
|
||
|
|
console.error('评论列表API调用失败:', error);
|
||
|
|
this.setData({
|
||
|
|
'testResults.listApiError': error.message || '未知错误'
|
||
|
|
});
|
||
|
|
}
|
||
|
|
},
|
||
|
|
|
||
|
|
// 运行完整测试
|
||
|
|
async runFullTest() {
|
||
|
|
console.log('开始运行完整API测试...');
|
||
|
|
await this.testCommentsStatistics();
|
||
|
|
await this.testCommentsList();
|
||
|
|
console.log('完整API测试完成');
|
||
|
|
},
|
||
|
|
|
||
|
|
// 清除测试结果
|
||
|
|
clearResults() {
|
||
|
|
this.setData({
|
||
|
|
testResults: {
|
||
|
|
statsApiCalled: false,
|
||
|
|
statsApiSuccess: false,
|
||
|
|
statsApiError: '',
|
||
|
|
statsRawData: null,
|
||
|
|
statsProcessedData: null,
|
||
|
|
|
||
|
|
listApiCalled: false,
|
||
|
|
listApiSuccess: false,
|
||
|
|
listApiError: '',
|
||
|
|
listRawData: null,
|
||
|
|
listProcessedData: null,
|
||
|
|
|
||
|
|
finalCommentsStatistics: null,
|
||
|
|
finalCommentsList: null,
|
||
|
|
shouldDisplay: false
|
||
|
|
}
|
||
|
|
});
|
||
|
|
},
|
||
|
|
|
||
|
|
// 格式化时间
|
||
|
|
formatTime(dateString) {
|
||
|
|
const date = new Date(dateString);
|
||
|
|
const year = date.getFullYear();
|
||
|
|
const month = String(date.getMonth() + 1).padStart(2, '0');
|
||
|
|
const day = String(date.getDate()).padStart(2, '0');
|
||
|
|
return `${year}-${month}-${day}`;
|
||
|
|
},
|
||
|
|
|
||
|
|
// 修改测试商品ID
|
||
|
|
onSpuIdInput(e) {
|
||
|
|
this.setData({
|
||
|
|
spuId: parseInt(e.detail.value) || 1
|
||
|
|
});
|
||
|
|
}
|
||
|
|
});
|