Selaa lähdekoodia

文章评价代码优化增加是否在文章作者的关注列表中权限

fangqing 6 kuukautta sitten
vanhempi
sitoutus
cf1233d402

+ 13 - 9
ruoyi-generator/src/main/java/com/ruoyi/generator/controller/CommunityArticleController.java

@@ -11,14 +11,8 @@ import com.ruoyi.common.utils.SecurityUtils;
 import com.ruoyi.common.utils.ServletUtils;
 import com.ruoyi.generator.domain.Community.*;
 import com.ruoyi.generator.mapper.community.*;
-import com.ruoyi.generator.service.ICommunityArticleRecommendService;
-import com.ruoyi.generator.service.ICommunityArticleService;
-import com.ruoyi.generator.service.ICommunityCollectionService;
-import com.ruoyi.generator.service.ICommunityCommentReplyService;
-import com.ruoyi.generator.vo.CommunityArticleVo;
-import com.ruoyi.generator.vo.CommunityCircleVo;
-import com.ruoyi.generator.vo.CommunityCollectionArticleVo;
-import com.ruoyi.generator.vo.CommunityCollectionVo;
+import com.ruoyi.generator.service.*;
+import com.ruoyi.generator.vo.*;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import org.apache.logging.log4j.util.Strings;
@@ -73,6 +67,11 @@ public class CommunityArticleController extends BaseController {
     @Autowired
     private ICommunityCommentReplyService commentReplyService;
 
+    @Autowired
+    private ICommunityPrivacyService communityPrivacyService;
+
+    @Autowired
+    private CommunityArticleMapper communityArticleMapper;
     /**
      * 获取文章列表信息
      */
@@ -157,7 +156,12 @@ public class CommunityArticleController extends BaseController {
         if (null == communityArticleComment.getArticleId()) {
             return AjaxResult.error("文章不存在或异常!");
         }
+
+        Boolean aBoolean = communityArticleService.checkCommentPermission(communityArticleComment);
         communityArticleComment.setUserId(SecurityUtils.getUserId());
+        /*if (!aBoolean) {  // 当 aBoolean 为 false 时,进入这个条件
+            return AjaxResult.success("您没有权限评论此文章,因为只允许关注的人评论您!");
+        }*/
         //新增评论
         communityArticleService.sendComment(communityArticleComment);
         return AjaxResult.success();
@@ -651,7 +655,7 @@ public class CommunityArticleController extends BaseController {
         return AjaxResult.success(collectionArticleVos);
     }
 
-   
+
 
 
 }

+ 1 - 1
ruoyi-generator/src/main/java/com/ruoyi/generator/controller/CommunityCommentController.java

@@ -211,4 +211,4 @@ public class CommunityCommentController extends BaseController {
         communityCommentReplyService.updateById(communityCommentReply);
         return AjaxResult.success("删除回复成功!");
     }
-}
+}

+ 70 - 0
ruoyi-generator/src/main/java/com/ruoyi/generator/service/CommunityArticleServiceImpl.java

@@ -2,6 +2,7 @@ package com.ruoyi.generator.service;
 
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.ruoyi.common.core.domain.AjaxResult;
 import com.ruoyi.common.core.domain.entity.SysUser;
 import com.ruoyi.common.utils.DateUtils;
 import com.ruoyi.common.utils.SecurityUtils;
@@ -90,6 +91,11 @@ public class CommunityArticleServiceImpl extends ServiceImpl<CommunityArticleMap
     @Autowired
     private CommunityArticleRecommendMapper recommendMapper;
 
