评论设置敏感词拦截

This commit is contained in:
wuchunlei
2023-12-04 17:55:16 +08:00
parent 576040c686
commit 6b59e8539f
5 changed files with 71 additions and 2 deletions

View File

@@ -1,5 +1,6 @@
package com.peanut.modules.book.controller;
import com.alibaba.druid.sql.visitor.functions.Instr;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
@@ -13,6 +14,8 @@ import com.peanut.modules.book.entity.MyUserEntity;
import com.peanut.modules.book.service.BookTeachCommentService;
import com.peanut.modules.book.service.BookTeachLikeService;
import com.peanut.modules.book.service.MyUserService;
import com.peanut.modules.sys.entity.SysSensitiveWords;
import com.peanut.modules.sys.service.SysSensitiveWordsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
@@ -36,6 +39,8 @@ public class BookTeachLikeAndCommentController {
private BookTeachCommentService commentService;
@Autowired
private MyUserService userService;
@Autowired
private SysSensitiveWordsService sensitiveWordsService;
/**
* 获取点赞数量
@@ -159,9 +164,26 @@ public class BookTeachLikeAndCommentController {
*/
@RequestMapping("/addComment")
public R addComment(@RequestBody BookTeachCommentEntity comment) {
R result = new R();
comment.setUserId(ShiroUtils.getUId());
commentService.saveOrUpdate(comment);
return R.ok();
LambdaQueryWrapper<SysSensitiveWords> wrapper = new LambdaQueryWrapper<>();
wrapper.last(" where instr(\""+comment.getContent()+"\",word)>0");
List<SysSensitiveWords> list = sensitiveWordsService.list(wrapper);
if (list.size() > 0){
String str = "";
for (SysSensitiveWords words:list) {
if ("".equals(str)){
str = words.getWord();
}else {
str = str + "" + words.getWord();
}
}
result.put("tip","您的评论含有敏感词:"+str+",请重新输入。");
}else {
commentService.saveOrUpdate(comment);
result.put("tip","评论成功!");
}
return result;
}
/**