|
@@ -1,49 +0,0 @@
|
|
|
-from django.utils.deprecation import MiddlewareMixin
|
|
|
|
|
-from rest_framework.exceptions import ErrorDetail
|
|
|
|
|
-from django.http import JsonResponse
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-class ResponseMiddleware(MiddlewareMixin):
|
|
|
|
|
- def process_response(self, request, response):
|
|
|
|
|
- """在每个响应返回给客户端之前自动调用"""
|
|
|
|
|
- if request.path.startswith('/equipmanager/'):
|
|
|
|
|
- # if isinstance(response, JsonResponse):
|
|
|
|
|
- # return response
|
|
|
|
|
- status_code = response.status_code
|
|
|
|
|
- rsp_data = response.data
|
|
|
|
|
- if status_code == 200:
|
|
|
|
|
- response = JsonResponse(
|
|
|
|
|
- status=200,
|
|
|
|
|
- data={"code":status_code,"msg":"请求成功","data":rsp_data}
|
|
|
|
|
- )
|
|
|
|
|
- elif status_code == 400:
|
|
|
|
|
- if isinstance(rsp_data, dict):
|
|
|
|
|
- try:
|
|
|
|
|
- for key, value in rsp_data.items():
|
|
|
|
|
- print(key, value, type(value[0]))
|
|
|
|
|
- if value and isinstance(value, list):
|
|
|
|
|
- value = value[0]
|
|
|
|
|
- if isinstance(value, ErrorDetail):
|
|
|
|
|
- value = f"{key} {value}"
|
|
|
|
|
- response = JsonResponse(
|
|
|
|
|
- status=200,
|
|
|
|
|
- data={'msg': str(value),'code': status_code,'data': ""}
|
|
|
|
|
- )
|
|
|
|
|
- break
|
|
|
|
|
- except Exception as e:
|
|
|
|
|
- pass
|
|
|
|
|
- elif status_code == 404:
|
|
|
|
|
- response = JsonResponse(
|
|
|
|
|
- status=200,
|
|
|
|
|
- data={"code":status_code,"msg":"请求接口不存在","data":""}
|
|
|
|
|
- )
|
|
|
|
|
- return response
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- def process_exception(self, request, exception):
|
|
|
|
|
- data = {
|
|
|
|
|
- 'msg': str(exception),
|
|
|
|
|
- 'code': 500,
|
|
|
|
|
- 'data': ""
|
|
|
|
|
- }
|
|
|
|
|
- return JsonResponse(status=500, data=data)
|
|
|