实现 SensitiveWord API 实现类

pull/2/head
YunaiV 2022-04-09 10:48:47 +08:00
parent 3f7d7c3bfa
commit 696756b3c8
16 changed files with 9171 additions and 8253 deletions

View File

@ -1,16 +1,30 @@
package cn.iocoder.yudao.module.system.api.sensitiveword;
import java.util.List;
import java.util.Set;
/**
*
* API
*
* @author: <>
* @since: 2022/3/23 17:00
* @description:
* @modification:
* @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

@ -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

@ -1,22 +1,4 @@
GET http://localhost:81/dev-api/admin-api/system/sensitive-word/get-all-tags
Accept: application/json
Authorization: Bearer 385d533b781f44f6bb21ea08afeec47c
###
POST http://localhost:81/dev-api/admin-api/system/sensitive-word/create
Content-Type: application/json
Authorization: Bearer 1649ff1f8b9a4eeeb458fe93a71c78b5
{
"name": "test",
"tags": [
"bbb,aaa"
],
"description": "test",
"status": true
}
###
### 请求 /system/sensitive-word/validate-text 接口 => 成功
GET {{baseUrl}}/system/sensitive-word/validate-text?text=XXX&tags=短信&tags=蔬菜
Authorization: Bearer {{token}}
tenant-id: {{adminTenentId}}

View File

@ -75,13 +75,6 @@ public class SensitiveWordController {
return success(SensitiveWordConvert.INSTANCE.convertPage(pageResult));
}
@GetMapping("/get-tags")
@ApiOperation("获取所有敏感词的标签数组")
@PreAuthorize("@ss.hasPermission('system:sensitive-word:query')")
public CommonResult<Set<String>> getSensitiveWordTags() throws IOException {
return success(sensitiveWordService.getSensitiveWordTags());
}
@GetMapping("/export-excel")
@ApiOperation("导出敏感词 Excel")
@PreAuthorize("@ss.hasPermission('system:sensitive-word:export')")
@ -94,11 +87,18 @@ public class SensitiveWordController {
ExcelUtils.write(response, "敏感词.xls", "数据", SensitiveWordExcelVO.class, datas);
}
// @GetMapping("/is-sensitive-word-by-text-and-tag")
// @ApiOperation("通过tag判断传入text是否含有敏感词")
// @PreAuthorize("@ss.hasPermission('system:sensitive-word:checkbytextandtag')")
// public CommonResult<Boolean> isSensitiveWordByTextAndTag(@NotBlank String text, @NotBlank String tag) throws IOException {
// return success(sensitiveWordApi.isSensitiveWordByTextAndTag(text,tag));
// }
@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

@ -83,4 +83,22 @@ public interface SensitiveWordService {
*/
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

@ -1,6 +1,7 @@
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;
@ -11,9 +12,11 @@ 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.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
@ -23,7 +26,8 @@ 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.*;
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
@ -71,9 +75,14 @@ public class SensitiveWordServiceImpl implements SensitiveWordService {
@Resource
private SensitiveWordProducer sensitiveWordProducer;
@Resource
@Lazy
private SensitiveWordService self;
/**
*
*/
private volatile SimpleTrie defaultSensitiveWordTrie = new SimpleTrie(Collections.emptySet());
/**
*
*/
private volatile Map<String, SimpleTrie> tagSensitiveWordTries = Collections.emptyMap();
/**
* {@link #sensitiveWordCache}
@ -91,15 +100,38 @@ public class SensitiveWordServiceImpl implements SensitiveWordService {
Set<String> tags = new HashSet<>();
sensitiveWordList.forEach(word -> tags.addAll(word.getTags()));
sensitiveWordTagsCache = tags;
// 写入 defaultSensitiveWordTrie、tagSensitiveWordTries 缓存
initSensitiveWordTrie(sensitiveWordList);
// 写入 sensitiveWordCache 缓存
sensitiveWordCache = CollectionUtils.convertMap(sensitiveWordList, SensitiveWordDO::getId);
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() {
self.initLocalCache();
initLocalCache();
}
/**
@ -203,4 +235,39 @@ public class SensitiveWordServiceImpl implements SensitiveWordService {
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

@ -1,5 +1,7 @@
package cn.iocoder.yudao.module.system.util.collection;
import cn.hutool.core.collection.CollUtil;
import java.util.*;
/**
@ -27,10 +29,10 @@ public class SimpleTrie {
*
* @param strs
*/
public SimpleTrie(List<String> strs) {
public SimpleTrie(Collection<String> strs) {
children = new HashMap<>();
// 构建树
Collections.sort(strs); // 排序,优先使用较短的前缀
CollUtil.sort(strs, String::compareTo); // 排序,优先使用较短的前缀
for (String str : strs) {
Map<Character, Object> child = children;
// 遍历每个字符

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