Browse Source

修复评论审核功能:确保review字段默认为0

pull/18/head
徐飞洋 1 month ago
parent
commit
5f444e9b37
  1. 25
      pages/goods-detail/goods-detail.js
  2. 13
      server-example/server-mysql.js

25
pages/goods-detail/goods-detail.js

@ -1815,11 +1815,12 @@ Page({
const productId = this.data.goodsDetail.productId || this.data.goodsDetail.id;
// 准备评论数据
// 只发送服务器需要的参数:productId、phoneNumber和comments
// 只发送服务器需要的参数:productId、phoneNumber、comments和review
const commentData = {
productId: String(productId),
phoneNumber: phoneNumber ? String(phoneNumber) : null,
comments: content
comments: content,
review: 0 // 新提交的评论默认为待审核状态
};
// 调试日志
@ -1851,7 +1852,8 @@ Page({
liked: false,
hated: false,
replies: [],
phoneNumber: phoneNumber // 添加用户标识信息,用于判断是否可以删除
phoneNumber: phoneNumber, // 添加用户标识信息,用于判断是否可以删除
review: 0 // 新提交的评论默认为待审核状态
};
// 更新评论列表 - add new comment to the end
@ -1952,7 +1954,8 @@ Page({
hated: false,
replies: [],
phoneNumber: '',
isDefault: true
isDefault: true,
review: 1 // 默认评论默认为审核通过状态
}));
},
@ -2003,9 +2006,19 @@ Page({
});
commentsData = uniqueComments;
// 应用审核逻辑:审核通过的评论所有人可见,未审核通过的评论仅自己可见
const currentUserPhone = this.data.currentUserPhone;
const filteredComments = commentsData.filter(comment => {
const reviewStatus = comment.review || 0; // 默认值为0(待审核)
// 审核通过的评论(review=1)所有人可见
// 未审核通过的评论(review=0或2)仅评论作者可见
return reviewStatus === 1 || comment.phoneNumber === currentUserPhone;
});
console.log('应用审核逻辑后剩余评论数量:', filteredComments.length);
// Always add default comments at the beginning
const defaultComments = this.getConsistentRandomComments(productId, 2);
commentsData = [...defaultComments, ...commentsData];
commentsData = [...defaultComments, ...filteredComments];
// 检查返回的评论是否都属于当前用户
const allCommentsBelongToCurrentUser = commentsData.every(comment =>
@ -2026,6 +2039,8 @@ Page({
id: comment.id || `comment_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,
// 确保phoneNumber字段存在
phoneNumber: comment.phoneNumber || comment.userPhone || '',
// 确保review字段存在
review: comment.review || 0,
// 格式化时间
time: timeUtils.formatRelativeTime(comment.time)
}));

13
server-example/server-mysql.js

@ -556,6 +556,11 @@ const comments = sequelize.define('Comment', {
type: DataTypes.INTEGER,
defaultValue: 0,
comment: '点踩数'
},
review: {
type: DataTypes.INTEGER,
defaultValue: 0,
comment: '审核字段'
}
}, {
tableName: 'comments',
@ -1075,6 +1080,11 @@ Comment.init({
type: DataTypes.INTEGER,
defaultValue: 0,
comment: '点踩数'
},
review: {
type: DataTypes.INTEGER,
defaultValue: 0,
comment: '审核字段'
}
}, {
sequelize,
@ -4380,7 +4390,7 @@ app.post('/api/comments/get', async (req, res) => {
// 提交评论
app.post('/api/comments/add', async (req, res) => {
try {
const { productId, phoneNumber, comments } = req.body;
const { productId, phoneNumber, comments, review = 0 } = req.body;
if (!productId || !phoneNumber || !comments) {
return res.status(400).json({
@ -4395,6 +4405,7 @@ app.post('/api/comments/add', async (req, res) => {
productId,
phoneNumber,
comments,
review,
time: new Date()
});

Loading…
Cancel
Save