Files
smart-project-pricing/后端服务/app/schemas/common.py
2026-01-31 21:33:06 +08:00

61 lines
1.1 KiB
Python

"""通用响应模型
遵循瑞小美 API 响应格式规范
"""
from typing import Generic, TypeVar, Optional, List
from pydantic import BaseModel
T = TypeVar("T")
class ResponseModel(BaseModel, Generic[T]):
"""统一响应格式"""
code: int = 0
message: str = "success"
data: Optional[T] = None
class PaginatedData(BaseModel, Generic[T]):
"""分页数据"""
items: List[T]
total: int
page: int
page_size: int
total_pages: int
class PaginatedResponse(BaseModel, Generic[T]):
"""分页响应"""
code: int = 0
message: str = "success"
data: Optional[PaginatedData[T]] = None
class ErrorResponse(BaseModel):
"""错误响应"""
code: int
message: str
data: None = None
# 错误码定义
class ErrorCode:
SUCCESS = 0
PARAM_ERROR = 10001
NOT_FOUND = 10002
ALREADY_EXISTS = 10003
NOT_ALLOWED = 10004
AUTH_FAILED = 20001
PERMISSION_DENIED = 20002
TOKEN_EXPIRED = 20003
INTERNAL_ERROR = 30001
SERVICE_UNAVAILABLE = 30002
AI_SERVICE_ERROR = 40001
AI_SERVICE_TIMEOUT = 40002