|
|
|
@ -1854,8 +1854,8 @@ Page({ |
|
|
|
phoneNumber: phoneNumber // 添加用户标识信息,用于判断是否可以删除
|
|
|
|
}; |
|
|
|
|
|
|
|
// 更新评论列表
|
|
|
|
const comments = [newComment, ...this.data.comments]; |
|
|
|
// 更新评论列表 - add new comment to the end
|
|
|
|
const comments = [...this.data.comments, newComment]; |
|
|
|
|
|
|
|
this.setData({ |
|
|
|
comments: comments, |
|
|
|
@ -1882,10 +1882,80 @@ Page({ |
|
|
|
title: '网络错误,评论提交失败', |
|
|
|
icon: 'none' |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}, |
|
|
|
|
|
|
|
// 20 default comments
|
|
|
|
getDefaultComments() { |
|
|
|
return [ |
|
|
|
"鸡蛋品相贼好无破损,规格统一超适合批发", |
|
|
|
"个头匀溜大小一致,装箱发货一点不费劲", |
|
|
|
"包装严实防震,整车运输下来个个完好", |
|
|
|
"蛋液浓稠清亮,品相达标完全符合供货要求", |
|
|
|
"性价比真绝了,新鲜度在线囤货超划算", |
|
|
|
"农家散养蛋品相佳,蛋黄紧实供货超稳定", |
|
|
|
"物流嗖嗖快,到货鸡蛋无磕碰超省心", |
|
|
|
"蛋壳干净无污渍,分拣打包效率直接拉满", |
|
|
|
"个个饱满无瘪壳,市场铺货回头客贼多", |
|
|
|
"分量超足规格齐,商超供货完全没毛病", |
|
|
|
"无抗生素达标蛋,走商超渠道妥妥放心", |
|
|
|
"蛋壳硬度够,装卸搬运全程零损耗", |
|
|
|
"防震包装太贴心,长途运输损耗率超低", |
|
|
|
"保鲜期够长,放一周品相依旧很能打", |
|
|
|
"蛋体完整无瑕疵,分拣挑拣省超多功夫", |
|
|
|
"品质稳定没色差,长期合作完全没问题", |
|
|
|
"货源稳定供货及时,补货节奏卡得刚刚好", |
|
|
|
"发货快包装硬,对接商超渠道超靠谱", |
|
|
|
"蛋黄蛋清分层好,加工拿货性价比拉满", |
|
|
|
"品质远超预期,后续订单必须锁定这家" |
|
|
|
]; |
|
|
|
}, |
|
|
|
|
|
|
|
// Seeded random number generator for consistent results
|
|
|
|
seededRandom(seed) { |
|
|
|
// Improved seeded random using a linear congruential generator
|
|
|
|
const a = 1103515245; |
|
|
|
const c = 12345; |
|
|
|
const m = Math.pow(2, 31); |
|
|
|
const nextSeed = (a * seed + c) % m; |
|
|
|
return nextSeed / m; |
|
|
|
}, |
|
|
|
|
|
|
|
// Get consistent random comments based on product ID
|
|
|
|
getConsistentRandomComments(productId, count = 2) { |
|
|
|
const defaultComments = this.getDefaultComments(); |
|
|
|
// Generate order-dependent seed by multiplying character codes with their positions
|
|
|
|
const seed = productId.toString().split('').reduce((sum, char, index) => sum + (char.charCodeAt(0) * (index + 1)), 0); |
|
|
|
|
|
|
|
// Create an array of comment objects with their original indices
|
|
|
|
const commentsWithIndices = defaultComments.map((comment, index) => ({ |
|
|
|
comment, |
|
|
|
index |
|
|
|
})); |
|
|
|
|
|
|
|
// Create a shuffled array based on the seed and comment index
|
|
|
|
const shuffled = [...commentsWithIndices].sort((a, b) => { |
|
|
|
const rand1 = this.seededRandom(seed + a.index); |
|
|
|
const rand2 = this.seededRandom(seed + b.index); |
|
|
|
return rand1 - rand2; |
|
|
|
}); |
|
|
|
|
|
|
|
// Return the first 'count' comments
|
|
|
|
return shuffled.slice(0, count).map((item, index) => ({ |
|
|
|
id: `default_comment_${productId}_${index}`, |
|
|
|
nickname: '匿名用户', |
|
|
|
avatar: 'https://via.placeholder.com/40', |
|
|
|
comments: item.comment, |
|
|
|
like: Math.floor(this.seededRandom(seed + index) * 50), |
|
|
|
hate: Math.floor(this.seededRandom(seed + index + 100) * 5), |
|
|
|
liked: false, |
|
|
|
hated: false, |
|
|
|
replies: [], |
|
|
|
phoneNumber: '', |
|
|
|
isDefault: true |
|
|
|
})); |
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
// 加载商品评论
|
|
|
|
loadComments: function(productId) { |
|
|
|
console.log('开始加载商品评论,商品ID:', productId); |
|
|
|
@ -1921,17 +1991,33 @@ Page({ |
|
|
|
commentsData = []; |
|
|
|
} |
|
|
|
|
|
|
|
// 检查返回的评论是否都属于当前用户
|
|
|
|
const allCommentsBelongToCurrentUser = commentsData.every(comment => |
|
|
|
comment.phoneNumber === this.data.currentUserPhone |
|
|
|
); |
|
|
|
console.log('所有评论是否都属于当前用户:', allCommentsBelongToCurrentUser); |
|
|
|
// Remove duplicate comments
|
|
|
|
const uniqueComments = []; |
|
|
|
const seenComments = new Set(); |
|
|
|
commentsData.forEach(comment => { |
|
|
|
const commentKey = comment.comments + (comment.phoneNumber || ''); |
|
|
|
if (!seenComments.has(commentKey)) { |
|
|
|
seenComments.add(commentKey); |
|
|
|
uniqueComments.push(comment); |
|
|
|
} |
|
|
|
}); |
|
|
|
commentsData = uniqueComments; |
|
|
|
|
|
|
|
// 如果所有评论都属于当前用户,可能服务器没有返回所有评论
|
|
|
|
if (allCommentsBelongToCurrentUser && commentsData.length > 0) { |
|
|
|
console.warn('所有评论都属于当前用户,可能服务器没有返回所有评论'); |
|
|
|
console.warn('当前用户手机号:', this.data.currentUserPhone); |
|
|
|
} |
|
|
|
// Always add default comments at the beginning
|
|
|
|
const defaultComments = this.getConsistentRandomComments(productId, 2); |
|
|
|
commentsData = [...defaultComments, ...commentsData]; |
|
|
|
|
|
|
|
// 检查返回的评论是否都属于当前用户
|
|
|
|
const allCommentsBelongToCurrentUser = commentsData.every(comment => |
|
|
|
comment.phoneNumber === this.data.currentUserPhone |
|
|
|
); |
|
|
|
console.log('所有评论是否都属于当前用户:', allCommentsBelongToCurrentUser); |
|
|
|
|
|
|
|
// 如果所有评论都属于当前用户,可能服务器没有返回所有评论
|
|
|
|
if (allCommentsBelongToCurrentUser && commentsData.length > 0) { |
|
|
|
console.warn('所有评论都属于当前用户,可能服务器没有返回所有评论'); |
|
|
|
console.warn('当前用户手机号:', this.data.currentUserPhone); |
|
|
|
} |
|
|
|
|
|
|
|
// 格式化评论时间为相对时间
|
|
|
|
const formattedComments = commentsData.map(comment => ({ |
|
|
|
@ -1952,9 +2038,11 @@ Page({ |
|
|
|
.catch(err => { |
|
|
|
console.error('获取评论失败:', err); |
|
|
|
console.error('错误详情:', JSON.stringify(err, null, 2)); |
|
|
|
// 加载失败时设置空数组,触发空评论状态的显示
|
|
|
|
// 加载失败时使用默认评论
|
|
|
|
console.log('使用默认评论'); |
|
|
|
const defaultComments = this.getConsistentRandomComments(productId, 2); |
|
|
|
this.setData({ |
|
|
|
comments: [] |
|
|
|
comments: defaultComments |
|
|
|
}); |
|
|
|
}); |
|
|
|
}, |
|
|
|
|