Browse Source

评论区和评论数不一致的bug

caojp 1 tháng trước cách đây
mục cha
commit
40c80dcf26

+ 40 - 7
LiangZhiYUMao/pages/plaza/detail.vue

@@ -70,7 +70,7 @@
 			<view class="comment-section">
 				<view class="section-title">
 					<text class="title-text">评论区</text>
-					<text class="title-count">{{ dynamic ? dynamic.commentCount || 0 : commentList.length }}</text>
+					<text class="title-count">{{ getCommentCount() }}</text>
 				</view>
 				
 				<!-- 评论列表 -->
@@ -211,11 +211,12 @@
 import api from '@/utils/api.js'
 
 export default {
-	data() {
+		data() {
 		return {
 			dynamicId: null,
 			dynamic: null,
 			commentList: [],
+			commentTotal: 0,  // 评论总数
 			showInput: false,
 			showMenu: false, // 显示更多菜单
 			commentText: '',
@@ -512,10 +513,41 @@ export default {
             try {
                 const res = await api.dynamic.getComments(this.dynamicId, 1, 20)
                 this.commentList = res.records || []
+                // 更新评论总数(优先使用接口返回的总数)
+                if (res.total !== undefined && res.total !== null) {
+                    this.commentTotal = res.total
+                    // 同步更新动态的评论数
+                    if (this.dynamic) {
+                        this.dynamic.commentCount = res.total
+                    }
+                } else if (this.commentList.length > 0) {
+                    // 如果没有总数,使用列表长度
+                    this.commentTotal = this.commentList.length
+                    // 如果动态的评论数为0,则更新
+                    if (this.dynamic && (!this.dynamic.commentCount || this.dynamic.commentCount === 0)) {
+                        this.dynamic.commentCount = this.commentList.length
+                    }
+                } else {
+                    this.commentTotal = 0
+                }
             } catch (e) {
                 console.error('加载评论失败', e)
             }
         },
+        
+        // 获取评论数(优先使用实际统计的评论数,确保准确性)
+        getCommentCount() {
+            // 优先使用评论总数
+            if (this.commentTotal > 0) {
+                return this.commentTotal
+            }
+            // 其次使用评论列表长度
+            if (this.commentList && this.commentList.length > 0) {
+                return this.commentList.length
+            }
+            // 最后使用动态的评论数
+            return this.dynamic ? (this.dynamic.commentCount || 0) : 0
+        },
 
         // 提交评论
         async submitComment() {
@@ -533,13 +565,14 @@ export default {
                 this.commentText = ''
                 this.commentImages = []
                 this.hideCommentInput()
+                // 更新评论数量(乐观更新)
+                if (this.dynamic) {
+                    this.dynamic.commentCount = (this.dynamic.commentCount || 0) + 1
+                }
+                this.commentTotal = (this.commentTotal || 0) + 1
+                // 重新加载评论列表
                 this.loadComments()
 				
-				// 更新评论数量
-				if (this.dynamic) {
-					this.dynamic.commentCount = (this.dynamic.commentCount || 0) + 1
-				}
-				
                 uni.showToast({ title: '已发布', icon: 'success' })
             } catch (e) {
                 uni.showToast({ title: '发布失败', icon: 'none' })

+ 24 - 0
service/dynamic/src/main/java/com/zhentao/service/impl/CommentServiceImpl.java

@@ -7,6 +7,7 @@ import com.zhentao.entity.CommentLikes;
 import com.zhentao.entity.DynamicComments;
 import com.zhentao.mapper.CommentLikesMapper;
 import com.zhentao.mapper.DynamicCommentsMapper;
+import com.zhentao.mapper.UserDynamicsMapper;
 import com.zhentao.service.CommentService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.dao.DuplicateKeyException;
@@ -27,6 +28,8 @@ public class CommentServiceImpl implements CommentService {
     private DynamicCommentsMapper commentsMapper;
     @Autowired
     private CommentLikesMapper commentLikesMapper;
+    @Autowired
+    private UserDynamicsMapper userDynamicsMapper;
 
     private static final Set<String> SENSITIVE = new HashSet<>(Arrays.asList("傻", "坏词", "违规"));
 
@@ -53,6 +56,12 @@ public class CommentServiceImpl implements CommentService {
         c.setCreatedAt(LocalDateTime.now());
         commentsMapper.insert(c);
 
+        // 更新动态的评论数
+        userDynamicsMapper.update(null,
+                new com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper<com.zhentao.entity.UserDynamics>()
+                        .eq("dynamic_id", dynamicId)
+                        .setSql("comment_count = comment_count + 1"));
+
         // 如果是回复,给父评论回复数+1
         if (c.getParentCommentId() != null && c.getParentCommentId() > 0) {
             commentsMapper.update(null,
@@ -69,6 +78,21 @@ public class CommentServiceImpl implements CommentService {
                 .eq("dynamic_id", dynamicId)
                 .eq("status", 1)
                 .orderByDesc("created_at"));
+        
+        // 同步更新动态的评论数(统计实际评论总数,确保数据一致性)
+        // 统计所有评论(包括回复)
+        Long actualCommentCount = commentsMapper.selectCount(new QueryWrapper<DynamicComments>()
+                .eq("dynamic_id", dynamicId)
+                .eq("status", 1));
+        
+        // 更新动态表的评论数(如果实际数量与数据库不一致)
+        if (actualCommentCount != null) {
+            userDynamicsMapper.update(null,
+                    new com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper<com.zhentao.entity.UserDynamics>()
+                            .eq("dynamic_id", dynamicId)
+                            .set("comment_count", actualCommentCount.intValue()));
+        }
+        
         return new PageResult<>(pg.getRecords(), pg.getTotal(), pg.getCurrent(), pg.getSize());
     }