result.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # coding=utf-8
  2. """
  3. @project: MaxKB
  4. @Author:虎虎
  5. @file: result.py
  6. @date:2025/4/14 15:18
  7. @desc:
  8. """
  9. from typing import List
  10. from django.http import JsonResponse
  11. from django.utils.translation import gettext_lazy as _
  12. from rest_framework import status
  13. class Page(dict):
  14. """
  15. 分页对象
  16. """
  17. def __init__(self, total: int, records: List, current_page: int, page_size: int, **kwargs):
  18. super().__init__(**{'total': total, 'records': records, 'current': current_page, 'size': page_size})
  19. class Result(JsonResponse):
  20. charset = 'utf-8'
  21. """
  22. 接口统一返回对象
  23. """
  24. def __init__(self, code=200, message=_('Success'), data=None, response_status=status.HTTP_200_OK, **kwargs):
  25. back_info_dict = {"code": code, "message": message, 'data': data}
  26. super().__init__(data=back_info_dict, status=response_status, **kwargs)
  27. def success(data, **kwargs):
  28. """
  29. 获取一个成功的响应对象
  30. :param data: 接口响应数据
  31. :return: 请求响应对象
  32. """
  33. return Result(data=data, **kwargs)
  34. def error(message, **kwargs):
  35. """
  36. 获取一个失败的响应对象
  37. :param message: 错误提示
  38. :return: 接口响应对象
  39. """
  40. return Result(code=500, message=message, **kwargs)