Merge branch 'refactor/backend-architecture' into development
This commit is contained in:
101
api/app/features/user/router.py
Normal file
101
api/app/features/user/router.py
Normal file
@@ -0,0 +1,101 @@
|
||||
from app.core.logging import get_logger
|
||||
import uuid
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
|
||||
from app.core.config import settings
|
||||
from app.core.dependencies import get_current_user
|
||||
from app.features.user.deps import get_user_service
|
||||
from app.features.user.models import User
|
||||
from app.features.user.schemas import (
|
||||
FeedbackResponse,
|
||||
SubmitFeedbackRequest,
|
||||
TestSubscriptionRequest,
|
||||
TestSubscriptionResponse,
|
||||
UpdateUserProfileRequest,
|
||||
UserProfileResponse,
|
||||
)
|
||||
from app.features.user.service import UserService
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
_SHARED_RESPONSES = {
|
||||
401: {"description": "认证失败"},
|
||||
403: {"description": "权限不足"},
|
||||
404: {"description": "资源不存在"},
|
||||
}
|
||||
|
||||
router = APIRouter(
|
||||
prefix="/api/user",
|
||||
tags=["user"],
|
||||
responses=_SHARED_RESPONSES,
|
||||
)
|
||||
|
||||
feedback_router = APIRouter(
|
||||
prefix="/api/feedback",
|
||||
tags=["feedback"],
|
||||
responses=_SHARED_RESPONSES,
|
||||
)
|
||||
|
||||
|
||||
@router.get("/profile", response_model=UserProfileResponse)
|
||||
async def get_user_profile(
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
return UserProfileResponse(
|
||||
id=current_user.id,
|
||||
phone=current_user.phone,
|
||||
email=current_user.email,
|
||||
nickname=current_user.nickname,
|
||||
avatar_url=current_user.avatar_url,
|
||||
subscription_type=current_user.subscription_type,
|
||||
created_at=current_user.created_at.isoformat(),
|
||||
birth_year=current_user.birth_year,
|
||||
birth_place=current_user.birth_place,
|
||||
grew_up_place=current_user.grew_up_place,
|
||||
occupation=current_user.occupation,
|
||||
)
|
||||
|
||||
|
||||
@router.put("/profile", response_model=UserProfileResponse)
|
||||
async def update_user_profile(
|
||||
body: UpdateUserProfileRequest,
|
||||
current_user: User = Depends(get_current_user),
|
||||
service: UserService = Depends(get_user_service),
|
||||
):
|
||||
return await service.update_profile(current_user.id, body)
|
||||
|
||||
|
||||
@router.post("/test-subscription", response_model=TestSubscriptionResponse)
|
||||
async def test_subscription(
|
||||
body: TestSubscriptionRequest,
|
||||
current_user: User = Depends(get_current_user),
|
||||
service: UserService = Depends(get_user_service),
|
||||
):
|
||||
if not settings.enable_test_subscription:
|
||||
raise HTTPException(status_code=404, detail="测试订阅功能未开放")
|
||||
if body.action == "activate" and body.plan_id not in ("pro", "pro_plus"):
|
||||
raise HTTPException(status_code=400, detail="plan_id 仅支持 pro 或 pro_plus")
|
||||
return await service.toggle_test_subscription(
|
||||
current_user.id, body.action, body.plan_id
|
||||
)
|
||||
|
||||
|
||||
@feedback_router.post("", response_model=FeedbackResponse, status_code=status.HTTP_201_CREATED)
|
||||
async def submit_feedback(
|
||||
request: SubmitFeedbackRequest,
|
||||
current_user: User = Depends(get_current_user),
|
||||
):
|
||||
"""提交用户反馈。用户可通过此接口提交反馈意见或联系客服。"""
|
||||
feedback_id = str(uuid.uuid4())
|
||||
logger.info(
|
||||
"用户反馈 - ID: %s, 用户ID: %s, 内容: %s..., 联系方式: %s",
|
||||
feedback_id,
|
||||
current_user.id,
|
||||
request.content[:100] if len(request.content) > 100 else request.content,
|
||||
request.contact or "未提供",
|
||||
)
|
||||
return FeedbackResponse(
|
||||
id=feedback_id,
|
||||
message="反馈已提交,我们会尽快处理",
|
||||
)
|
||||
Reference in New Issue
Block a user