!121 新增敏感词功能

Merge pull request !121 from 芋道源码/feature/1.6.2-sensitive-word2
This commit is contained in:
芋道源码 2022-04-09 03:19:17 +00:00 committed by Gitee
commit c9d3913544
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
36 changed files with 11199 additions and 8284 deletions

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,58 @@
package cn.iocoder.yudao.framework.mybatis.core.type;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.apache.ibatis.type.TypeHandler;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
/**
* List<String> 的类型转换器实现类对应数据库的 varchar 类型
*
* @author 永不言败
* @since 2022 3/23 12:50:15
*/
@MappedJdbcTypes(JdbcType.VARCHAR)
@MappedTypes(List.class)
public class StringLiSTTypeHandler implements TypeHandler<List<String>> {
private static final String COMMA = ",";
@Override
public void setParameter(PreparedStatement ps, int i, List<String> strings, JdbcType jdbcType) throws SQLException {
// 设置占位符
ps.setString(i, CollUtil.join(strings, COMMA));
}
@Override
public List<String> getResult(ResultSet rs, String columnName) throws SQLException {
String value = rs.getString(columnName);
return getResult(value);
}
@Override
public List<String> getResult(ResultSet rs, int columnIndex) throws SQLException {
String value = rs.getString(columnIndex);
return getResult(value);
}
@Override
public List<String> getResult(CallableStatement cs, int columnIndex) throws SQLException {
String value = cs.getString(columnIndex);
return getResult(value);
}
private List<String> getResult(String value) {
if (value == null) {
return null;
}
return StrUtil.splitTrim(value, COMMA);
}
}

View File

@ -0,0 +1,30 @@
package cn.iocoder.yudao.module.system.api.sensitiveword;
import java.util.List;
/**
* 敏感词 API 接口
*
* @author 永不言败
*/
public interface SensitiveWordApi {
/**
* 获得文本所包含的不合法的敏感词数组
*
* @param text 文本
* @param tags 标签数组
* @return 不合法的敏感词数组
*/
List<String> validateText(String text, List<String> tags);
/**
* 判断文本是否包含敏感词
*
* @param text 文本
* @param tags 表述数组
* @return 是否包含
*/
boolean isTextValid(String text, List<String> tags);
}

View File

@ -119,4 +119,8 @@ public interface ErrorCodeConstants {
ErrorCode SOCIAL_USER_UNBIND_NOT_SELF = new ErrorCode(1002018001, "社交解绑失败,非当前用户绑定");
ErrorCode SOCIAL_USER_NOT_FOUND = new ErrorCode(1002018002, "社交授权失败,找不到对应的用户");
// ========== 系统铭感词 1002019000 =========
ErrorCode SENSITIVE_WORD_NOT_EXISTS = new ErrorCode(1002019000, "系统敏感词在所有标签中都不存在");
ErrorCode SENSITIVE_WORD_EXISTS = new ErrorCode(1002019001, "系统敏感词已在标签中存在");
}

View File

@ -0,0 +1,29 @@
package cn.iocoder.yudao.module.system.api.sensitiveword;
import cn.iocoder.yudao.module.system.service.sensitiveword.SensitiveWordService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
* 敏感词 API 实现类
*
* @author 永不言败
*/
@Service
public class SensitiveWordApiImpl implements SensitiveWordApi {
@Resource
private SensitiveWordService sensitiveWordService;
@Override
public List<String> validateText(String text, List<String> tags) {
return sensitiveWordService.validateText(text, tags);
}
@Override
public boolean isTextValid(String text, List<String> tags) {
return sensitiveWordService.isTextValid(text, tags);
}
}

View File

@ -0,0 +1,4 @@
### 请求 /system/sensitive-word/validate-text 接口 => 成功
GET {{baseUrl}}/system/sensitive-word/validate-text?text=XXX&tags=短信&tags=蔬菜
Authorization: Bearer {{token}}
tenant-id: {{adminTenentId}}

View File

@ -0,0 +1,104 @@
package cn.iocoder.yudao.module.system.controller.admin.sensitiveword;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.system.controller.admin.sensitiveword.vo.*;
import cn.iocoder.yudao.module.system.convert.sensitiveword.SensitiveWordConvert;
import cn.iocoder.yudao.module.system.dal.dataobject.sensitiveword.SensitiveWordDO;
import cn.iocoder.yudao.module.system.service.sensitiveword.SensitiveWordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Set;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;
@Api(tags = "管理后台 - 敏感词")
@RestController
@RequestMapping("/system/sensitive-word")
@Validated
public class SensitiveWordController {
@Resource
private SensitiveWordService sensitiveWordService;
@PostMapping("/create")
@ApiOperation("创建敏感词")
@PreAuthorize("@ss.hasPermission('system:sensitive-word:create')")
public CommonResult<Long> createSensitiveWord(@Valid @RequestBody SensitiveWordCreateReqVO createReqVO) {
return success(sensitiveWordService.createSensitiveWord(createReqVO));
}
@PutMapping("/update")
@ApiOperation("更新敏感词")
@PreAuthorize("@ss.hasPermission('system:sensitive-word:update')")
public CommonResult<Boolean> updateSensitiveWord(@Valid @RequestBody SensitiveWordUpdateReqVO updateReqVO) {
sensitiveWordService.updateSensitiveWord(updateReqVO);
return success(true);
}
@DeleteMapping("/delete")
@ApiOperation("删除敏感词")
@ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
@PreAuthorize("@ss.hasPermission('system:sensitive-word:delete')")
public CommonResult<Boolean> deleteSensitiveWord(@RequestParam("id") Long id) {
sensitiveWordService.deleteSensitiveWord(id);
return success(true);
}
@GetMapping("/get")
@ApiOperation("获得敏感词")
@ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
@PreAuthorize("@ss.hasPermission('system:sensitive-word:query')")
public CommonResult<SensitiveWordRespVO> getSensitiveWord(@RequestParam("id") Long id) {
SensitiveWordDO sensitiveWord = sensitiveWordService.getSensitiveWord(id);
return success(SensitiveWordConvert.INSTANCE.convert(sensitiveWord));
}
@GetMapping("/page")
@ApiOperation("获得敏感词分页")
@PreAuthorize("@ss.hasPermission('system:sensitive-word:query')")
public CommonResult<PageResult<SensitiveWordRespVO>> getSensitiveWordPage(@Valid SensitiveWordPageReqVO pageVO) {
PageResult<SensitiveWordDO> pageResult = sensitiveWordService.getSensitiveWordPage(pageVO);
return success(SensitiveWordConvert.INSTANCE.convertPage(pageResult));
}
@GetMapping("/export-excel")
@ApiOperation("导出敏感词 Excel")
@PreAuthorize("@ss.hasPermission('system:sensitive-word:export')")
@OperateLog(type = EXPORT)
public void exportSensitiveWordExcel(@Valid SensitiveWordExportReqVO exportReqVO,
HttpServletResponse response) throws IOException {
List<SensitiveWordDO> list = sensitiveWordService.getSensitiveWordList(exportReqVO);
// 导出 Excel
List<SensitiveWordExcelVO> datas = SensitiveWordConvert.INSTANCE.convertList02(list);
ExcelUtils.write(response, "敏感词.xls", "数据", SensitiveWordExcelVO.class, datas);
}
@GetMapping("/get-tags")
@ApiOperation("获取所有敏感词的标签数组")
@PreAuthorize("@ss.hasPermission('system:sensitive-word:query')")
public CommonResult<Set<String>> getSensitiveWordTags() throws IOException {
return success(sensitiveWordService.getSensitiveWordTags());
}
@GetMapping("/validate-text")
@ApiOperation("获得文本所包含的不合法的敏感词数组")
public CommonResult<List<String>> validateText(@RequestParam("text") String text,
@RequestParam(value = "tags", required = false) List<String> tags) {
return success(sensitiveWordService.validateText(text, tags));
}
}

