R.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # File : R.py
  4. # Author: DaShenHan&道长-----先苦后甜,任凭晚风拂柳颜------
  5. # Date : 2022/9/6
  6. from flask import jsonify
  7. class copy_utils:
  8. @staticmethod
  9. def obj_to_dic(obj):
  10. '''
  11. 将传入的data对象转成字典
  12. '''
  13. result = {}
  14. for temp in obj.__dict__:
  15. if temp.startswith('_') or temp == 'metadata':
  16. continue
  17. result[temp] = getattr(obj, temp)
  18. return result
  19. @staticmethod
  20. def obj_to_list(list_obj):
  21. '''
  22. 将传入的data对象转成List,list中的元素是字典
  23. '''
  24. result = []
  25. for obj in list_obj:
  26. result.append(copy_utils.obj_to_dic(obj))
  27. return result
  28. class R(object):
  29. @classmethod
  30. def ok(self,msg='操作成功',data=None):
  31. if not data:
  32. data = []
  33. result = {"code": 200, "msg": msg, "data": data,"count":len(data)}
  34. return jsonify(result)
  35. @classmethod
  36. def error(self,msg="系统异常",code=404):
  37. result = {"code": code, "msg": msg}
  38. return jsonify(result)
  39. @classmethod
  40. def success(self,msg='操作成功', data=None):
  41. return self.ok(msg,data)
  42. @classmethod
  43. def failed(self,msg="系统异常", code=404):
  44. return self.error(msg,code)