增加了账号管理的分组功能

This commit is contained in:
yml2213
2026-06-22 15:09:51 +08:00
parent 5fa91c1bb4
commit 9af92fc2a9
13 changed files with 437 additions and 55 deletions
+54 -4
View File
@@ -7,7 +7,7 @@ from sqlalchemy.orm import Session
from ..database import get_db
from ..models import User, Account, AuditLog
from ..schemas import AccountImport, AccountAssign, AccountOut
from ..schemas import AccountImport, AccountAssign, AccountTag, AccountOut
from ..deps import get_current_user, require_permission
from ..permissions import has_permission
@@ -29,6 +29,7 @@ def _split_account_line(line: str) -> list[str]:
@router.get("", response_model=list[AccountOut])
def list_accounts(
assigned_only: bool = Query(False),
tag: str = Query(None),
db: Session = Depends(get_db),
current: User = Depends(get_current_user),
):
@@ -45,11 +46,15 @@ def list_accounts(
if assigned_only and has_permission(current.role, "account:view_all"):
query = query.filter(Account.assigned_to.isnot(None))
if tag:
query = query.filter(Account.tag == tag)
accounts = query.order_by(Account.id).all()
result = []
for acc in accounts:
item = AccountOut(
id=acc.id, username=acc.username, remark=acc.remark or "",
tag=acc.tag or "",
assigned_to=acc.assigned_to,
assigned_username=acc.assigned_user.username if acc.assigned_user else None,
created_at=acc.created_at,
@@ -69,7 +74,7 @@ def import_accounts(
db: Session = Depends(get_db),
current: User = Depends(require_permission("account:import")),
):
"""批量导入账号。格式:用户名|密码|邮箱|邮箱密码"""
"""批量导入账号。格式:用户名|密码|邮箱|邮箱密码|标签(可选)"""
from core.douyu.email_verifier import get_email_config_for_account
accounts = []
@@ -79,11 +84,12 @@ def import_accounts(
if not line or line.startswith('#'):
continue
parts = _split_account_line(line)
if len(parts) != 4:
if len(parts) < 4:
skipped += 1
continue
username, password, email, email_password = [p.strip() for p in parts]
username, password, email, email_password = [p.strip() for p in parts[:4]]
tag = parts[4].strip() if len(parts) > 4 else ""
if not all([username, password, email, email_password]):
skipped += 1
continue
@@ -99,6 +105,7 @@ def import_accounts(
email_password=email_password,
email_imap_server=email_cfg['server'],
email_imap_port=email_cfg.get('port', 993),
tag=tag,
))
if accounts:
@@ -135,6 +142,49 @@ def assign_account(
return {"message": "已分配", "success": True}
@router.put("/{account_id}/tag")
def set_account_tag(
account_id: int,
req: AccountTag,
db: Session = Depends(get_db),
current: User = Depends(require_permission("account:import")),
):
"""设置单个账号标签。"""
acc = db.query(Account).filter(Account.id == account_id).first()
if not acc:
raise HTTPException(status_code=404, detail="账号不存在")
acc.tag = (req.tag or "").strip()
db.commit()
return {"message": "标签已更新", "success": True}
@router.put("/batch-tag")
def batch_tag(
req: AccountTag,
db: Session = Depends(get_db),
current: User = Depends(require_permission("account:import")),
):
"""批量设置账号标签。"""
if not req.account_ids:
raise HTTPException(status_code=400, detail="请选择账号")
tag = (req.tag or "").strip()
count = db.query(Account).filter(Account.id.in_(req.account_ids)).update(
{Account.tag: tag}, synchronize_session=False
)
db.commit()
return {"message": f"已为 {count} 个账号设置标签", "success": True}
@router.get("/tags/list")
def list_tags(
db: Session = Depends(get_db),
current: User = Depends(get_current_user),
):
"""获取所有标签列表。"""
tags = db.query(Account.tag).filter(Account.tag != "", Account.tag.isnot(None)).distinct().all()
return [t[0] for t in tags if t[0]]
@router.delete("/{account_id}")
def delete_account(
account_id: int,