View File

@ -0,0 +1,31 @@
package cn.iocoder.yudao.module.system.controller.admin.sensitiveword.vo;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotNull;
import java.util.List;
/**
* 敏感词 Base VO提供给添加修改详细的子 VO 使用
* 如果子 VO 存在差异的字段请不要添加到这里影响 Swagger 文档生成
*/
@Data
public class SensitiveWordBaseVO {
@ApiModelProperty(value = "敏感词", required = true, example = "敏感词")
@NotNull(message = "敏感词不能为空")
private String name;
@ApiModelProperty(value = "标签", required = true, example = "短信,评论")
@NotNull(message = "标签不能为空")
private List<String> tags;
@ApiModelProperty(value = "状态", required = true, example = "1", notes = "参见 CommonStatusEnum 枚举类")
@NotNull(message = "状态不能为空")
private Integer status;
@ApiModelProperty(value = "描述", example = "污言秽语")
private String description;
}

View File

@ -0,0 +1,14 @@
package cn.iocoder.yudao.module.system.controller.admin.sensitiveword.vo;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
@ApiModel("管理后台 - 敏感词创建 Request VO")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class SensitiveWordCreateReqVO extends SensitiveWordBaseVO {
}

View File

@ -0,0 +1,35 @@
package cn.iocoder.yudao.module.system.controller.admin.sensitiveword.vo;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;
import java.util.Date;
import java.util.List;
/**
* 敏感词 Excel VO
*
* @author 永不言败
*/
@Data
public class SensitiveWordExcelVO {
@ExcelProperty("编号")
private Long id;
@ExcelProperty("敏感词")
private String name;
@ExcelProperty("标签")
private List<String> tags;
@ExcelProperty("状态true-启用false-禁用")
private Integer status;
@ExcelProperty("描述")
private String description;
@ExcelProperty("创建时间")
private Date createTime;
}

View File

@ -0,0 +1,33 @@
package cn.iocoder.yudao.module.system.controller.admin.sensitiveword.vo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
@ApiModel(value = "管理后台 - 敏感词 Excel 导出 Request VO", description = "参数和 SensitiveWordPageReqVO 是一致的")
@Data
public class SensitiveWordExportReqVO {
@ApiModelProperty(value = "敏感词", example = "敏感词")
private String name;
@ApiModelProperty(value = "标签", example = "短信,评论")
private String tag;
@ApiModelProperty(value = "状态", example = "true-启用false-禁用")
private Integer status;
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
@ApiModelProperty(value = "开始创建时间")
private Date beginCreateTime;
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
@ApiModelProperty(value = "结束创建时间")
private Date endCreateTime;
}

View File

@ -0,0 +1,38 @@
package cn.iocoder.yudao.module.system.controller.admin.sensitiveword.vo;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
@ApiModel("管理后台 - 敏感词分页 Request VO")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class SensitiveWordPageReqVO extends PageParam {
@ApiModelProperty(value = "敏感词", example = "敏感词")
private String name;
@ApiModelProperty(value = "标签", example = "短信,评论")
private String tag;
@ApiModelProperty(value = "状态", example = "true-启用true-禁用")
private Integer status;
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
@ApiModelProperty(value = "开始创建时间")
private Date beginCreateTime;
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
@ApiModelProperty(value = "结束创建时间")
private Date endCreateTime;
}

View File

@ -0,0 +1,23 @@
package cn.iocoder.yudao.module.system.controller.admin.sensitiveword.vo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import java.util.Date;
@ApiModel("管理后台 - 敏感词 Response VO")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class SensitiveWordRespVO extends SensitiveWordBaseVO {
@ApiModelProperty(value = "编号", required = true, example = "1")
private Long id;
@ApiModelProperty(value = "创建时间", required = true)
private Date createTime;
}

View File

@ -0,0 +1,21 @@
package cn.iocoder.yudao.module.system.controller.admin.sensitiveword.vo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import javax.validation.constraints.NotNull;
@ApiModel("管理后台 - 敏感词更新 Request VO")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class SensitiveWordUpdateReqVO extends SensitiveWordBaseVO {
@ApiModelProperty(value = "编号", required = true, example = "1")
@NotNull(message = "编号不能为空")
private Long id;
}

View File

@ -0,0 +1,36 @@
package cn.iocoder.yudao.module.system.convert.sensitiveword;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.system.controller.admin.sensitiveword.vo.SensitiveWordCreateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.sensitiveword.vo.SensitiveWordExcelVO;
import cn.iocoder.yudao.module.system.controller.admin.sensitiveword.vo.SensitiveWordRespVO;
import cn.iocoder.yudao.module.system.controller.admin.sensitiveword.vo.SensitiveWordUpdateReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.sensitiveword.SensitiveWordDO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import java.util.List;
/**
* 敏感词 Convert
*
* @author 永不言败
*/
@Mapper
public interface SensitiveWordConvert {
SensitiveWordConvert INSTANCE = Mappers.getMapper(SensitiveWordConvert.class);
SensitiveWordDO convert(SensitiveWordCreateReqVO bean);
SensitiveWordDO convert(SensitiveWordUpdateReqVO bean);
SensitiveWordRespVO convert(SensitiveWordDO bean);
List<SensitiveWordRespVO> convertList(List<SensitiveWordDO> list);
PageResult<SensitiveWordRespVO> convertPage(PageResult<SensitiveWordDO> page);
List<SensitiveWordExcelVO> convertList02(List<SensitiveWordDO> list);
}

View File

@ -0,0 +1,56 @@
package cn.iocoder.yudao.module.system.dal.dataobject.sensitiveword;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.yudao.framework.mybatis.core.type.StringLiSTTypeHandler;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.*;
import java.util.List;
/**
* 敏感词 DO
*
* @author 永不言败
*/
@TableName(value = "system_sensitive_word", autoResultMap = true)
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class SensitiveWordDO extends BaseDO {
/**
* 编号
*/
@TableId
private Long id;
/**
* 敏感词
*/
private String name;
/**
* 描述
*/
private String description;
/**
* 标签数组
*
* 用于实现不同的业务场景下需要使用不同标签的敏感词
* 例如说tag 有短信论坛两种敏感词 "推广" 在短信下是敏感词在论坛下不是敏感词
* 此时我们会存储一条敏感词记录它的 name "推广"tag 为短信
*/
@TableField(typeHandler = StringLiSTTypeHandler.class)
private List<String> tags;
/**
* 状态
*
* 枚举 {@link CommonStatusEnum}
*/
private Integer status;
}

View File

