mirror of
https://github.com/hongguang1994/SimNexus.git
synced 2026-11-24 14:39:09 +08:00
- 新增 RBAC 角色管理:roles 表 + user_roles 多对多,支持每用户分配多个角色,权限取并集 - 新增客服角色(can_support),可代替管理员回复用户咨询 - 通知系统新增 support audience,按角色路由(admin/support/all/user) - 登录页新增 SVG 验证码(JWT 签名,无服务端 session) - 前端新增多语言(zh/en)和主题(light/dark/system)支持 - 系统预置 5 个角色:全功能用户、只读用户、短信操作员、任务管理员、客服 - 新增页面:Roles、SupportAdmin、AdminTasks;用户管理支持多角色分配 - 更新 README.md 和 CLAUDE.md 覆盖所有新功能和架构说明 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
103 lines
2.8 KiB
Python
103 lines
2.8 KiB
Python
from fastapi import APIRouter, Depends, Query
|
|
from sqlalchemy.orm import Session
|
|
from sqlalchemy import or_, and_
|
|
from pydantic import BaseModel
|
|
from datetime import datetime
|
|
from typing import List
|
|
|
|
from app.core.database import get_db
|
|
from app.core.security import get_current_user, is_support_staff
|
|
from app.models.notification import Notification
|
|
from app.models.user import User, UserRole
|
|
|
|
router = APIRouter(prefix="/notifications", tags=["notifications"])
|
|
|
|
|
|
class NotificationOut(BaseModel):
|
|
id: int
|
|
type: str
|
|
title: str
|
|
body: str
|
|
is_read: bool
|
|
created_at: datetime
|
|
audience: str
|
|
|
|
class Config:
|
|
from_attributes = True
|
|
|
|
|
|
def _visible_filter(me: User):
|
|
"""SQLAlchemy filter expression for notifications visible to this user.
|
|
|
|
audience values:
|
|
'admin' — system admins only
|
|
'support' — admins + users with can_support role
|
|
'all' — every logged-in user
|
|
'user' — specific user via target_user_id
|
|
"""
|
|
conditions = [
|
|
Notification.audience == "all",
|
|
and_(Notification.audience == "user", Notification.target_user_id == me.id),
|
|
]
|
|
if me.role == UserRole.ADMIN:
|
|
conditions += [
|
|
Notification.audience == "admin",
|
|
Notification.audience == "support",
|
|
]
|
|
elif is_support_staff(me):
|
|
# support staff (can_support role) sees support-channel notifications
|
|
conditions.append(Notification.audience == "support")
|
|
return or_(*conditions)
|
|
|
|
|
|
@router.get("", response_model=List[NotificationOut])
|
|
def list_notifications(
|
|
limit: int = Query(50, le=100),
|
|
db: Session = Depends(get_db),
|
|
me: User = Depends(get_current_user),
|
|
):
|
|
return (
|
|
db.query(Notification)
|
|
.filter(_visible_filter(me))
|
|
.order_by(Notification.id.desc())
|
|
.limit(limit)
|
|
.all()
|
|
)
|
|
|
|
|
|
@router.get("/unread-count")
|
|
def unread_count(db: Session = Depends(get_db), me: User = Depends(get_current_user)):
|
|
count = (
|
|
db.query(Notification)
|
|
.filter(_visible_filter(me), Notification.is_read == False)
|
|
.count()
|
|
)
|
|
return {"count": count}
|
|
|
|
|
|
@router.post("/read-all")
|
|
def mark_all_read(db: Session = Depends(get_db), me: User = Depends(get_current_user)):
|
|
(
|
|
db.query(Notification)
|
|
.filter(_visible_filter(me), Notification.is_read == False)
|
|
.update({"is_read": True}, synchronize_session=False)
|
|
)
|
|
db.commit()
|
|
return {"ok": True}
|
|
|
|
|
|
@router.post("/{notification_id}/read")
|
|
def mark_one_read(
|
|
notification_id: int,
|
|
db: Session = Depends(get_db),
|
|
me: User = Depends(get_current_user),
|
|
):
|
|
n = db.query(Notification).filter(
|
|
Notification.id == notification_id,
|
|
_visible_filter(me),
|
|
).first()
|
|
if n:
|
|
n.is_read = True
|
|
db.commit()
|
|
return {"ok": True}
|