2026-03-16 11:39:39 +08:00
|
|
|
# 用户管理路由
|
|
|
|
|
from typing import Optional
|
2026-03-18 14:55:02 +08:00
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status, Query, Body
|
2026-03-16 11:39:39 +08:00
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
|
from app.core.database import get_db
|
|
|
|
|
from app.core.auth import get_current_user
|
|
|
|
|
from app.models.models import User, Collection
|
2026-03-18 13:32:52 +08:00
|
|
|
from app.schemas.schemas import UserResponse, UserUpdate
|
2026-03-16 11:39:39 +08:00
|
|
|
|
|
|
|
|
router = APIRouter(prefix="/api", tags=["用户"])
|
|
|
|
|
|
|
|
|
|
# ============ 当前用户接口 ============
|
|
|
|
|
|
|
|
|
|
@router.get("/users/me", response_model=UserResponse)
|
|
|
|
|
def get_current_user_info(
|
|
|
|
|
current_user: User = Depends(get_current_user)
|
|
|
|
|
):
|
|
|
|
|
"""获取当前登录用户信息"""
|
|
|
|
|
return {
|
|
|
|
|
"f99_90_id": current_user.f99_90_id,
|
|
|
|
|
"f01_01_name": current_user.f01_01_name,
|
|
|
|
|
"email": current_user.email,
|
|
|
|
|
"phone": current_user.phone,
|
|
|
|
|
"avatar": current_user.avatar,
|
|
|
|
|
"address": current_user.address,
|
|
|
|
|
"bio": current_user.bio,
|
|
|
|
|
"role": current_user.role,
|
|
|
|
|
"f99_92_created_at": current_user.f99_92_created_at.isoformat() if current_user.f99_92_created_at else None,
|
|
|
|
|
"f99_93_updated_at": current_user.f99_93_updated_at.isoformat() if current_user.f99_93_updated_at else None
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-18 13:32:52 +08:00
|
|
|
@router.put("/users/me", response_model=UserResponse)
|
2026-03-16 11:39:39 +08:00
|
|
|
def update_current_user(
|
2026-03-18 13:32:52 +08:00
|
|
|
user_update: UserUpdate,
|
2026-03-16 11:39:39 +08:00
|
|
|
current_user: User = Depends(get_current_user),
|
|
|
|
|
db: Session = Depends(get_db)
|
|
|
|
|
):
|
|
|
|
|
"""更新当前用户信息"""
|
2026-03-18 13:32:52 +08:00
|
|
|
import logging
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
# 获取用户ID
|
|
|
|
|
user_id = current_user.f99_90_id
|
|
|
|
|
logger.info(f"Updating user {user_id}, data={user_update.model_dump()}")
|
|
|
|
|
|
|
|
|
|
# 在当前session中重新查询用户
|
|
|
|
|
user = db.query(User).filter(User.f99_90_id == user_id).first()
|
2026-03-18 13:10:45 +08:00
|
|
|
if not user:
|
|
|
|
|
raise HTTPException(status_code=404, detail="用户不存在")
|
|
|
|
|
|
2026-03-16 11:39:39 +08:00
|
|
|
# 更新字段
|
2026-03-18 13:32:52 +08:00
|
|
|
update_data = user_update.model_dump(exclude_unset=True)
|
|
|
|
|
for field, value in update_data.items():
|
|
|
|
|
if field == 'f01_01_name':
|
|
|
|
|
user.f01_01_name = value
|
|
|
|
|
elif field == 'username':
|
|
|
|
|
pass # skip, already handled as f01_01_name
|
|
|
|
|
elif hasattr(user, field):
|
|
|
|
|
setattr(user, field, value)
|
2026-03-16 11:39:39 +08:00
|
|
|
|
2026-03-18 13:32:52 +08:00
|
|
|
# 强制刷新以确保更新被提交
|
|
|
|
|
db.flush()
|
2026-03-16 11:39:39 +08:00
|
|
|
db.commit()
|
2026-03-18 13:10:45 +08:00
|
|
|
db.refresh(user)
|
2026-03-18 13:32:52 +08:00
|
|
|
logger.info(f"After commit, user email={user.email}")
|
2026-03-16 11:39:39 +08:00
|
|
|
|
2026-03-18 13:32:52 +08:00
|
|
|
return user
|
2026-03-16 11:39:39 +08:00
|
|
|
|
|
|
|
|
# ============ 管理员用户管理 ============
|
|
|
|
|
|
|
|
|
|
admin_router = APIRouter(prefix="/api/admin/users", tags=["用户管理"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@admin_router.get("")
|
|
|
|
|
def get_users(
|
|
|
|
|
page: int = Query(1, ge=1),
|
|
|
|
|
limit: int = Query(20, ge=1, le=100),
|
|
|
|
|
current_user: User = Depends(get_current_user),
|
|
|
|
|
db: Session = Depends(get_db)
|
|
|
|
|
):
|
|
|
|
|
"""获取用户列表(仅管理员)"""
|
|
|
|
|
if current_user.role != "admin":
|
|
|
|
|
raise HTTPException(status_code=403, detail="无权访问")
|
|
|
|
|
|
|
|
|
|
total = db.query(User).count()
|
2026-03-23 10:56:23 +08:00
|
|
|
from sqlalchemy import case
|
|
|
|
|
users = db.query(User).order_by(
|
|
|
|
|
case(
|
|
|
|
|
(User.user_code == None, 1),
|
|
|
|
|
else_=0
|
|
|
|
|
),
|
|
|
|
|
User.user_code.asc()
|
|
|
|
|
).offset((page-1)*limit).limit(limit).all()
|
2026-03-16 11:39:39 +08:00
|
|
|
|
|
|
|
|
user_list = []
|
|
|
|
|
for u in users:
|
|
|
|
|
# 统计每个用户的藏品数量
|
|
|
|
|
count = db.query(Collection).filter(Collection.f99_91_user_id == u.f99_90_id).count()
|
|
|
|
|
user_list.append({
|
|
|
|
|
"id": u.f99_90_id,
|
|
|
|
|
"username": u.f01_01_name,
|
|
|
|
|
"email": u.email,
|
|
|
|
|
"phone": u.phone,
|
|
|
|
|
"role": u.role,
|
2026-03-23 10:56:23 +08:00
|
|
|
"user_code": u.user_code,
|
2026-03-16 11:39:39 +08:00
|
|
|
"created_at": u.f99_92_created_at.isoformat() if u.f99_92_created_at else None,
|
|
|
|
|
"collection_count": count
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return user_list
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@admin_router.get("/{user_id}")
|
|
|
|
|
def get_user(
|
|
|
|
|
user_id: str,
|
|
|
|
|
current_user: User = Depends(get_current_user),
|
|
|
|
|
db: Session = Depends(get_db)
|
|
|
|
|
):
|
|
|
|
|
"""获取单个用户信息"""
|
|
|
|
|
if current_user.role != "admin":
|
|
|
|
|
raise HTTPException(status_code=403, detail="无权访问")
|
|
|
|
|
|
|
|
|
|
user = db.query(User).filter(User.id == user_id).first()
|
|
|
|
|
if not user:
|
|
|
|
|
raise HTTPException(status_code=404, detail="用户不存在")
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
"id": user.id,
|
|
|
|
|
"username": user.username,
|
|
|
|
|
"email": user.email,
|
|
|
|
|
"phone": user.phone,
|
|
|
|
|
"role": user.role,
|
|
|
|
|
"created_at": user.created_at.isoformat() if user.created_at else None
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@admin_router.get("/{user_id}/collections")
|
|
|
|
|
def get_user_collections(
|
|
|
|
|
user_id: str,
|
|
|
|
|
limit: int = Query(100, ge=1, le=100),
|
|
|
|
|
current_user: User = Depends(get_current_user),
|
|
|
|
|
db: Session = Depends(get_db)
|
|
|
|
|
):
|
|
|
|
|
"""获取指定用户的藏品列表"""
|
|
|
|
|
if current_user.role != "admin":
|
|
|
|
|
raise HTTPException(status_code=403, detail="无权访问")
|
|
|
|
|
|
|
|
|
|
collections = db.query(Collection).filter(
|
|
|
|
|
Collection.user_id == user_id
|
|
|
|
|
).limit(limit).all()
|
|
|
|
|
|
|
|
|
|
return [c.code for c in collections]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@admin_router.get("/{user_id}/count")
|
|
|
|
|
def get_user_collection_count(
|
|
|
|
|
user_id: str,
|
|
|
|
|
current_user: User = Depends(get_current_user),
|
|
|
|
|
db: Session = Depends(get_db)
|
|
|
|
|
):
|
|
|
|
|
"""获取指定用户的藏品数量"""
|
|
|
|
|
if current_user.role != "admin":
|
|
|
|
|
raise HTTPException(status_code=403, detail="E00050: 仅管理员可访问")
|
|
|
|
|
|
|
|
|
|
count = db.query(Collection).filter(Collection.user_id == user_id).count()
|
|
|
|
|
return {"count": count}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@admin_router.put("/{user_id}")
|
|
|
|
|
def update_user(
|
|
|
|
|
user_id: str,
|
2026-03-18 14:55:02 +08:00
|
|
|
username: Optional[str] = Body(None),
|
|
|
|
|
email: Optional[str] = Body(None),
|
|
|
|
|
role: Optional[str] = Body(None),
|
|
|
|
|
password: Optional[str] = Body(None),
|
2026-03-23 10:56:23 +08:00
|
|
|
user_code: Optional[str] = Body(None),
|
2026-03-16 11:39:39 +08:00
|
|
|
current_user: User = Depends(get_current_user),
|
|
|
|
|
db: Session = Depends(get_db)
|
|
|
|
|
):
|
|
|
|
|
"""更新用户信息(仅管理员)"""
|
|
|
|
|
if current_user.role != "admin":
|
|
|
|
|
raise HTTPException(status_code=403, detail="E00050: 仅管理员可访问")
|
|
|
|
|
|
|
|
|
|
user = db.query(User).filter(User.f99_90_id == user_id).first()
|
|
|
|
|
if not user:
|
|
|
|
|
raise HTTPException(status_code=404, detail="E00051: 用户不存在")
|
|
|
|
|
|
|
|
|
|
# 更新基本信息
|
|
|
|
|
if username:
|
|
|
|
|
user.f01_01_name = username
|
|
|
|
|
if email:
|
|
|
|
|
user.email = email
|
2026-03-18 14:55:02 +08:00
|
|
|
if role is not None:
|
2026-03-16 11:39:39 +08:00
|
|
|
user.role = role
|
|
|
|
|
|
2026-03-23 10:56:23 +08:00
|
|
|
# 更新用户编码
|
|
|
|
|
if user_code is not None:
|
|
|
|
|
# 只有非空字符串才检查唯一性
|
|
|
|
|
user_code_str = user_code.strip() if user_code else ''
|
|
|
|
|
if user_code_str:
|
|
|
|
|
existing = db.query(User).filter(
|
|
|
|
|
User.user_code == user_code_str,
|
|
|
|
|
User.f99_90_id != user_id
|
|
|
|
|
).first()
|
|
|
|
|
if existing:
|
|
|
|
|
raise HTTPException(status_code=400, detail="E00052: 该用户编码已被其他用户使用")
|
|
|
|
|
user.user_code = user_code_str
|
|
|
|
|
else:
|
|
|
|
|
user.user_code = None
|
|
|
|
|
|
2026-03-16 11:39:39 +08:00
|
|
|
# 更新密码
|
|
|
|
|
if password and password.strip():
|
|
|
|
|
from app.core.auth import get_password_hash
|
|
|
|
|
user.password = get_password_hash(password)
|
|
|
|
|
|
|
|
|
|
db.commit()
|
|
|
|
|
db.refresh(user)
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
"id": user.f99_90_id,
|
|
|
|
|
"username": user.f01_01_name,
|
|
|
|
|
"email": user.email,
|
|
|
|
|
"role": user.role,
|
|
|
|
|
"message": "更新成功"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@admin_router.delete("/{user_id}")
|
|
|
|
|
def delete_user(
|
|
|
|
|
user_id: str,
|
|
|
|
|
current_user: User = Depends(get_current_user),
|
|
|
|
|
db: Session = Depends(get_db)
|
|
|
|
|
):
|
|
|
|
|
"""删除用户(仅管理员)"""
|
|
|
|
|
if current_user.role != "admin":
|
|
|
|
|
raise HTTPException(status_code=403, detail="E00050: 仅管理员可访问")
|
|
|
|
|
|
|
|
|
|
# 不能删除自己
|
|
|
|
|
if user_id == str(current_user.f99_90_id):
|
|
|
|
|
raise HTTPException(status_code=400, detail="E00052: 不能删除自己")
|
|
|
|
|
|
|
|
|
|
user = db.query(User).filter(User.f99_90_id == user_id).first()
|
|
|
|
|
if not user:
|
|
|
|
|
raise HTTPException(status_code=404, detail="E00051: 用户不存在")
|
|
|
|
|
|
|
|
|
|
db.delete(user)
|
|
|
|
|
db.commit()
|
|
|
|
|
|
|
|
|
|
return {"message": "删除成功"}
|