@ -0,0 +1,47 @@
package cn.iocoder.yudao.module.system.dal.mysql.sensitiveword;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.controller.admin.sensitiveword.vo.SensitiveWordExportReqVO;
import cn.iocoder.yudao.module.system.controller.admin.sensitiveword.vo.SensitiveWordPageReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.sensitiveword.SensitiveWordDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.Date;
import java.util.List;
/**
* 敏感词 Mapper
*
* @author 永不言败
*/
@Mapper
public interface SensitiveWordMapper extends BaseMapperX<SensitiveWordDO> {
default PageResult<SensitiveWordDO> selectPage(SensitiveWordPageReqVO reqVO) {
return selectPage(reqVO, new LambdaQueryWrapperX<SensitiveWordDO>()
.likeIfPresent(SensitiveWordDO::getName, reqVO.getName())
.likeIfPresent(SensitiveWordDO::getTags, reqVO.getTag())
.eqIfPresent(SensitiveWordDO::getStatus, reqVO.getStatus())
.betweenIfPresent(SensitiveWordDO::getCreateTime, reqVO.getBeginCreateTime(), reqVO.getEndCreateTime())
.orderByDesc(SensitiveWordDO::getId));
}
default List<SensitiveWordDO> selectList(SensitiveWordExportReqVO reqVO) {
return selectList(new LambdaQueryWrapperX<SensitiveWordDO>()
.likeIfPresent(SensitiveWordDO::getName, reqVO.getName())
.likeIfPresent(SensitiveWordDO::getTags, reqVO.getTag())
.eqIfPresent(SensitiveWordDO::getStatus, reqVO.getStatus())
.betweenIfPresent(SensitiveWordDO::getCreateTime, reqVO.getBeginCreateTime(), reqVO.getEndCreateTime())
.orderByDesc(SensitiveWordDO::getId));
}
default SensitiveWordDO selectByName(String name) {
return selectOne(SensitiveWordDO::getName, name);
}
@Select("SELECT id FROM system_sensitive_word WHERE update_time > #{maxUpdateTime} LIMIT 1")
SensitiveWordDO selectExistsByUpdateTimeAfter(Date maxUpdateTime);
}

View File

@ -0,0 +1,29 @@
package cn.iocoder.yudao.module.system.mq.consumer.sensitiveword;
import cn.iocoder.yudao.framework.mq.core.pubsub.AbstractChannelMessageListener;
import cn.iocoder.yudao.module.system.mq.message.sensitiveword.SensitiveWordRefreshMessage;
import cn.iocoder.yudao.module.system.service.sensitiveword.SensitiveWordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/**
* 针对 {@link SensitiveWordRefreshMessage} 的消费者
*
* @author 芋道源码
*/
@Component
@Slf4j
public class SensitiveWordRefreshConsumer extends AbstractChannelMessageListener<SensitiveWordRefreshMessage> {
@Resource
private SensitiveWordService sensitiveWordService;
@Override
public void onMessage(SensitiveWordRefreshMessage message) {
log.info("[onMessage][收到 SensitiveWord 刷新消息]");
sensitiveWordService.initLocalCache();
}
}

View File

@ -0,0 +1,19 @@
package cn.iocoder.yudao.module.system.mq.message.sensitiveword;
import cn.iocoder.yudao.framework.mq.core.pubsub.AbstractChannelMessage;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* 敏感词的刷新 Message
*/
@Data
@EqualsAndHashCode(callSuper = true)
public class SensitiveWordRefreshMessage extends AbstractChannelMessage {
@Override
public String getChannel() {
return "system.sensitive-word.refresh";
}
}

View File

@ -0,0 +1,26 @@
package cn.iocoder.yudao.module.system.mq.producer.sensitiveword;
import cn.iocoder.yudao.framework.mq.core.RedisMQTemplate;
import cn.iocoder.yudao.module.system.mq.message.sensitiveword.SensitiveWordRefreshMessage;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/**
* 敏感词相关的 Producer
*/
@Component
public class SensitiveWordProducer {
@Resource
private RedisMQTemplate redisMQTemplate;
/**
* 发送 {@link SensitiveWordRefreshMessage} 消息
*/
public void sendSensitiveWordRefreshMessage() {
SensitiveWordRefreshMessage message = new SensitiveWordRefreshMessage();
redisMQTemplate.send(message);
}
}

View File

@ -0,0 +1,104 @@
package cn.iocoder.yudao.module.system.service.sensitiveword;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.system.controller.admin.sensitiveword.vo.SensitiveWordCreateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.sensitiveword.vo.SensitiveWordExportReqVO;
import cn.iocoder.yudao.module.system.controller.admin.sensitiveword.vo.SensitiveWordPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.sensitiveword.vo.SensitiveWordUpdateReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.sensitiveword.SensitiveWordDO;
import javax.validation.Valid;
import java.util.List;
import java.util.Set;
/**
* 敏感词 Service 接口
*
* @author 永不言败
*/
public interface SensitiveWordService {
/**
* 初始化本地缓存
*/
void initLocalCache();
/**
* 创建敏感词
*
* @param createReqVO 创建信息
* @return 编号
*/
Long createSensitiveWord(@Valid SensitiveWordCreateReqVO createReqVO);
/**
* 更新敏感词
*
* @param updateReqVO 更新信息
*/
void updateSensitiveWord(@Valid SensitiveWordUpdateReqVO updateReqVO);
/**
* 删除敏感词
*
* @param id 编号
*/
void deleteSensitiveWord(Long id);
/**
* 获得敏感词
*
* @param id 编号
* @return 敏感词
*/
SensitiveWordDO getSensitiveWord(Long id);
/**
* 获得敏感词列表
*
* @return 敏感词列表
*/
List<SensitiveWordDO> getSensitiveWordList();
/**
* 获得敏感词分页
*
* @param pageReqVO 分页查询
* @return 敏感词分页
*/
PageResult<SensitiveWordDO> getSensitiveWordPage(SensitiveWordPageReqVO pageReqVO);
/**
* 获得敏感词列表, 用于 Excel 导出
*
* @param exportReqVO 查询条件
* @return 敏感词列表
*/
List<SensitiveWordDO> getSensitiveWordList(SensitiveWordExportReqVO exportReqVO);
/**
* 获得所有敏感词的标签数组
*
* @return 标签数组
*/
Set<String> getSensitiveWordTags();
/**
* 获得文本所包含的不合法的敏感词数组
*
* @param text 文本
* @param tags 标签数组
* @return 不合法的敏感词数组
*/
List<String> validateText(String text, List<String> tags);
/**
* 判断文本是否包含敏感词
*
* @param text 文本
* @param tags 表述数组
* @return 是否包含
*/
boolean isTextValid(String text, List<String> tags);
}

View File

