Files
2025-11-17 13:32:54 +08:00

395 lines
11 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { fetchComments } from '../../../services/comments/fetchComments';
import { fetchCommentsCount } from '../../../services/comments/fetchCommentsCount';
import { fetchUserComments } from '../../../services/comments/createComment';
import dayjs from 'dayjs';
const layoutMap = {
0: 'vertical',
};
Page({
data: {
pageLoading: false,
commentList: [],
pageNum: 1,
myPageNum: 1,
pageSize: 10,
total: 0,
myTotal: 0,
hasLoaded: false,
layoutText: layoutMap[0],
loadMoreStatus: 0,
myLoadStatus: 0,
spuId: '1060004',
commentLevel: '',
hasImage: '',
commentType: '',
totalCount: 0,
countObj: {
badCount: '0',
commentCount: '0',
goodCount: '0',
middleCount: '0',
hasImageCount: '0',
uidCount: '0',
},
},
onLoad(options) {
console.log('[评论页面] onLoad 接收到的参数:', options);
const { productId, spuId } = options;
// 兼容新旧参数名
const id = productId || spuId;
console.log('[评论页面] 最终使用的spuId:', id);
if (id) {
this.setData({ spuId: id });
console.log('[评论页面] 开始获取评论统计...');
this.getCount({ spuId: id });
console.log('[评论页面] 开始获取评论列表...');
this.getComments({ spuId: id });
}
},
async getCount(options) {
const { spuId } = options;
console.log('[评论页面] getCount 开始spuId:', spuId, '类型:', typeof spuId);
try {
// 直接传递spuId作为productId参数
const result = await fetchCommentsCount(spuId);
console.log('[评论页面] fetchCommentsCount 返回数据:', result);
// 尝试获取用户自己的评论数
try {
const token = wx.getStorageSync('token');
if (token) {
const userCommentsData = await fetchUserComments({
page: 1,
pageSize: 1
});
if (userCommentsData && userCommentsData.total !== undefined) {
result.uidCount = String(userCommentsData.total);
console.log('[评论页面] 获取到用户评论数:', userCommentsData.total);
}
}
} catch (userError) {
console.log('[评论页面] 获取用户评论数失败使用默认值0:', userError.message);
// 保持默认值0不影响主要功能
}
console.log('[评论页面] 设置评论统计数据:', result);
this.setData({
countObj: result,
});
// const { data, code = '' } = result;
// if (code.toUpperCase() === 'SUCCESS') {
// wx.setNavigationBarTitle({
// title: `全部评价(${data.commentCount})`,
// });
// this.setData({
// countObj: data,
// });
// } else {
// wx.showToast({
// title: '查询失败,请稍候重试',
// });
// }
} catch (error) {
console.error('[评论页面] 获取评论统计失败:', error);
wx.showToast({
title: '获取评论失败,请稍候重试',
icon: 'none'
});
this.setData({
loadMoreStatus: 0,
});
}
},
generalQueryData(reset) {
const { hasImage, pageNum, pageSize, spuId, commentLevel } = this.data;
const params = {
pageNum: 1,
pageSize: 30,
queryParameter: {
spuId,
},
};
if (
Number(commentLevel) === 3 ||
Number(commentLevel) === 2 ||
Number(commentLevel) === 1
) {
params.queryParameter.commentLevel = Number(commentLevel);
}
if (hasImage && hasImage === '1') {
params.queryParameter.hasImage = true;
} else {
delete params.queryParameter.hasImage;
}
// 重置请求
if (reset) return params;
return {
...params,
pageNum: pageNum + 1,
pageSize,
};
},
async init(reset = true) {
console.log('[评论页面] init 开始reset:', reset);
const { loadMoreStatus, commentList = [], spuId, hasImage, commentLevel } = this.data;
console.log('[评论页面] 当前页面数据状态:', { loadMoreStatus, commentListLength: commentList.length, spuId, hasImage, commentLevel });
// 在加载中或者无更多数据,直接返回
if (loadMoreStatus !== 0) {
console.log('[评论页面] 跳过加载loadMoreStatus:', loadMoreStatus);
return;
}
console.log('[评论页面] 设置加载状态为1');
this.setData({
loadMoreStatus: 1,
});
// 构造正确的API参数格式
const apiParams = {
productId: spuId,
page: reset ? 1 : this.data.pageNum + 1,
pageSize: this.data.pageSize,
};
// 添加评分筛选
if (commentLevel && (commentLevel === '1' || commentLevel === '2' || commentLevel === '3')) {
apiParams.rating = Number(commentLevel);
}
// 添加图片筛选
if (hasImage === '1') {
apiParams.hasImages = true;
}
console.log('[评论页面] API调用参数:', apiParams);
try {
console.log('[评论页面] 开始调用fetchComments API...');
const data = await fetchComments(apiParams);
console.log('[评论页面] fetchComments API返回数据:', data);
if (data && data.comments) {
const { comments, totalCount = 0 } = data;
// 转换数据格式以匹配页面期望的结构
const pageList = comments.map((item) => ({
...item,
commentTime: dayjs(item.created_at).format('YYYY/MM/DD HH:mm'),
userName: item.user_name,
userHeadUrl: item.user_avatar,
commentScore: item.rating,
commentContent: item.content,
commentImageList: item.images || [],
commentResources: (item.images || []).map(imageUrl => ({
src: imageUrl,
type: 'image'
})),
isAnonymous: item.is_anonymous,
replyContent: item.reply_content,
specValue: item.product_spec
}));
if (Number(totalCount) === 0 && reset) {
this.setData({
commentList: [],
hasLoaded: true,
total: totalCount,
loadMoreStatus: 2,
});
return;
}
const _commentList = reset ? pageList : commentList.concat(pageList);
const _loadMoreStatus =
_commentList.length >= Number(totalCount) ? 2 : 0;
this.setData({
commentList: _commentList,
pageNum: data.pageNum || 1,
totalCount: Number(totalCount),
loadMoreStatus: _loadMoreStatus,
});
} else {
wx.showToast({
title: '查询失败,请稍候重试',
});
}
} catch (error) {}
this.setData({
hasLoaded: true,
});
},
getScoreArray(score) {
var array = [];
for (let i = 0; i < 5; i++) {
if (i < score) {
array.push(2);
} else {
array.push(0);
}
}
return array;
},
getComments(options) {
console.log('[评论页面] getComments 开始,参数:', options);
const { commentLevel = -1, spuId, hasImage = '' } = options;
if (commentLevel !== -1) {
this.setData({
commentLevel: commentLevel,
});
}
const updateData = {
hasImage: hasImage,
commentType: hasImage ? '4' : '',
spuId: spuId,
};
console.log('[评论页面] 设置评论筛选条件:', updateData);
this.setData(updateData);
console.log('[评论页面] 开始初始化评论列表...');
this.init(true);
},
// 获取我的评论列表
async getMyCommentsList() {
console.log('[评论页面] getMyCommentsList 开始');
const { myLoadStatus, myPageNum, pageSize } = this.data;
// 在加载中,直接返回
if (myLoadStatus !== 0) {
console.log('[评论页面] 跳过加载myLoadStatus:', myLoadStatus);
return;
}
console.log('[评论页面] 设置我的评论加载状态为1');
this.setData({
myLoadStatus: 1,
});
try {
console.log('[评论页面] 开始调用fetchUserComments API...');
const data = await fetchUserComments({
page: myPageNum,
pageSize: pageSize
});
console.log('[评论页面] fetchUserComments API返回数据:', data);
if (data && data.list) {
const { list, total = 0 } = data;
// 转换数据格式以匹配页面期望的结构
const pageList = list.map((item) => ({
...item,
commentTime: dayjs(item.created_at).format('YYYY/MM/DD HH:mm'),
userName: item.user_name || '我',
userHeadUrl: item.user_avatar || 'https://tdesign.gtimg.com/miniprogram/template/retail/avatar/avatar1.png',
commentScore: item.rating,
commentContent: item.content,
commentImageList: item.images || [],
commentResources: (item.images || []).map(imageUrl => ({
src: imageUrl,
type: 'image'
})),
isAnonymous: item.is_anonymous,
replyContent: item.reply_content,
specValue: item.product_spec
}));
if (Number(total) === 0) {
this.setData({
commentList: [],
myTotal: total,
myLoadStatus: 2,
});
return;
}
const _commentList = myPageNum === 1 ? pageList : this.data.commentList.concat(pageList);
const _myLoadStatus = _commentList.length >= Number(total) ? 2 : 0;
this.setData({
commentList: _commentList,
myPageNum: myPageNum + 1,
myTotal: Number(total),
myLoadStatus: _myLoadStatus,
});
} else {
console.log('[评论页面] fetchUserComments API返回空结果或无list字段');
this.setData({
commentList: [],
myLoadStatus: 2,
});
}
} catch (error) {
console.error('[评论页面] 获取我的评论列表失败:', error);
wx.showToast({
title: '获取我的评论失败',
icon: 'none'
});
this.setData({
myLoadStatus: 0,
});
}
},
changeTag(e) {
var { commenttype } = e.currentTarget.dataset;
var { commentType } = this.data;
if (commentType === commenttype) return;
this.setData({
loadMoreStatus: 0,
commentList: [],
total: 0,
myTotal: 0,
myPageNum: 1,
pageNum: 1,
});
if (commenttype === '' || commenttype === '5') {
this.setData({
hasImage: '',
commentLevel: '',
});
} else if (commenttype === '4') {
this.setData({
hasImage: '1',
commentLevel: '',
});
} else {
this.setData({
hasImage: '',
commentLevel: commenttype,
});
}
if (commenttype === '5') {
this.setData({
myLoadStatus: 1,
commentType: commenttype,
});
this.getMyCommentsList();
} else {
this.setData({
myLoadStatus: 0,
commentType: commenttype,
});
this.init(true);
}
},
onReachBottom() {
const { total = 0, commentList } = this.data;
if (commentList.length === total) {
this.setData({
loadMoreStatus: 2,
});
return;
}
this.init(false);
},
});