Files
LoveACE-EndF/loveace/router/schemas/uniresponse.py
Sibuxiangx bbc86b8330 ⚒️ 重大重构 LoveACE V2
引入了 mongodb
对数据库进行了一定程度的数据加密
性能改善
代码简化
统一错误模型和响应
使用 apifox 作为文档
2025-11-20 20:44:25 +08:00

46 lines
1.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import time
from typing import Generic, TypeVar, Union
from pydantic import BaseModel, Field
from loveace.router.schemas.model import ErrorModel, ValidationErrorModel
T = TypeVar("T")
class UniResponseModel(BaseModel, Generic[T]):
"""
统一响应模型适用于所有API响应。
Attributes:
success (bool): 操作是否成功。
message (str | None): 操作的详细信息。
data (ResponseModel | None): 操作返回的数据。
error (DetailModel | None): 操作错误信息,支持 ErrorModel 或 ValidationErrorDetail。
timestamp (str): 响应生成的时间戳,格式为 "YYYY-MM-DD HH:MM:SS"
"""
success: bool = Field(..., description="操作是否成功")
message: str | None = Field(..., description="操作的详细信息")
data: T | None = Field(..., description="操作返回的数据")
error: Union[ErrorModel, ValidationErrorModel] | None = Field(
None, description="操作错误信息"
)
timestamp: str = Field(
default_factory=lambda: time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
description="响应生成的时间戳",
)
@classmethod
def from_response(
cls,
success: bool,
message: str,
data: T | None = None,
) -> "UniResponseModel":
return cls(
success=success,
message=message,
data=data,
error=None,
)