@ -0,0 +1,265 @@
package cn.iocoder.yudao.module.system.service.sensitiveword;
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.system.controller.admin.sensitiveword.vo.SensitiveWordCreateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.sensitiveword.vo.SensitiveWordExportReqVO;
import cn.iocoder.yudao.module.system.controller.admin.sensitiveword.vo.SensitiveWordPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.sensitiveword.vo.SensitiveWordUpdateReqVO;
import cn.iocoder.yudao.module.system.convert.sensitiveword.SensitiveWordConvert;
import cn.iocoder.yudao.module.system.dal.dataobject.sensitiveword.SensitiveWordDO;
import cn.iocoder.yudao.module.system.dal.mysql.sensitiveword.SensitiveWordMapper;
import cn.iocoder.yudao.module.system.mq.producer.sensitiveword.SensitiveWordProducer;
import cn.iocoder.yudao.module.system.util.collection.SimpleTrie;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.*;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.SENSITIVE_WORD_EXISTS;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.SENSITIVE_WORD_NOT_EXISTS;
/**
* 敏感词 Service 实现类
*
* @author 永不言败
*/
@Service
@Slf4j
@Validated
public class SensitiveWordServiceImpl implements SensitiveWordService {
/**
* 定时执行 {@link #schedulePeriodicRefresh()} 的周期
* 因为已经通过 Redis Pub/Sub 机制所以频率不需要高
*/
private static final long SCHEDULER_PERIOD = 5 * 60 * 1000L;
/**
* 敏感词标签缓存
* key敏感词编号 {@link SensitiveWordDO#getId()}
* <p>
* 这里声明 volatile 修饰的原因是每次刷新时直接修改指向
*/
@Getter
private volatile Set<String> sensitiveWordTagsCache = Collections.emptySet();
/**
* 缓存敏感词的最大更新时间用于后续的增量轮询判断是否有更新
*/
@Getter
private volatile Date maxUpdateTime;
@Resource
private SensitiveWordMapper sensitiveWordMapper;
@Resource
private SensitiveWordProducer sensitiveWordProducer;
/**
* 默认的敏感词的字典树包含所有敏感词
*/
@Getter
private volatile SimpleTrie defaultSensitiveWordTrie = new SimpleTrie(Collections.emptySet());
/**
* 标签与敏感词的字段数的映射
*/
@Getter
private volatile Map<String, SimpleTrie> tagSensitiveWordTries = Collections.emptyMap();
/**
* 初始化缓存
*/
@Override
@PostConstruct
public void initLocalCache() {
// 获取敏感词列表如果有更新
List<SensitiveWordDO> sensitiveWordList = loadSensitiveWordIfUpdate(maxUpdateTime);
if (CollUtil.isEmpty(sensitiveWordList)) {
return;
}
// 写入 sensitiveWordTagsCache 缓存
Set<String> tags = new HashSet<>();
sensitiveWordList.forEach(word -> tags.addAll(word.getTags()));
sensitiveWordTagsCache = tags;
// 写入 defaultSensitiveWordTrietagSensitiveWordTries 缓存
initSensitiveWordTrie(sensitiveWordList);
// 写入 maxUpdateTime 最大更新时间
maxUpdateTime = CollectionUtils.getMaxValue(sensitiveWordList, SensitiveWordDO::getUpdateTime);
log.info("[initLocalCache][初始化 敏感词 数量为 {}]", sensitiveWordList.size());
}
private void initSensitiveWordTrie(List<SensitiveWordDO> wordDOs) {
// 过滤禁用的敏感词
wordDOs = CollectionUtils.filterList(wordDOs, word -> word.getStatus().equals(CommonStatusEnum.ENABLE.getStatus()));
// 初始化默认的 defaultSensitiveWordTrie
this.defaultSensitiveWordTrie = new SimpleTrie(CollectionUtils.convertList(wordDOs, SensitiveWordDO::getName));
// 初始化 tagSensitiveWordTries
Multimap<String, String> tagWords = HashMultimap.create();
for (SensitiveWordDO word : wordDOs) {
if (CollUtil.isEmpty(word.getTags())) {
continue;
}
word.getTags().forEach(tag -> tagWords.put(tag, word.getName()));
}
// 添加到 tagSensitiveWordTries
Map<String, SimpleTrie> tagSensitiveWordTries = new HashMap<>();
tagWords.asMap().forEach((tag, words) -> tagSensitiveWordTries.put(tag, new SimpleTrie(words)));
this.tagSensitiveWordTries = tagSensitiveWordTries;
}
@Scheduled(fixedDelay = SCHEDULER_PERIOD, initialDelay = SCHEDULER_PERIOD)
public void schedulePeriodicRefresh() {
initLocalCache();
}
/**
* 如果敏感词发生变化从数据库中获取最新的全量敏感词
* 如果未发生变化则返回空
*
* @param maxUpdateTime 当前敏感词的最大更新时间
* @return 敏感词列表
*/
private List<SensitiveWordDO> loadSensitiveWordIfUpdate(Date maxUpdateTime) {
// 第一步判断是否要更新
// 如果更新时间为空说明 DB 一定有新数据
if (maxUpdateTime == null) {
log.info("[loadSensitiveWordIfUpdate][首次加载全量敏感词]");
} else { // 判断数据库中是否有更新的敏感词
if (sensitiveWordMapper.selectExistsByUpdateTimeAfter(maxUpdateTime) == null) {
return null;
}
log.info("[loadSensitiveWordIfUpdate][增量加载全量敏感词]");
}
// 第二步如果有更新则从数据库加载所有敏感词
return sensitiveWordMapper.selectList();
}
@Override
public Long createSensitiveWord(SensitiveWordCreateReqVO createReqVO) {
// 校验唯一性
checkSensitiveWordNameUnique(null, createReqVO.getName());
// 插入
SensitiveWordDO sensitiveWord = SensitiveWordConvert.INSTANCE.convert(createReqVO);
sensitiveWordMapper.insert(sensitiveWord);
// 发送消息刷新缓存
sensitiveWordProducer.sendSensitiveWordRefreshMessage();
return sensitiveWord.getId();
}
@Override
public void updateSensitiveWord(SensitiveWordUpdateReqVO updateReqVO) {
// 校验唯一性
checkSensitiveWordExists(updateReqVO.getId());
checkSensitiveWordNameUnique(updateReqVO.getId(), updateReqVO.getName());
// 更新
SensitiveWordDO updateObj = SensitiveWordConvert.INSTANCE.convert(updateReqVO);
sensitiveWordMapper.updateById(updateObj);
// 发送消息刷新缓存
sensitiveWordProducer.sendSensitiveWordRefreshMessage();
}
@Override
public void deleteSensitiveWord(Long id) {
// 校验存在
checkSensitiveWordExists(id);
// 删除
sensitiveWordMapper.deleteById(id);
// 发送消息刷新缓存
sensitiveWordProducer.sendSensitiveWordRefreshMessage();
}
private void checkSensitiveWordNameUnique(Long id, String name) {
SensitiveWordDO word = sensitiveWordMapper.selectByName(name);
if (word == null) {
return;
}
// 如果 id 为空说明不用比较是否为相同 id 的敏感词
if (id == null) {
throw exception(SENSITIVE_WORD_EXISTS);
}
if (!word.getId().equals(id)) {
throw exception(SENSITIVE_WORD_EXISTS);
}
}
private void checkSensitiveWordExists(Long id) {
if (sensitiveWordMapper.selectById(id) == null) {
throw exception(SENSITIVE_WORD_NOT_EXISTS);
}
}
@Override
public SensitiveWordDO getSensitiveWord(Long id) {
return sensitiveWordMapper.selectById(id);
}
@Override
public List<SensitiveWordDO> getSensitiveWordList() {
return sensitiveWordMapper.selectList();
}
@Override
public PageResult<SensitiveWordDO> getSensitiveWordPage(SensitiveWordPageReqVO pageReqVO) {
return sensitiveWordMapper.selectPage(pageReqVO);
}
@Override
public List<SensitiveWordDO> getSensitiveWordList(SensitiveWordExportReqVO exportReqVO) {
return sensitiveWordMapper.selectList(exportReqVO);
}
@Override
public Set<String> getSensitiveWordTags() {
return sensitiveWordTagsCache;
}
@Override
public List<String> validateText(String text, List<String> tags) {
if (CollUtil.isEmpty(tags)) {
return defaultSensitiveWordTrie.validate(text);
}
// 有标签的情况
Set<String> result = new HashSet<>();
tags.forEach(tag -> {
SimpleTrie trie = tagSensitiveWordTries.get(tag);
if (trie == null) {
return;
}
result.addAll(trie.validate(text));
});
return new ArrayList<>(result);
}
@Override
public boolean isTextValid(String text, List<String> tags) {
if (CollUtil.isEmpty(tags)) {
return defaultSensitiveWordTrie.isValid(text);
}
// 有标签的情况
for (String tag : tags) {
SimpleTrie trie = tagSensitiveWordTries.get(tag);
if (trie == null) {
continue;
}
if (!trie.isValid(text)) {
return false;
}
}
return true;
}
}

