535d97a70c
17-table PostgreSQL schema with full Chinese column comments, FastAPI project structure (admin/common/modules), DSL rule files, and schema migration scripts.
58 lines
1.2 KiB
Python
58 lines
1.2 KiB
Python
"""统一响应格式。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
from enum import Enum
|
|
from typing import Any, Generic, TypeVar
|
|
|
|
T = TypeVar("T")
|
|
|
|
|
|
class StatusCodeEnum(Enum):
|
|
"""HTTP 状态码枚举。"""
|
|
|
|
HTTP_200_OK = 200
|
|
HTTP_201_CREATED = 201
|
|
HTTP_400_BAD_REQUEST = 400
|
|
HTTP_401_UNAUTHORIZED = 401
|
|
HTTP_403_FORBIDDEN = 403
|
|
HTTP_404_NOT_FOUND = 404
|
|
HTTP_409_CONFLICT = 409
|
|
HTTP_422_UNPROCESSABLE_ENTITY = 422
|
|
HTTP_500_INTERNAL_SERVER_ERROR = 500
|
|
|
|
|
|
@dataclass
|
|
class PaginationInfo:
|
|
"""分页信息。"""
|
|
|
|
total: int
|
|
page: int
|
|
pageSize: int
|
|
|
|
|
|
@dataclass
|
|
class PageResult(Generic[T]):
|
|
"""分页结果。"""
|
|
|
|
list: list[T]
|
|
pagination: PaginationInfo
|
|
|
|
|
|
@dataclass
|
|
class Result(Generic[T]):
|
|
"""统一响应。"""
|
|
|
|
code: int
|
|
message: str
|
|
data: T | None = None
|
|
|
|
@classmethod
|
|
def success(cls, data: T | None = None, message: str = "ok") -> "Result[T]":
|
|
return cls(code=200, message=message, data=data)
|
|
|
|
@classmethod
|
|
def error(cls, status: StatusCodeEnum, message: str | None = None) -> "Result[None]":
|
|
return cls(code=status.value, message=message or status.name, data=None)
|