refactor: 优化响应格式和错误处理 fix: 修复文件上传类型校验和删除无用PDF文件 perf: 添加估值评估审核时间字段和查询条件 docs: 更新Docker镜像版本至v1.8 test: 添加响应格式检查脚本 style: 统一API响应数据结构 chore: 清理无用静态文件和更新构建脚本
79 lines
2.3 KiB
Python
79 lines
2.3 KiB
Python
from typing import Any, Optional, Generic, TypeVar, List
|
|
from pydantic import BaseModel, Field
|
|
from pydantic.generics import GenericModel
|
|
|
|
from fastapi.responses import JSONResponse
|
|
from fastapi.encoders import jsonable_encoder
|
|
|
|
|
|
class Success(JSONResponse):
|
|
def __init__(
|
|
self,
|
|
code: int = 200,
|
|
msg: Optional[str] = "OK",
|
|
data: Optional[Any] = None,
|
|
**kwargs,
|
|
):
|
|
content = {"code": code, "msg": msg, "data": ({} if data is None else data)}
|
|
content.update(kwargs)
|
|
super().__init__(content=jsonable_encoder(content), status_code=code)
|
|
|
|
|
|
class Fail(JSONResponse):
|
|
def __init__(
|
|
self,
|
|
code: int = 400,
|
|
msg: Optional[str] = None,
|
|
data: Optional[Any] = None,
|
|
**kwargs,
|
|
):
|
|
content = {"code": code, "msg": msg, "data": ({} if data is None else data)}
|
|
content.update(kwargs)
|
|
super().__init__(content=jsonable_encoder(content), status_code=code)
|
|
|
|
|
|
class SuccessExtra(JSONResponse):
|
|
def __init__(
|
|
self,
|
|
code: int = 200,
|
|
msg: Optional[str] = None,
|
|
data: Optional[Any] = None,
|
|
total: int = 0,
|
|
page: int = 1,
|
|
page_size: int = 20,
|
|
**kwargs,
|
|
):
|
|
content = {
|
|
"code": code,
|
|
"msg": msg,
|
|
"data": data,
|
|
"total": total,
|
|
"page": page,
|
|
"page_size": page_size,
|
|
}
|
|
content.update(kwargs)
|
|
super().__init__(content=jsonable_encoder(content), status_code=code)
|
|
|
|
|
|
T = TypeVar("T")
|
|
|
|
|
|
class BasicResponse(GenericModel, Generic[T]):
|
|
code: int = Field(200, description="状态码")
|
|
msg: Optional[str] = Field("OK", description="信息")
|
|
data: Optional[T] = Field(None, description="数据载荷")
|
|
|
|
|
|
class PageResponse(GenericModel, Generic[T]):
|
|
code: int = Field(200, description="状态码")
|
|
msg: Optional[str] = Field(None, description="信息")
|
|
data: List[T] = Field(default_factory=list, description="数据列表")
|
|
total: int = Field(0, description="总数量")
|
|
page: int = Field(1, description="当前页码")
|
|
page_size: int = Field(20, description="每页数量")
|
|
pages: Optional[int] = Field(None, description="总页数")
|
|
|
|
|
|
class MessageOut(BaseModel):
|
|
message: str = Field(..., description="提示信息")
|