内容安全审核
This commit is contained in:
36
service/sensitive_service.py
Normal file
36
service/sensitive_service.py
Normal file
@ -0,0 +1,36 @@
|
||||
from mysql.connector import Error as MySQLError
|
||||
|
||||
from ds.db import db
|
||||
|
||||
|
||||
def get_all_sensitive_words() -> list[str]:
|
||||
"""
|
||||
获取所有敏感词(返回纯字符串列表、用于过滤业务)
|
||||
|
||||
返回:
|
||||
list[str]: 包含所有敏感词的数组
|
||||
|
||||
异常:
|
||||
MySQLError: 数据库操作相关错误
|
||||
"""
|
||||
conn = None
|
||||
cursor = None
|
||||
try:
|
||||
# 获取数据库连接
|
||||
conn = db.get_connection()
|
||||
cursor = conn.cursor(dictionary=True)
|
||||
|
||||
# 执行查询(只获取敏感词字段、按ID排序)
|
||||
query = "SELECT name FROM sensitives ORDER BY id"
|
||||
cursor.execute(query)
|
||||
sensitive_records = cursor.fetchall()
|
||||
|
||||
# 提取敏感词到纯字符串数组
|
||||
return [record['name'] for record in sensitive_records]
|
||||
|
||||
except MySQLError as e:
|
||||
# 数据库错误向上抛出、由调用方处理
|
||||
raise MySQLError(f"查询敏感词列表失败: {str(e)}") from e
|
||||
finally:
|
||||
# 确保数据库连接正确释放
|
||||
db.close_connection(conn, cursor)
|
||||
Reference in New Issue
Block a user