Files
Django/jituan/services/api_compat.py
T

19 lines
666 B
Python

"""老小程序兼容:统一 JSON 错误格式(勿改前端时靠后端适配)。"""
from rest_framework import status
from rest_framework.response import Response
def legacy_miniapp_error(code, message, http_status=status.HTTP_200_OK):
"""
业务错误返回 HTTP 200 + body.code。
老端 request.js 会把 HTTP 403 当成「请先登录」,导致只显示「注册失败」。
同时写 message 与 msg,兼容 dashouzhuce / wdlyhdl 两种字段名。
"""
text = message or '操作失败'
return Response({
'code': code,
'message': text,
'msg': text,
'data': None,
}, status=http_status)