custom_response.py 592 B

1234567891011121314151617181920212223
  1. from flask import jsonify
  2. class ResponseCode:
  3. SUCCESS = 200
  4. PARAM_WARING = 400
  5. MESSAGE = "success"
  6. def generate_response(data=None, code=ResponseCode.SUCCESS, msg=ResponseCode.MESSAGE, **kwargs):
  7. """
  8. 自定义响应
  9. :param code:状态码
  10. :param data:返回数据
  11. :param msg:返回消息
  12. :param kwargs:
  13. :return:
  14. """
  15. msg = msg or 'success' if code == 200 else msg or 'fail'
  16. success = True if code == 200 else False
  17. res = jsonify(dict(code=code, success=success, data=data, msg=msg, **kwargs))
  18. res.status_code = 200
  19. return res