refactor: 优化API路由和响应模型 feat(admin): 添加App用户管理接口 feat(sms): 实现阿里云短信服务集成 feat(email): 添加SMTP邮件发送功能 feat(upload): 支持文件上传接口 feat(rate-limiter): 实现手机号限流器 fix: 修复计算步骤入库问题 docs: 更新API文档和测试计划 chore: 更新依赖和配置
78 lines
2.1 KiB
Python
78 lines
2.1 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
|
|
|
|
|
|
class Success(JSONResponse):
|
|
def __init__(
|
|
self,
|
|
code: int = 200,
|
|
msg: Optional[str] = "OK",
|
|
data: Optional[Any] = None,
|
|
**kwargs,
|
|
):
|
|
content = {"code": code, "msg": msg, "data": data}
|
|
content.update(kwargs)
|
|
super().__init__(content=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": data}
|
|
content.update(kwargs)
|
|
super().__init__(content=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=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="提示信息")
|