const { fetchComments } = require('../../services/comments/fetchComments'); const { fetchCommentsCount } = require('../../services/comments/fetchCommentsCount'); Page({ data: { spuId: 1, commentsList: [], commentsStatistics: { badCount: 0, commentCount: 0, goodCount: 0, goodRate: 0, hasImageCount: 0, middleCount: 0, }, isLoading: false, debugInfo: { apiCalled: false, dataReceived: false, errorMessage: '', rawData: null, processedData: null } }, onLoad() { console.log('评论显示测试页面加载'); this.loadCommentData(); }, async loadCommentData() { this.setData({ isLoading: true, 'debugInfo.apiCalled': false, 'debugInfo.dataReceived': false, 'debugInfo.errorMessage': '', 'debugInfo.rawData': null, 'debugInfo.processedData': null }); try { // 获取评论统计 await this.getCommentsStatistics(this.data.spuId); // 获取评论列表 await this.getCommentsList(this.data.spuId); this.setData({ isLoading: false, 'debugInfo.dataReceived': true }); } catch (error) { console.error('加载评论数据失败:', error); this.setData({ isLoading: false, 'debugInfo.errorMessage': error.message || '未知错误' }); } }, // 获取评论统计数据 - 复制商品详情页面的逻辑 async getCommentsStatistics(spuId) { try { this.setData({ 'debugInfo.apiCalled': true }); console.log('调用 fetchCommentsCount,spuId:', spuId); const res = await fetchCommentsCount(spuId); console.log('fetchCommentsCount 返回结果:', res); this.setData({ 'debugInfo.rawData': res }); if (res) { const goodRate = res.total_count > 0 ? Math.round((res.good_count / res.total_count) * 100) : 0; const commentsStatistics = { commentCount: res.total_count || 0, goodCount: res.good_count || 0, middleCount: res.medium_count || 0, badCount: res.bad_count || 0, goodRate: goodRate, hasImageCount: res.image_count || 0, }; console.log('处理后的统计数据:', commentsStatistics); console.log('显示条件 (commentCount > 0):', commentsStatistics.commentCount > 0); this.setData({ commentsStatistics: commentsStatistics, 'debugInfo.processedData': commentsStatistics }); } } catch (error) { console.error('获取评论统计失败:', error); this.setData({ 'debugInfo.errorMessage': error.message || '获取评论统计失败', commentsStatistics: { commentCount: 0, goodCount: 0, middleCount: 0, badCount: 0, goodRate: 0, hasImageCount: 0, } }); } }, // 获取评论列表数据 - 复制商品详情页面的逻辑 async getCommentsList(spuId) { try { console.log('调用 fetchComments,参数:', { productId: spuId, page: 1, pageSize: 3 }); const res = await fetchComments({ productId: spuId, page: 1, pageSize: 3 }); console.log('fetchComments 返回结果:', res); if (res && res.comments) { // 转换评论数据格式 const commentsList = res.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 || []).map(imageUrl => ({ src: imageUrl, type: 'image' })), isAnonymity: comment.is_anonymous || false, sellerReply: comment.reply_content || '', goodsDetailInfo: '' })); console.log('处理后的评论列表:', commentsList); this.setData({ commentsList: commentsList }); } } catch (error) { console.error('获取评论列表失败:', error); this.setData({ commentsList: [] }); } }, // 格式化时间 formatTime(dateString) { if (!dateString) return ''; try { const date = new Date(dateString); return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`; } catch (error) { return dateString; } }, // 重新加载数据 reloadData() { this.loadCommentData(); }, // 清除数据 clearData() { this.setData({ commentsList: [], commentsStatistics: { badCount: 0, commentCount: 0, goodCount: 0, goodRate: 0, hasImageCount: 0, middleCount: 0, }, debugInfo: { apiCalled: false, dataReceived: false, errorMessage: '', rawData: null, processedData: null } }); } });