feat: add menu favorites API under /web/admin/menu-favorites
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
1a0c58f82d
commit
7898d65437
4 files changed
+259
No files matched your search
@@ -28,6 +28,7 @@ from app.crud import role as role_svc
|
||||
from app.crud import user as user_svc
|
||||
from app.crud import oss as oss_svc
|
||||
from app.models.models import SysDept, SysDictData, SysRole, SysUser
|
||||
from app.schemas.menu_favorite import MenuFavoriteCreateBody, MenuFavoriteDeleteBody
|
||||
from app.services.web_auth_roles import _map_role_name_to_web
|
||||
|
||||
router = APIRouter(prefix="/web/admin", tags=["运营端管理"])
|
||||
@@ -1432,3 +1433,65 @@ def attachment_preview_url(
|
||||
return success_response({"previewUrl": url})
|
||||
except Exception as e:
|
||||
return error_response(500, f"获取预览链接失败: {str(e)}")
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 用户菜单收藏
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
@router.get("/menu-favorites")
|
||||
def list_menu_favorites(
|
||||
user: dict = Depends(require_admin_or_super_admin),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
user_id = user.get("user_id")
|
||||
if not user_id:
|
||||
return error_response(401, "用户无效")
|
||||
tenant_no = _get_tenant_no(db, user)
|
||||
from app.crud import menu_favorite as fav_svc
|
||||
|
||||
return success_response(fav_svc.list_favorites(db, user_id=user_id, tenant_no=tenant_no))
|
||||
|
||||
|
||||
@router.post("/menu-favorites")
|
||||
def create_menu_favorite(
|
||||
body: MenuFavoriteCreateBody,
|
||||
user: dict = Depends(require_admin_or_super_admin),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
from app.crud import menu_favorite as fav_svc
|
||||
|
||||
user_id = user.get("user_id")
|
||||
if not user_id:
|
||||
return error_response(401, "用户无效")
|
||||
tenant_no = _get_tenant_no(db, user)
|
||||
try:
|
||||
data = fav_svc.add_favorite(
|
||||
db,
|
||||
user_id=user_id,
|
||||
tenant_no=tenant_no,
|
||||
menu_path=body.path,
|
||||
create_dept_id=str(user.get("dept_id") or ""),
|
||||
)
|
||||
return success_response(data)
|
||||
except ValueError as e:
|
||||
return error_response(400, str(e))
|
||||
|
||||
|
||||
@router.delete("/menu-favorites")
|
||||
def delete_menu_favorite(
|
||||
body: MenuFavoriteDeleteBody,
|
||||
user: dict = Depends(require_admin_or_super_admin),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
from app.crud import menu_favorite as fav_svc
|
||||
|
||||
user_id = user.get("user_id")
|
||||
if not user_id:
|
||||
return error_response(401, "用户无效")
|
||||
tenant_no = _get_tenant_no(db, user)
|
||||
fav_svc.soft_delete_favorite(
|
||||
db, user_id=user_id, tenant_no=tenant_no, menu_path=body.path
|
||||
)
|
||||
return success_response({"path": body.path})
|
||||
@@ -0,0 +1,177 @@
|
||||
"""用户菜单收藏 CRUD"""
|
||||
from __future__ import annotations
|
||||
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.models.models import SysUserMenuFavorite
|
||||
|
||||
MAX_FAVORITES = 10
|
||||
|
||||
|
||||
def normalize_menu_path(path: str) -> str:
|
||||
p = (path or "").strip()
|
||||
if not p:
|
||||
return ""
|
||||
if not p.startswith("/"):
|
||||
p = "/" + p
|
||||
return p
|
||||
|
||||
|
||||
def _row_to_dict(row: SysUserMenuFavorite) -> Dict[str, Any]:
|
||||
return {
|
||||
"id": row.id,
|
||||
"path": row.menu_path or "",
|
||||
"orderNum": row.order_num,
|
||||
"createTime": row.create_time.isoformat(sep=" ", timespec="seconds") if row.create_time else None,
|
||||
}
|
||||
|
||||
|
||||
def _next_order_num(db: Session, *, user_id: str, tenant_no: str) -> int:
|
||||
max_order = (
|
||||
db.query(SysUserMenuFavorite.order_num)
|
||||
.filter(
|
||||
SysUserMenuFavorite.del_flag == 0,
|
||||
SysUserMenuFavorite.user_id == user_id,
|
||||
SysUserMenuFavorite.tenant_no == tenant_no,
|
||||
)
|
||||
.order_by(SysUserMenuFavorite.order_num.desc())
|
||||
.first()
|
||||
)
|
||||
return (max_order[0] if max_order and max_order[0] is not None else 0) + 1
|
||||
|
||||
|
||||
def list_favorites(
|
||||
db: Session,
|
||||
*,
|
||||
user_id: str,
|
||||
tenant_no: str,
|
||||
) -> List[Dict[str, Any]]:
|
||||
rows = (
|
||||
db.query(SysUserMenuFavorite)
|
||||
.filter(
|
||||
SysUserMenuFavorite.del_flag == 0,
|
||||
SysUserMenuFavorite.user_id == user_id,
|
||||
SysUserMenuFavorite.tenant_no == tenant_no,
|
||||
)
|
||||
.order_by(
|
||||
SysUserMenuFavorite.order_num.asc(),
|
||||
SysUserMenuFavorite.create_time.asc(),
|
||||
)
|
||||
.all()
|
||||
)
|
||||
return [_row_to_dict(r) for r in rows]
|
||||
|
||||
|
||||
def count_active(db: Session, *, user_id: str, tenant_no: str) -> int:
|
||||
return (
|
||||
db.query(SysUserMenuFavorite)
|
||||
.filter(
|
||||
SysUserMenuFavorite.del_flag == 0,
|
||||
SysUserMenuFavorite.user_id == user_id,
|
||||
SysUserMenuFavorite.tenant_no == tenant_no,
|
||||
)
|
||||
.count()
|
||||
)
|
||||
|
||||
|
||||
def find_active(
|
||||
db: Session, *, user_id: str, tenant_no: str, menu_path: str
|
||||
) -> Optional[SysUserMenuFavorite]:
|
||||
return (
|
||||
db.query(SysUserMenuFavorite)
|
||||
.filter(
|
||||
SysUserMenuFavorite.del_flag == 0,
|
||||
SysUserMenuFavorite.user_id == user_id,
|
||||
SysUserMenuFavorite.tenant_no == tenant_no,
|
||||
SysUserMenuFavorite.menu_path == menu_path,
|
||||
)
|
||||
.first()
|
||||
)
|
||||
|
||||
|
||||
def find_soft_deleted(
|
||||
db: Session, *, user_id: str, tenant_no: str, menu_path: str
|
||||
) -> Optional[SysUserMenuFavorite]:
|
||||
return (
|
||||
db.query(SysUserMenuFavorite)
|
||||
.filter(
|
||||
SysUserMenuFavorite.del_flag == 1,
|
||||
SysUserMenuFavorite.user_id == user_id,
|
||||
SysUserMenuFavorite.tenant_no == tenant_no,
|
||||
SysUserMenuFavorite.menu_path == menu_path,
|
||||
)
|
||||
.first()
|
||||
)
|
||||
|
||||
|
||||
def add_favorite(
|
||||
db: Session,
|
||||
*,
|
||||
user_id: str,
|
||||
tenant_no: str,
|
||||
menu_path: str,
|
||||
create_dept_id: str = "",
|
||||
) -> Dict[str, Any]:
|
||||
path = normalize_menu_path(menu_path)
|
||||
if not path:
|
||||
raise ValueError("菜单路径无效")
|
||||
|
||||
existing = find_active(db, user_id=user_id, tenant_no=tenant_no, menu_path=path)
|
||||
if existing:
|
||||
return _row_to_dict(existing)
|
||||
|
||||
if count_active(db, user_id=user_id, tenant_no=tenant_no) >= MAX_FAVORITES:
|
||||
raise ValueError(f"最多收藏 {MAX_FAVORITES} 个菜单")
|
||||
|
||||
now = datetime.now()
|
||||
next_order = _next_order_num(db, user_id=user_id, tenant_no=tenant_no)
|
||||
|
||||
deleted = find_soft_deleted(db, user_id=user_id, tenant_no=tenant_no, menu_path=path)
|
||||
if deleted:
|
||||
deleted.del_flag = 0
|
||||
deleted.order_num = next_order
|
||||
deleted.update_by = user_id
|
||||
deleted.update_time = now
|
||||
db.commit()
|
||||
db.refresh(deleted)
|
||||
return _row_to_dict(deleted)
|
||||
|
||||
row = SysUserMenuFavorite(
|
||||
id=str(uuid.uuid4()),
|
||||
user_id=user_id,
|
||||
menu_path=path,
|
||||
tenant_no=tenant_no,
|
||||
order_num=next_order,
|
||||
create_by=user_id,
|
||||
create_dept_id=create_dept_id or "",
|
||||
create_time=now,
|
||||
update_by=user_id,
|
||||
update_time=now,
|
||||
del_flag=0,
|
||||
)
|
||||
db.add(row)
|
||||
db.commit()
|
||||
db.refresh(row)
|
||||
return _row_to_dict(row)
|
||||
|
||||
|
||||
def soft_delete_favorite(
|
||||
db: Session,
|
||||
*,
|
||||
user_id: str,
|
||||
tenant_no: str,
|
||||
menu_path: str,
|
||||
) -> bool:
|
||||
path = normalize_menu_path(menu_path)
|
||||
row = find_active(db, user_id=user_id, tenant_no=tenant_no, menu_path=path)
|
||||
if not row:
|
||||
return False
|
||||
row.del_flag = 1
|
||||
row.update_by = user_id
|
||||
row.update_time = datetime.now()
|
||||
db.commit()
|
||||
return True
|
||||
@@ -0,0 +1,9 @@
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
|
||||
class MenuFavoriteCreateBody(BaseModel):
|
||||
path: str = Field(..., min_length=1, max_length=512)
|
||||
|
||||
|
||||
class MenuFavoriteDeleteBody(BaseModel):
|
||||
path: str = Field(..., min_length=1, max_length=512)
|
||||
@@ -0,0 +1,10 @@
|
||||
from app.crud.menu_favorite import normalize_menu_path, MAX_FAVORITES
|
||||
|
||||
|
||||
def test_normalize_menu_path_strips_and_requires_slash():
|
||||
assert normalize_menu_path(" /crm/customer ") == "/crm/customer"
|
||||
assert normalize_menu_path("crm/customer") == "/crm/customer"
|
||||
|
||||
|
||||
def test_max_favorites_constant():
|
||||
assert MAX_FAVORITES == 10
|
||||
Reference in new issue
Block a user