diff --git a/service/sensitive_service.py b/service/sensitive_service.py index 3564017..fe64235 100644 --- a/service/sensitive_service.py +++ b/service/sensitive_service.py @@ -107,9 +107,7 @@ async def get_sensitive( # 3. 获取所有敏感信息记录 # ------------------------------ @router.get("", response_model=APIResponse, summary="获取所有敏感信息记录") -async def get_all_sensitives( - current_user: UserResponse = Depends(get_current_user) # 需登录认证 -): +async def get_all_sensitives(): """ 获取所有敏感信息记录: - 需登录认证 @@ -256,3 +254,36 @@ async def delete_sensitive( raise Exception(f"删除敏感信息记录失败:{str(e)}") from e finally: db.close_connection(conn, cursor) + + +def get_all_sensitive_words() -> list[str]: + """ + 获取所有敏感词,返回字符串数组 + + 返回: + list[str]: 包含所有敏感词的数组 + + 异常: + MySQLError: 数据库操作相关错误 + """ + conn = None + cursor = None + try: + # 获取数据库连接 + conn = db.get_connection() + cursor = conn.cursor(dictionary=True) + + # 执行查询,只获取敏感词字段 + 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) \ No newline at end of file