View File

@ -0,0 +1,145 @@
package cn.iocoder.yudao.module.system.util.collection;
import cn.hutool.core.collection.CollUtil;
import java.util.*;
/**
* 基于前缀树实现敏感词的校验
* <p>
* 相比 Apache Common 提供的 PatriciaTrie 来说性能可能会更加好一些
*
* @author 芋道源码
*/
@SuppressWarnings("unchecked")
public class SimpleTrie {
/**
* 一个敏感词结束后对应的 key
*/
private static final Character CHARACTER_END = '\0';
/**
* 使用敏感词构建的前缀树
*/
private final Map<Character, Object> children;
/**
* 基于字符串构建前缀树
*
* @param strs 字符串数组
*/
public SimpleTrie(Collection<String> strs) {
children = new HashMap<>();
// 构建树
CollUtil.sort(strs, String::compareTo); // 排序优先使用较短的前缀
for (String str : strs) {
Map<Character, Object> child = children;
// 遍历每个字符
for (Character c : str.toCharArray()) {
// 如果已经到达结束就没必要在添加更长的敏感词
// 例如说有两个敏感词是吃饭啊吃饭输入一句话是 我要吃饭啊则只要匹配到 吃饭 这个敏感词即可
if (child.containsKey(CHARACTER_END)) {
break;
}
if (!child.containsKey(c)) {
child.put(c, new HashMap<>());
}
child = (Map<Character, Object>) child.get(c);
}
// 结束
child.put(CHARACTER_END, null);
}
}
/**
* 验证文本是否合法即不包含敏感词
*
* @param text 文本
* @return 是否 ok
*/
public boolean isValid(String text) {
// 遍历 text使用每一个 [i, n) 段的字符串使用 children 前缀树匹配是否包含敏感词
for (int i = 0; i < text.length() - 1; i++) {
Map<Character, Object> child = (Map<Character, Object>) children.get(text.charAt(i));
if (child == null) {
continue;
}
boolean ok = recursion(text, i + 1, child);
if (!ok) {
return false;
}
}
return true;
}
/**
* 验证文本从指定位置开始是否包含某个敏感词
*
* @param text 文本
* @param index 开始位置
* @param child 节点当前遍历到的
* @return 是否包含
*/
private boolean recursion(String text, int index, Map<Character, Object> child) {
if (index == text.length()) {
return true;
}
child = (Map<Character, Object>) child.get(text.charAt(index));
return child == null || !child.containsKey(CHARACTER_END) && recursion(text, ++index, child);
}
/**
* 获得文本所包含的不合法的敏感词
*
* 注意才当即最短匹配原则例如说当敏感词存在 煞笔煞笔二货 只会返回 煞笔
*
* @param text 文本
* @return 匹配的敏感词
*/
public List<String> validate(String text) {
Set<String> results = new HashSet<>();
for (int i = 0; i < text.length() - 1; i++) {
Character c = text.charAt(i);
Map<Character, Object> child = (Map<Character, Object>) children.get(c);
if (child == null) {
continue;
}
StringBuilder result = new StringBuilder().append(c);
boolean ok = recursionWithResult(text, i + 1, child, result);
if (!ok) {
results.add(result.toString());
}
}
return new ArrayList<>(results);
}
/**
* 返回文本从 index 开始的敏感词并使用 StringBuilder 参数进行返回
*
* 逻辑和 {@link #recursion(String, int, Map)} 是一致只是多了 result 返回结果
*
* @param text 文本
* @param index 开始未知
* @param child 节点当前遍历到的
* @param result 返回敏感词
* @return 是否有敏感词
*/
@SuppressWarnings("unchecked")
private static boolean recursionWithResult(String text, int index, Map<Character, Object> child, StringBuilder result) {
if (index == text.length()) {
return true;
}
Character c = text.charAt(index);
child = (Map<Character, Object>) child.get(c);
if (child == null) {
return true;
}
if (child.containsKey(CHARACTER_END)) {
result.append(c);
return false;
}
return recursionWithResult(text, ++index, child, result.append(c));
}
}

View File

@ -0,0 +1,4 @@
/**
* 每个模块的 util 放专属当前模块的 Utils 工具类
*/
package cn.iocoder.yudao.module.system.util;

View File

