Files
banban/talkingq-url/services/role_service.py
2026-03-24 15:04:36 +08:00

57 lines
1.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from typing import Dict, List, Optional
from services.role_manager import role_manager
class RoleService:
async def get_roles_paginated(self, page: int, page_size: int, search: Optional[str] = None):
"""分页获取角色列表,支持搜索"""
roles = await role_manager.get_all_roles()
filtered_roles = {}
for key, config in roles.items():
if search:
name = config.get("name", "")
description = config.get("description", "")
if search.lower() not in name.lower() and search.lower() not in description.lower():
continue
filtered_roles[key] = config
total_roles = len(filtered_roles)
start_idx = (page - 1) * page_size
end_idx = start_idx + page_size
role_items = list(filtered_roles.items())
paged_roles = role_items[start_idx:end_idx]
result = []
for key, config in paged_roles:
languages = []
if "multilingual" in config:
languages = list(config["multilingual"].keys())
result.append({
"role_key": key,
"name": config.get("name", "未命名角色"),
"description": config.get("description", ""),
"languages": languages
})
return {
"total": total_roles,
"page": page,
"page_size": page_size,
"data": result
}
async def get_role_summaries(self) -> List[Dict]:
"""获取角色简要信息列表仅包含key和name"""
roles = await role_manager.get_all_roles()
return [
{
"role_key": key,
"name": config.get("name", "未命名角色")
}
for key, config in roles.items()
]
role_service = RoleService()