实现敏感词检测后,失败发生邮箱
This commit is contained in:
64419
schoolNewsServ/.bin/mysql/sensitiveData/sensitive_word_dict.txt
Normal file
64419
schoolNewsServ/.bin/mysql/sensitiveData/sensitive_word_dict.txt
Normal file
File diff suppressed because it is too large
Load Diff
181
schoolNewsServ/.bin/mysql/sensitiveData/writeWord.py
Normal file
181
schoolNewsServ/.bin/mysql/sensitiveData/writeWord.py
Normal file
@@ -0,0 +1,181 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
敏感词批量导入脚本
|
||||
从 sensitive_word_dict.txt 文件读取敏感词并批量插入到数据库
|
||||
"""
|
||||
|
||||
import pymysql
|
||||
import os
|
||||
import sys
|
||||
from datetime import datetime
|
||||
|
||||
# 数据库配置
|
||||
DB_CONFIG = {
|
||||
'host': 'localhost',
|
||||
'port': 3306,
|
||||
'user': 'root',
|
||||
'password': '123456',
|
||||
'database': 'school_news',
|
||||
'charset': 'utf8mb4'
|
||||
}
|
||||
|
||||
def get_db_connection():
|
||||
"""获取数据库连接"""
|
||||
try:
|
||||
connection = pymysql.connect(**DB_CONFIG)
|
||||
return connection
|
||||
except Exception as e:
|
||||
print(f"数据库连接失败: {e}")
|
||||
return None
|
||||
|
||||
def read_sensitive_words(file_path):
|
||||
"""读取敏感词文件"""
|
||||
words = []
|
||||
try:
|
||||
with open(file_path, 'r', encoding='utf-8') as f:
|
||||
for line in f:
|
||||
word = line.strip()
|
||||
if word and len(word) > 0:
|
||||
words.append(word)
|
||||
print(f"成功读取 {len(words)} 个敏感词")
|
||||
return words
|
||||
except Exception as e:
|
||||
print(f"读取敏感词文件失败: {e}")
|
||||
return []
|
||||
|
||||
def batch_insert_words(connection, words, batch_size=1000):
|
||||
"""批量插入敏感词到数据库"""
|
||||
cursor = connection.cursor()
|
||||
|
||||
try:
|
||||
# 清空现有的deny类型敏感词(可选)
|
||||
print("清理现有的deny类型敏感词...")
|
||||
cursor.execute("DELETE FROM tb_sensitive_word WHERE type = 'deny'")
|
||||
|
||||
# 准备批量插入SQL
|
||||
insert_sql = "INSERT INTO tb_sensitive_word (word, type) VALUES (%s, %s)"
|
||||
|
||||
# 分批插入
|
||||
total_words = len(words)
|
||||
inserted_count = 0
|
||||
|
||||
for i in range(0, total_words, batch_size):
|
||||
batch_words = words[i:i + batch_size]
|
||||
batch_data = [(word, 'deny') for word in batch_words]
|
||||
|
||||
try:
|
||||
cursor.executemany(insert_sql, batch_data)
|
||||
connection.commit()
|
||||
inserted_count += len(batch_data)
|
||||
print(f"已插入 {inserted_count}/{total_words} 个敏感词")
|
||||
except Exception as e:
|
||||
print(f"批量插入失败: {e}")
|
||||
connection.rollback()
|
||||
break
|
||||
|
||||
print(f"批量插入完成,共插入 {inserted_count} 个敏感词")
|
||||
return inserted_count
|
||||
|
||||
except Exception as e:
|
||||
print(f"批量插入过程中发生错误: {e}")
|
||||
connection.rollback()
|
||||
return 0
|
||||
finally:
|
||||
cursor.close()
|
||||
|
||||
def check_duplicates(connection, words):
|
||||
"""检查重复的敏感词"""
|
||||
cursor = connection.cursor()
|
||||
|
||||
try:
|
||||
# 查询已存在的敏感词
|
||||
cursor.execute("SELECT word FROM tb_sensitive_word WHERE type = 'deny'")
|
||||
existing_words = set(row[0] for row in cursor.fetchall())
|
||||
|
||||
# 过滤重复词
|
||||
new_words = [word for word in words if word not in existing_words]
|
||||
duplicate_count = len(words) - len(new_words)
|
||||
|
||||
if duplicate_count > 0:
|
||||
print(f"发现 {duplicate_count} 个重复敏感词,将跳过")
|
||||
|
||||
return new_words
|
||||
|
||||
except Exception as e:
|
||||
print(f"检查重复词时发生错误: {e}")
|
||||
return words
|
||||
finally:
|
||||
cursor.close()
|
||||
|
||||
def main():
|
||||
"""主函数"""
|
||||
print("=" * 50)
|
||||
print("敏感词批量导入工具")
|
||||
print("=" * 50)
|
||||
|
||||
# 获取脚本所在目录
|
||||
script_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
dict_file = os.path.join(script_dir, 'sensitive_word_dict.txt')
|
||||
|
||||
# 检查敏感词文件是否存在
|
||||
if not os.path.exists(dict_file):
|
||||
print(f"敏感词文件不存在: {dict_file}")
|
||||
return
|
||||
|
||||
# 读取敏感词
|
||||
print("正在读取敏感词文件...")
|
||||
words = read_sensitive_words(dict_file)
|
||||
|
||||
if not words:
|
||||
print("没有读取到有效的敏感词")
|
||||
return
|
||||
|
||||
# 连接数据库
|
||||
print("正在连接数据库...")
|
||||
connection = get_db_connection()
|
||||
|
||||
if not connection:
|
||||
print("数据库连接失败,程序退出")
|
||||
return
|
||||
|
||||
try:
|
||||
# 检查重复词(可选,如果不需要可以注释掉)
|
||||
# print("正在检查重复敏感词...")
|
||||
# words = check_duplicates(connection, words)
|
||||
|
||||
if not words:
|
||||
print("所有敏感词都已存在,无需导入")
|
||||
return
|
||||
|
||||
# 确认导入
|
||||
print(f"准备导入 {len(words)} 个敏感词到数据库")
|
||||
confirm = input("是否继续?(y/N): ").strip().lower()
|
||||
|
||||
if confirm != 'y':
|
||||
print("用户取消导入")
|
||||
return
|
||||
|
||||
# 批量插入
|
||||
print("开始批量导入敏感词...")
|
||||
start_time = datetime.now()
|
||||
|
||||
inserted_count = batch_insert_words(connection, words)
|
||||
|
||||
end_time = datetime.now()
|
||||
duration = (end_time - start_time).total_seconds()
|
||||
|
||||
print("=" * 50)
|
||||
print(f"导入完成!")
|
||||
print(f"成功导入: {inserted_count} 个敏感词")
|
||||
print(f"耗时: {duration:.2f} 秒")
|
||||
print("=" * 50)
|
||||
|
||||
except Exception as e:
|
||||
print(f"程序执行过程中发生错误: {e}")
|
||||
finally:
|
||||
connection.close()
|
||||
print("数据库连接已关闭")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -15,7 +15,7 @@ CREATE TABLE `tb_resource` (
|
||||
`view_count` INT(11) DEFAULT 0 COMMENT '浏览次数',
|
||||
`like_count` INT(11) DEFAULT 0 COMMENT '点赞次数',
|
||||
`collect_count` INT(11) DEFAULT 0 COMMENT '收藏次数',
|
||||
`status` INT(4) DEFAULT 0 COMMENT '状态(0草稿 1已发布 2下架)',
|
||||
`status` INT(4) DEFAULT 0 COMMENT '状态(0草稿 1已发布 2下架 3审核中 4敏感词未通过)',
|
||||
`is_audited` TINYINT(1) NOT NULL DEFAULT 0 COMMENT '是否已审核',
|
||||
`is_recommend` TINYINT(1) DEFAULT 0 COMMENT '是否推荐',
|
||||
`is_banner` TINYINT(1) DEFAULT 0 COMMENT '是否轮播',
|
||||
|
||||
@@ -3,8 +3,8 @@ use school_news;
|
||||
DROP TABLE IF EXISTS `tb_sensitive_word`;
|
||||
CREATE TABLE `tb_sensitive_word` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`word` varchar(255) NOT NULL COMMENT '敏感词',
|
||||
`type` VARCHAR(10) NOT NULL COMMENT '类型allow\deny'
|
||||
`word` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL COMMENT '敏感词',
|
||||
`type` varchar(10) NOT NULL COMMENT '类型:allow-允许词,deny-禁用词',
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `word` (`word`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='敏感词表';
|
||||
UNIQUE KEY `uk_word` (`word`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='敏感词表';
|
||||
@@ -41,7 +41,8 @@ INSERT INTO `tb_sys_module` (id, module_id, name, code, description, icon, order
|
||||
('5', 'module_usercenter', '用户中心', 'usercenter', '用户中心模块', 'el-icon-user', 5, 1, '1', now()),
|
||||
('6', 'module_file', '文件管理', 'file', '文件管理模块', 'el-icon-folder', 6, 1, '1', now()),
|
||||
('7', 'module_crontab', '定时任务', 'crontab', '定时任务管理模块', 'el-icon-alarm-clock', 7, 1, '1', now()),
|
||||
('8', 'module_message', '消息通知', 'message', '消息通知管理模块', 'el-icon-message', 8, 1, '1', now());
|
||||
('8', 'module_message', '消息通知', 'message', '消息通知管理模块', 'el-icon-message', 8, 1, '1', now()),
|
||||
('9', 'module_sensitive', '敏感词管理', 'sensitive', '敏感词管理模块', 'el-icon-warning', 9, 1, '1', now());
|
||||
|
||||
-- 插入权限数据
|
||||
INSERT INTO `tb_sys_permission` (id,permission_id, name, code, description, module_id, creator, create_time) VALUES
|
||||
@@ -64,7 +65,8 @@ INSERT INTO `tb_sys_permission` (id,permission_id, name, code, description, modu
|
||||
('15','perm_crontab_execute', '定时任务执行', 'crontab:execute', '定时任务执行权限', 'module_crontab', '1', now()),
|
||||
('16','perm_message_manage', '消息管理', 'message:manage', '消息管理权限(管理端)', 'module_message', '1', now()),
|
||||
('17','perm_message_send', '消息发送', 'message:send', '消息发送权限', 'module_message', '1', now()),
|
||||
('18','perm_message_view', '消息查看', 'message:view', '消息查看权限(用户端)', 'module_message', '1', now());
|
||||
('18','perm_message_view', '消息查看', 'message:view', '消息查看权限(用户端)', 'module_message', '1', now()),
|
||||
('19','perm_sensitive_manage', '敏感词管理', 'sensitive:manage', '敏感词管理权限', 'module_sensitive', '1', now());
|
||||
|
||||
-- 插入角色-权限关联数据
|
||||
INSERT INTO `tb_sys_role_permission` (id, role_id, permission_id, creator, create_time) VALUES
|
||||
@@ -88,28 +90,30 @@ INSERT INTO `tb_sys_role_permission` (id, role_id, permission_id, creator, creat
|
||||
('17', 'superadmin', 'perm_crontab_execute', '1', now()),
|
||||
('18', 'superadmin', 'perm_message_manage', '1', now()),
|
||||
('19', 'superadmin', 'perm_message_send', '1', now()),
|
||||
('19.1', 'superadmin', 'perm_message_view', '1', now()),
|
||||
('20', 'superadmin', 'perm_message_view', '1', now()),
|
||||
('21', 'superadmin', 'perm_sensitive_manage', '1', now()),
|
||||
|
||||
-- 管理员:拥有业务管理权限,但没有系统日志等系统管理权限
|
||||
('20', 'admin', 'perm_default', '1', now()),
|
||||
('21', 'admin', 'perm_news_manage', '1', now()),
|
||||
('22', 'admin', 'perm_news_article_add', '1', now()),
|
||||
('23', 'admin', 'perm_study_manage', '1', now()),
|
||||
('24', 'admin', 'perm_achievement_manage', '1', now()),
|
||||
('25', 'admin', 'perm_ai_manage', '1', now()),
|
||||
('26', 'admin', 'perm_usercenter_manage', '1', now()),
|
||||
('27', 'admin', 'perm_file_manage', '1', now()),
|
||||
('28', 'admin', 'perm_message_manage', '1', now()),
|
||||
('29', 'admin', 'perm_message_send', '1', now()),
|
||||
('29.1', 'admin', 'perm_message_view', '1', now()),
|
||||
('22', 'admin', 'perm_default', '1', now()),
|
||||
('23', 'admin', 'perm_news_manage', '1', now()),
|
||||
('24', 'admin', 'perm_news_article_add', '1', now()),
|
||||
('25', 'admin', 'perm_study_manage', '1', now()),
|
||||
('26', 'admin', 'perm_achievement_manage', '1', now()),
|
||||
('27', 'admin', 'perm_ai_manage', '1', now()),
|
||||
('28', 'admin', 'perm_usercenter_manage', '1', now()),
|
||||
('29', 'admin', 'perm_file_manage', '1', now()),
|
||||
('30', 'admin', 'perm_message_manage', '1', now()),
|
||||
('31', 'admin', 'perm_message_send', '1', now()),
|
||||
('32', 'admin', 'perm_message_view', '1', now()),
|
||||
('33', 'admin', 'perm_sensitive_manage', '1', now()),
|
||||
|
||||
-- 自由角色:拥有用户视图相关的所有权限(前台用户权限)
|
||||
('30', 'freedom', 'perm_default', '1', now()),
|
||||
('31', 'freedom', 'perm_news_article_add', '1', now()),
|
||||
('32', 'freedom', 'perm_ai_manage', '1', now()),
|
||||
('33', 'freedom', 'perm_usercenter_manage', '1', now()),
|
||||
('34', 'freedom', 'perm_file_manage', '1', now()),
|
||||
('35', 'freedom', 'perm_message_view', '1', now());
|
||||
('40', 'freedom', 'perm_default', '1', now()),
|
||||
('41', 'freedom', 'perm_news_article_add', '1', now()),
|
||||
('42', 'freedom', 'perm_ai_manage', '1', now()),
|
||||
('43', 'freedom', 'perm_usercenter_manage', '1', now()),
|
||||
('44', 'freedom', 'perm_file_manage', '1', now()),
|
||||
('45', 'freedom', 'perm_message_view', '1', now());
|
||||
|
||||
-- 插入前端菜单数据
|
||||
INSERT INTO `tb_sys_menu` VALUES
|
||||
@@ -149,6 +153,7 @@ INSERT INTO `tb_sys_menu` VALUES
|
||||
('4001', 'menu_admin_banner', 'Banner管理', 'menu_admin_content_manage', '/admin/manage/content/banner', 'admin/manage/content/BannerManagementView', NULL, 1, 0, 'SidebarLayout', '1', NULL, '2025-10-27 17:26:06', '2025-10-29 11:49:56', NULL, 0),
|
||||
('4002', 'menu_admin_tag', '标签管理', 'menu_admin_content_manage', '/admin/manage/content/tag', 'admin/manage/content/TagManagementView', NULL, 2, 0, 'SidebarLayout', '1', NULL, '2025-10-27 17:26:06', '2025-10-29 11:49:56', NULL, 0),
|
||||
('4003', 'menu_admin_column', '栏目管理', 'menu_admin_content_manage', '/admin/manage/content/column', 'admin/manage/content/ColumnManagementView', NULL, 3, 0, 'SidebarLayout', '1', NULL, '2025-10-27 17:26:06', '2025-10-29 11:49:56', NULL, 0),
|
||||
('4004', 'menu_admin_sensitive', '敏感词管理', 'menu_admin_content_manage', '/admin/manage/content/sensitive', 'admin/manage/content/SensitiveManagementView', NULL, 4, 0, 'SidebarLayout', '1', NULL, '2025-11-22 11:30:00', '2025-11-22 11:30:00', NULL, 0),
|
||||
('5000', 'menu_admin_study_manage', '学习管理', NULL, '', '', 'admin/study.svg', 5, 0, 'SidebarLayout', '1', NULL, '2025-10-27 17:26:06', '2025-10-29 11:52:46', NULL, 0),
|
||||
('5002', 'menu_admin_task_manage', '任务管理', 'menu_admin_study_manage', '/admin/manage/study/task-manage', 'admin/manage/study/TaskManageView', NULL, 2, 0, 'SidebarLayout', '1', NULL, '2025-10-27 17:26:06', '2025-10-29 11:48:39', NULL, 0),
|
||||
('5003', 'menu_admin_study_records', '学习记录', 'menu_admin_study_manage', '/admin/manage/study/study-records', 'admin/manage/study/StudyRecordsView', NULL, 3, 0, 'SidebarLayout', '1', NULL, '2025-10-27 17:26:06', '2025-10-29 11:48:39', NULL, 0),
|
||||
@@ -212,6 +217,7 @@ INSERT INTO `tb_sys_menu_permission` (id, permission_id, menu_id, creator, creat
|
||||
('214', 'perm_news_manage', 'menu_admin_banner', '1', now()),
|
||||
('215', 'perm_news_manage', 'menu_admin_tag', '1', now()),
|
||||
('216', 'perm_news_manage', 'menu_admin_column', '1', now()),
|
||||
('217', 'perm_sensitive_manage', 'menu_admin_sensitive', '1', now()),
|
||||
('218', 'perm_study_manage', 'menu_admin_study_manage', '1', now()),
|
||||
('220', 'perm_study_manage', 'menu_admin_task_manage', '1', now()),
|
||||
('221', 'perm_study_manage', 'menu_admin_study_records', '1', now()),
|
||||
|
||||
Reference in New Issue
Block a user