@ -0,0 +1,246 @@
package cn.iocoder.yudao.module.system.service.sensitiveword;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.SetUtils;
import cn.iocoder.yudao.framework.common.util.date.DateUtils;
import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest;
import cn.iocoder.yudao.module.system.controller.admin.sensitiveword.vo.SensitiveWordCreateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.sensitiveword.vo.SensitiveWordExportReqVO;
import cn.iocoder.yudao.module.system.controller.admin.sensitiveword.vo.SensitiveWordPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.sensitiveword.vo.SensitiveWordUpdateReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.sensitiveword.SensitiveWordDO;
import cn.iocoder.yudao.module.system.dal.mysql.sensitiveword.SensitiveWordMapper;
import cn.iocoder.yudao.module.system.mq.producer.sensitiveword.SensitiveWordProducer;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.max;
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.SENSITIVE_WORD_NOT_EXISTS;
import static java.util.Collections.singletonList;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.verify;
/**
* {@link SensitiveWordServiceImpl} 的单元测试类
*
* @author 永不言败
*/
@Import(SensitiveWordServiceImpl.class)
public class SensitiveWordServiceImplTest extends BaseDbUnitTest {
@Resource
private SensitiveWordServiceImpl sensitiveWordService;
@Resource
private SensitiveWordMapper sensitiveWordMapper;
@MockBean
private SensitiveWordProducer sensitiveWordProducer;
@Test
public void testInitLocalCache() {
SensitiveWordDO wordDO1 = randomPojo(SensitiveWordDO.class, o -> o.setName("傻瓜")
.setTags(singletonList("论坛")).setStatus(CommonStatusEnum.ENABLE.getStatus()));
sensitiveWordMapper.insert(wordDO1);
SensitiveWordDO wordDO2 = randomPojo(SensitiveWordDO.class, o -> o.setName("笨蛋")
.setTags(singletonList("蔬菜")).setStatus(CommonStatusEnum.ENABLE.getStatus()));
sensitiveWordMapper.insert(wordDO2);
// 调用
sensitiveWordService.initLocalCache();
// 断言 maxUpdateTime 缓存
assertEquals(max(wordDO1.getUpdateTime(), wordDO2.getUpdateTime()), sensitiveWordService.getMaxUpdateTime());
// 断言 sensitiveWordTagsCache 缓存
assertEquals(SetUtils.asSet("论坛", "蔬菜"), sensitiveWordService.getSensitiveWordTags());
// 断言 tagSensitiveWordTries 缓存
assertNotNull(sensitiveWordService.getDefaultSensitiveWordTrie());
assertEquals(2, sensitiveWordService.getTagSensitiveWordTries().size());
assertNotNull(sensitiveWordService.getTagSensitiveWordTries().get("论坛"));
assertNotNull(sensitiveWordService.getTagSensitiveWordTries().get("蔬菜"));
}
@Test
public void testCreateSensitiveWord_success() {
// 准备参数
SensitiveWordCreateReqVO reqVO = randomPojo(SensitiveWordCreateReqVO.class);
// 调用
Long sensitiveWordId = sensitiveWordService.createSensitiveWord(reqVO);
// 断言
assertNotNull(sensitiveWordId);
// 校验记录的属性是否正确
SensitiveWordDO sensitiveWord = sensitiveWordMapper.selectById(sensitiveWordId);
assertPojoEquals(reqVO, sensitiveWord);
verify(sensitiveWordProducer).sendSensitiveWordRefreshMessage();
}
@Test
public void testUpdateSensitiveWord_success() {
// mock 数据
SensitiveWordDO dbSensitiveWord = randomPojo(SensitiveWordDO.class);
sensitiveWordMapper.insert(dbSensitiveWord);// @Sql: 先插入出一条存在的数据
// 准备参数
SensitiveWordUpdateReqVO reqVO = randomPojo(SensitiveWordUpdateReqVO.class, o -> {
o.setId(dbSensitiveWord.getId()); // 设置更新的 ID
});
// 调用
sensitiveWordService.updateSensitiveWord(reqVO);
// 校验是否更新正确
SensitiveWordDO sensitiveWord = sensitiveWordMapper.selectById(reqVO.getId()); // 获取最新的
assertPojoEquals(reqVO, sensitiveWord);
verify(sensitiveWordProducer).sendSensitiveWordRefreshMessage();
}
@Test
public void testUpdateSensitiveWord_notExists() {
// 准备参数
SensitiveWordUpdateReqVO reqVO = randomPojo(SensitiveWordUpdateReqVO.class);
// 调用, 并断言异常
assertServiceException(() -> sensitiveWordService.updateSensitiveWord(reqVO), SENSITIVE_WORD_NOT_EXISTS);
}
@Test
public void testDeleteSensitiveWord_success() {
// mock 数据
SensitiveWordDO dbSensitiveWord = randomPojo(SensitiveWordDO.class);
sensitiveWordMapper.insert(dbSensitiveWord);// @Sql: 先插入出一条存在的数据
// 准备参数
Long id = dbSensitiveWord.getId();
// 调用
sensitiveWordService.deleteSensitiveWord(id);
// 校验数据不存在了
assertNull(sensitiveWordMapper.selectById(id));
verify(sensitiveWordProducer).sendSensitiveWordRefreshMessage();
}
@Test
public void testDeleteSensitiveWord_notExists() {
// 准备参数
Long id = randomLongId();
// 调用, 并断言异常
assertServiceException(() -> sensitiveWordService.deleteSensitiveWord(id), SENSITIVE_WORD_NOT_EXISTS);
}
@Test
public void testGetSensitiveWordPage() {
// mock 数据
SensitiveWordDO dbSensitiveWord = randomPojo(SensitiveWordDO.class, o -> { // 等会查询到
o.setName("笨蛋");
o.setTags(Arrays.asList("论坛", "蔬菜"));
o.setStatus(CommonStatusEnum.ENABLE.getStatus());
o.setCreateTime(DateUtils.buildTime(2022, 2, 8));
});
sensitiveWordMapper.insert(dbSensitiveWord);
// 测试 name 不匹配
sensitiveWordMapper.insert(cloneIgnoreId(dbSensitiveWord, o -> o.setName("傻瓜")));
// 测试 tags 不匹配
sensitiveWordMapper.insert(cloneIgnoreId(dbSensitiveWord, o -> o.setTags(Arrays.asList("短信", "日用品"))));
// 测试 createTime 不匹配
sensitiveWordMapper.insert(cloneIgnoreId(dbSensitiveWord, o -> o.setCreateTime(DateUtils.buildTime(2022, 2, 16))));
// 准备参数
SensitiveWordPageReqVO reqVO = new SensitiveWordPageReqVO();
reqVO.setName("");
reqVO.setTag("论坛");
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
reqVO.setBeginCreateTime(DateUtils.buildTime(2022, 2, 1));
reqVO.setEndCreateTime(DateUtils.buildTime(2022, 2, 12));
// 调用
PageResult<SensitiveWordDO> pageResult = sensitiveWordService.getSensitiveWordPage(reqVO);
// 断言
assertEquals(1, pageResult.getTotal());
assertEquals(1, pageResult.getList().size());
assertPojoEquals(dbSensitiveWord, pageResult.getList().get(0));
}
@Test
public void testGetSensitiveWordList() {
// mock 数据
SensitiveWordDO dbSensitiveWord = randomPojo(SensitiveWordDO.class, o -> { // 等会查询到
o.setName("笨蛋");
o.setTags(Arrays.asList("论坛", "蔬菜"));
o.setStatus(CommonStatusEnum.ENABLE.getStatus());
o.setCreateTime(DateUtils.buildTime(2022, 2, 8));
});
sensitiveWordMapper.insert(dbSensitiveWord);
// 测试 name 不匹配
sensitiveWordMapper.insert(cloneIgnoreId(dbSensitiveWord, o -> o.setName("傻瓜")));
// 测试 tags 不匹配
sensitiveWordMapper.insert(cloneIgnoreId(dbSensitiveWord, o -> o.setTags(Arrays.asList("短信", "日用品"))));
// 测试 createTime 不匹配
sensitiveWordMapper.insert(cloneIgnoreId(dbSensitiveWord, o -> o.setCreateTime(DateUtils.buildTime(2022, 2, 16))));
// 准备参数
SensitiveWordExportReqVO reqVO = new SensitiveWordExportReqVO();
reqVO.setName("");
reqVO.setTag("论坛");
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
reqVO.setBeginCreateTime(DateUtils.buildTime(2022, 2, 1));
reqVO.setEndCreateTime(DateUtils.buildTime(2022, 2, 12));
// 调用
List<SensitiveWordDO> list = sensitiveWordService.getSensitiveWordList(reqVO);
// 断言
assertEquals(1, list.size());
assertPojoEquals(dbSensitiveWord, list.get(0));
}
@Test
public void testValidateText_noTag() {
testInitLocalCache();
// 准备参数
String text = "你是傻瓜,你是笨蛋";
// 调用
List<String> result = sensitiveWordService.validateText(text, null);
// 断言
assertEquals(Arrays.asList("傻瓜", "笨蛋"), result);
}
@Test
public void testValidateText_hasTag() {
testInitLocalCache();
// 准备参数
String text = "你是傻瓜,你是笨蛋";
// 调用
List<String> result = sensitiveWordService.validateText(text, singletonList("论坛"));
// 断言
assertEquals(singletonList("傻瓜"), result);
}
@Test
public void testIsTestValid_noTag() {
testInitLocalCache();
// 准备参数
String text = "你是傻瓜,你是笨蛋";
// 调用断言
assertFalse(sensitiveWordService.isTextValid(text, null));
}
@Test
public void testIsTestValid_hasTag() {
testInitLocalCache();
// 准备参数
String text = "你是傻瓜,你是笨蛋";
// 调用断言
assertFalse(sensitiveWordService.isTextValid(text, singletonList("论坛")));
}
}