+    @Autowired
+    private ICommunityArticleService communityArticleService;
+
+    @Autowired
+    private ICommunityPrivacyService communityPrivacyService;
 
     /**
      * 查询文章列表
@@ -603,6 +609,70 @@ public class CommunityArticleServiceImpl extends ServiceImpl<CommunityArticleMap
         return communityUserLikeVos;
     }
 
+    /**
+     *
+     * @param communityArticleComment
+     * @throws ParseException
+     */
+    @Override
+    public Boolean checkCommentPermission(CommunityArticleComment communityArticleComment) throws ParseException {
+        /**
+         * 1.获取文章创建人ID
+         * 2.通过文章创建人ID 查询隐私设置表 communityPrivacyService.selectUserPrivacy
+         * 3.查询是否开启 只允许我关注的人评论我  有则进入3,没有就返回,
+         * 4.获取用户关注列表
+         * 5.是否是关注的人评论我 有则评论成功,没有则返回失败
+         */
+        //获取文章信息 找到文章创建人
+        List<CommunityArticle> communityArticles
+                = communityArticleMapper
+                .selectList(new QueryWrapper<CommunityArticle>().eq("id", communityArticleComment.getArticleId()));
+
+        // 提取用户ID
+        Long articleId = null; // 初始化用户ID
+
+        if (!communityArticles.isEmpty()) { // 检查列表是否为空
+            CommunityArticle article = communityArticles.get(0); // 获取第一个文章
+            articleId = article.getUserId(); // 假设CommunityArticle有getUserId()方法
+        } else {
+            // 处理未找到文章的情况
+            // 例如,可以抛出异常或记录日志
+            return false;
+        }
+
+        // 直接允许作者评论
+        if (articleId.equals(SecurityUtils.getLoginUser().getUserId())) {
+           /* // 如果评论者是文章作者,直接进行评论
+            communityArticleService.sendComment(communityArticleComment);*/
+            return true;
+        }
+
+        // 1. 查询隐私设置
+        List<CommunityUserPrivacyVo> communityUserPrivacyVos = communityPrivacyService.selectUserPrivacy(articleId);
+        // 2. 检查隐私设置 只允许我关注的人评论我
+        boolean IsComment = false;
+        for (CommunityUserPrivacyVo privacyVo : communityUserPrivacyVos) {
+            if (privacyVo.getIsComment() != null && privacyVo.getIsComment()) {
+                IsComment = true; // 只允许特定用户评论
+                break;
+            }
+        }
+        // 如果评论受限,则查询用户关注列表
+        if (IsComment) {
+            List<CommunityUserLikeVo> communityUserLikeVos = communityArticleService.selectCommunityUserLikeList(articleId);
+
+            // 3. 检查评论者是否在关注列表中
+            boolean isFollower = communityUserLikeVos.stream()
+                    .anyMatch(like -> like.getUserId().equals(SecurityUtils.getLoginUser().getUserId()));
+
+            // 如果不是关注者,返回失败
+            if (!isFollower) {
+                //return AjaxResult.error("您没有权限评论此文章,因为只允许关注的人评论您!");
+                return false;
+            }
+        }
+        return true;
+    }
 
 
     /**

+ 4 - 0
ruoyi-generator/src/main/java/com/ruoyi/generator/service/CommunityNotificationServiceImpl.java

@@ -13,6 +13,7 @@ import org.springframework.stereotype.Service;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Objects;
 
 /**
  * 业务 服务层实现
@@ -32,6 +33,9 @@ public class CommunityNotificationServiceImpl extends ServiceImpl<CommunityUserN
      */
     @Override
     public List<CommunityUserNotificationVo> selectUserNotification(Long userId) {
+        if (Objects.isNull(userId)) {
+            userId = SecurityUtils.getLoginUser().getUserId();
+        }
         // 查询自己的通知权限
         List<CommunityUserNotification> communityUserNotifications
                 = communityUserNotificationMapper

+ 4 - 0
ruoyi-generator/src/main/java/com/ruoyi/generator/service/CommunityPrivacyServiceImpl.java

@@ -16,6 +16,7 @@ import org.springframework.stereotype.Service;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Objects;
 
 /**
  * 业务 服务层实现
@@ -36,6 +37,9 @@ public class CommunityPrivacyServiceImpl extends ServiceImpl<CommunityUserPrivac
      */
     @Override
     public List<CommunityUserPrivacyVo> selectUserPrivacy(Long userId) {
+        if (Objects.isNull(userId)) {
+            userId = SecurityUtils.getLoginUser().getUserId();
+        }
         // 查询自己的隐私权限
         List<CommunityUserPrivacy> communityUserPrivacies
                 = communityUserPrivacyMapper

+ 7 - 0
ruoyi-generator/src/main/java/com/ruoyi/generator/service/ICommunityArticleService.java

@@ -147,5 +147,12 @@ public interface ICommunityArticleService extends IService<CommunityArticle> {
     List<CommunityUserLikeVo> selectMutualAttention(Long userId);
 
 
+    /**
+     * 判断评论的人 是否在文章作者的关注列表中
+     * @param communityArticleComment
+     * @throws ParseException
+     */
+    Boolean checkCommentPermission(CommunityArticleComment communityArticleComment) throws ParseException;
+
 
 }