View File

@ -17,3 +17,4 @@ DELETE FROM "system_error_code";
DELETE FROM "system_social_user";
DELETE FROM "system_tenant";
DELETE FROM "system_tenant_package";
DELETE FROM "system_sensitive_word";

View File

@ -426,3 +426,17 @@ CREATE TABLE IF NOT EXISTS "system_tenant_package" (
"deleted" bit NOT NULL DEFAULT FALSE,
PRIMARY KEY ("id")
) COMMENT '租户套餐表';
CREATE TABLE IF NOT EXISTS "system_sensitive_word" (
"id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
"name" varchar(255) NOT NULL,
"tags" varchar(1024) NOT NULL,
"status" bit NOT NULL DEFAULT FALSE,
"description" varchar(512),
"creator" varchar(64) DEFAULT '',
"create_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updater" varchar(64) DEFAULT '',
"update_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
"deleted" bit NOT NULL DEFAULT FALSE,
PRIMARY KEY ("id")
) COMMENT '系统敏感词';

View File

@ -110,6 +110,7 @@ yudao:
- system_sms_channel
- system_sms_template
- system_sms_log
- system_sensitive_word
- infra_codegen_column
- infra_codegen_table
- infra_test_demo

View File

@ -0,0 +1,71 @@
import request from '@/utils/request'
import qs from 'qs'
// 创建敏感词
export function createSensitiveWord(data) {
return request({
url: '/system/sensitive-word/create',
method: 'post',
data: data
})
}
// 更新敏感词
export function updateSensitiveWord(data) {
return request({
url: '/system/sensitive-word/update',
method: 'put',
data: data
})
}
// 删除敏感词
export function deleteSensitiveWord(id) {
return request({
url: '/system/sensitive-word/delete?id=' + id,
method: 'delete'
})
}
// 获得敏感词
export function getSensitiveWord(id) {
return request({
url: '/system/sensitive-word/get?id=' + id,
method: 'get'
})
}
// 获得敏感词分页
export function getSensitiveWordPage(query) {
return request({
url: '/system/sensitive-word/page',
method: 'get',
params: query
})
}
// 导出敏感词 Excel
export function exportSensitiveWordExcel(query) {
return request({
url: '/system/sensitive-word/export-excel',
method: 'get',
params: query,
responseType: 'blob'
})
}
// 获取所有敏感词的标签数组
export function getSensitiveWordTags(){
return request({
url: '/system/sensitive-word/get-tags',
method: 'get'
})
}
// 获得文本所包含的不合法的敏感词数组
export function validateText(query) {
return request({
url: '/system/sensitive-word/validate-text?' + qs.stringify(query, {arrayFormat: 'repeat'}),
method: 'get',
})
}

View File

@ -163,7 +163,6 @@ export default {
//
initFormOnChanged(element) {
let activatedElement = element;
// debugger
if (!activatedElement) {
activatedElement =
window.bpmnInstances.elementRegistry.find(el => el.type === "bpmn:Process") ??

View File

@ -27,7 +27,6 @@ export default {
download0(data, fileName, mineType) {
// 创建 blob
let blob = new Blob([data], {type: mineType});
debugger
// 创建 href 超链接,点击进行下载
window.URL = window.URL || window.webkitURL;
let href = URL.createObjectURL(blob);

View File

@ -57,7 +57,6 @@ function filterAsyncRouter(asyncRouterMap, lastRouter = false, type = false) {
}
// 处理 component 属性
if (route.children) { // 父节点
// debugger
if (route.parentId === 0) {
route.component = Layout
} else {

View File

@ -64,11 +64,6 @@ export const DICT_TYPE = {
* @returns {*|Array} 数据字典数组
*/
export function getDictDatas(dictType) {
// if (dictType === 'bpm_task_assign_script') {
// console.log(store.getters.dict_datas[dictType]);
// debugger
// }
// debugger
return store.getters.dict_datas[dictType] || []
}
@ -95,8 +90,6 @@ export function getDictDatas2(dictType, values) {
results.push(dict);
}
}
// debugger
// console.log(results);
return results;
}

View File

@ -84,7 +84,6 @@ export default {
{ required: true, trigger: "blur", message: "租户不能为空" },
{
validator: (rule, value, callback) => {
// debugger
getTenantIdByName(value).then(res => {
const tenantId = res.data;
if (tenantId && tenantId >= 0) {

View File

@ -0,0 +1,326 @@
<template>
<div class="app-container">
<!-- 搜索工作栏 -->
<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
<el-form-item label="敏感词" prop="name">
<el-input v-model="queryParams.name" placeholder="请输入敏感词" clearable @keyup.enter.native="handleQuery"/>
</el-form-item>
<el-form-item label="标签" prop="tag">
<el-select v-model="queryParams.tag" placeholder="请选择标签" clearable @keyup.enter.native="handleQuery">
<el-option v-for="tag in tags" :key="tag" :label="tag" :value="tag"/>
</el-select>
</el-form-item>
<el-form-item label="状态" prop="status">
<el-select v-model="queryParams.status" placeholder="请选择启用状态" clearable>
<el-option v-for="dict in this.getDictDatas(DICT_TYPE.COMMON_STATUS)"
:key="dict.value" :label="dict.label" :value="dict.value"/>
</el-select>
</el-form-item>
<el-form-item label="创建时间">
<el-date-picker v-model="dateRangeCreateTime" size="small" style="width: 240px" value-format="yyyy-MM-dd"
type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"/>
</el-form-item>
<el-form-item>
<el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
<el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
</el-form-item>
</el-form>
<!-- 操作工具栏 -->
<el-row :gutter="10" class="mb8">
<el-col :span="1.5">
<el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
v-hasPermi="['system:sensitive-word:create']">新增</el-button>
</el-col>
<el-col :span="1.5">
<el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
:loading="exportLoading" v-hasPermi="['system:sensitive-word:export']">导出</el-button>
</el-col>
<el-col :span="1.5">
<el-button type="success" plain icon="el-icon-document-checked" size="mini" @click="handleTest">测试</el-button>
</el-col>
<right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
</el-row>
<!-- 列表 -->
<el-table v-loading="loading" :data="list">
<el-table-column label="编号" align="center" prop="id"/>
<el-table-column label="敏感词" align="center" prop="name"/>
<el-table-column label="状态" align="center" prop="status">
<template slot-scope="scope">
<dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.status"/>
</template>
</el-table-column>
<el-table-column label="描述" align="center" prop="description"/>
<el-table-column label="标签" align="center" prop="tags">
</el-table-column>
<el-table-column label="创建时间" align="center" prop="createTime" width="180">
<template slot-scope="scope">
<el-tag :disable-transitions="true" v-for="(tag, index) in scope.row.tags" :index="index">
{{ tag }}
</el-tag>
</template>
</el-table-column>
<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
<template slot-scope="scope">
<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
v-hasPermi="['system:sensitive-word:update']">修改
</el-button>
<el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
v-hasPermi="['system:sensitive-word:delete']">删除
</el-button>
</template>
</el-table-column>
</el-table>
<!-- 分页组件 -->
<pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
@pagination="getList"/>
<!-- 对话框(添加 / 修改) -->
<el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
<el-form ref="form" :model="form" :rules="rules" label-width="80px">
<el-form-item label="敏感词" prop="name">
<el-input v-model="form.name" placeholder="请输入敏感词"/>
</el-form-item>
<el-form-item label="状态">
<el-radio-group v-model="form.status">
<el-radio v-for="dict in this.getDictDatas(DICT_TYPE.COMMON_STATUS)"
:key="dict.value" :label="parseInt(dict.value)">{{dict.label}}</el-radio>
</el-radio-group>
</el-form-item>
<el-form-item label="备注" prop="description">
<el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
</el-form-item>
<el-form-item label="标签" prop="tags">
<el-select v-model="form.tags" multiple filterable allow-create placeholder="请选择文章标签" style="width: 380px" >
<el-option v-for="tag in tags" :key="tag" :label="tag" :value="tag"/>
</el-select>
</el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button type="primary" @click="submitForm"> </el-button>
<el-button @click="cancel"> </el-button>
</div>
</el-dialog>
<!-- 对话框(测试敏感词) -->
<el-dialog title="检测敏感词" :visible.sync="testOpen" width="500px" append-to-body>
<el-form ref="testForm" :model="testForm" :rules="testRules" label-width="80px">
<el-form-item label="文本" prop="text">
<el-input type="textarea" v-model="testForm.text" placeholder="请输入测试文本"/>
</el-form-item>
<el-form-item label="标签" prop="tags">
<el-select v-model="testForm.tags" multiple placeholder="请选择标签" style="width: 380px" >
<el-option v-for="tag in tags" :key="tag" :label="tag" :value="tag"/>
</el-select>
</el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button type="primary" @click="submitTestForm"> </el-button>
<el-button @click="cancelTest"> </el-button>
</div>
</el-dialog>
</div>
</template>
<script>
import { createSensitiveWord, updateSensitiveWord, deleteSensitiveWord, getSensitiveWord, getSensitiveWordPage,
exportSensitiveWordExcel, validateText, getSensitiveWordTags} from "@/api/system/sensitiveWord";
import {CommonStatusEnum} from "@/utils/constants";
export default {
name: "SensitiveWord",
data() {
return {
//
loading: true,
//
exportLoading: false,
//
showSearch: true,
//
total: 0,
//
list: [],
//
title: "",
//
open: false,
testOpen: false,
dateRangeCreateTime: [],
tags: [],
//
queryParams: {
pageNo: 1,
pageSize: 10,
name: null,
tag: null,
},
//
form: {},
//
testForm: {},
//
rules: {
name: [{required: true, message: "敏感词不能为空", trigger: "blur"}],
tags: [{required: true, message: "标签不能为空", trigger: "blur"}]
},
testRules: {
text: [{required: true, message: "测试文本不能为空", trigger: 'blur'}],
}
};
},
created() {
this.getTags();
this.getList();
},
methods: {
/** 初始化标签select*/
getTags(){
getSensitiveWordTags().then(response => {
this.tags = response.data;
});
},
/** 查询列表 */
getList() {
this.loading = true;
//
let params = {...this.queryParams};
this.addBeginAndEndTime(params, this.dateRangeCreateTime, 'createTime');
//
getSensitiveWordPage(params).then(response => {
this.list = response.data.list;
this.total = response.data.total;
this.loading = false;
});
},
/** 取消按钮 */
cancel() {
this.open = false;
this.reset();
},
/** 取消按钮 */
cancelTest() {
this.resetTest();
},
/** 表单重置 */
reset() {
this.form = {
id: undefined,
name: undefined,
description: undefined,
tags: undefined,
status: CommonStatusEnum.ENABLE
};
this.resetForm("form");
},
/** 表单重置 */
resetTest() {
this.testForm = {
text: undefined,
tags: undefined
};
this.resetForm("testForm");
},
/** 搜索按钮操作 */
handleQuery() {
this.queryParams.pageNo = 1;
this.getList();
},
/** 重置按钮操作 */
resetQuery() {
this.dateRangeCreateTime = [];
this.resetForm("queryForm");
this.handleQuery();
},
/** 新增按钮操作 */
handleAdd() {
this.reset();
this.open = true;
this.title = "添加敏感词";
},
/** 测试敏感词按钮操作 */
handleTest() {
this.resetTest();
this.testOpen = true;
this.titleTest = "检测文本是否含有敏感词";
},
/** 修改按钮操作 */
handleUpdate(row) {
this.reset();
const id = row.id;
getSensitiveWord(id).then(response => {
this.form = response.data;
this.open = true;
this.title = "修改敏感词";
});
},
/** 提交按钮 */
submitForm() {
this.$refs["form"].validate(valid => {
if (!valid) {
return;
}
//
if (this.form.id != null) {
updateSensitiveWord(this.form).then(response => {
this.$modal.msgSuccess("修改成功");
this.open = false;
this.getList();
}).catch(err => {});
return;
}
//
createSensitiveWord(this.form).then(response => {
this.$modal.msgSuccess("新增成功");
this.open = false;
this.getList();
}).catch(err => {});
});
},
/** 测试文本2提交按钮 */
submitTestForm() {
this.$refs["testForm"].validate(valid => {
if (!valid) {
return;
}
validateText(this.testForm).then(response => {
if (response.data.length === 0) {
this.$modal.msgSuccess("不包含敏感词!");
return;
}
this.$modal.msgWarning("包含敏感词:" + response.data.join(', '));
})
});
},
/** 删除按钮操作 */
handleDelete(row) {
const id = row.id;
this.$modal.confirm('是否确认删除敏感词编号为"' + id + '"的数据项?').then(function () {
return deleteSensitiveWord(id);
}).then(() => {
this.getList();
this.$modal.msgSuccess("删除成功");
}).catch(() => {
});
},
/** 导出按钮操作 */
handleExport() {
//
let params = {...this.queryParams};
params.pageNo = undefined;
params.pageSize = undefined;
this.addBeginAndEndTime(params, this.dateRangeCreateTime, 'createTime');
//
this.$modal.confirm('是否确认导出所有敏感词数据项?').then(() => {
this.exportLoading = true;
return exportSensitiveWordExcel(params);
}).then(response => {
this.$download.excel(response, '${table.classComment}.xls');
this.exportLoading = false;
}).catch(() => {
});
},
}
};
</script>

File diff suppressed because it is too large Load Diff