diff --git a/server/util/constant_util.py b/server/util/constant_util.py new file mode 100644 index 0000000..505ee18 --- /dev/null +++ b/server/util/constant_util.py @@ -0,0 +1,2 @@ +CODE_SUCCESS = 0 +CODE_FAILED = -1 diff --git a/server/util/mix_util.py b/server/util/mix_util.py index 0a01039..aa7999a 100644 --- a/server/util/mix_util.py +++ b/server/util/mix_util.py @@ -1,5 +1,6 @@ from rest_framework.response import Response +from server.util.constant_util import * -def build_response(code=0, msg='', data=''): +def build_response(code=CODE_SUCCESS, msg='', data=''): return Response({'code': code, 'msg': msg, 'data': data}) diff --git a/server/views/mind_map.py b/server/views/mind_map.py index fcbf067..98c3afb 100644 --- a/server/views/mind_map.py +++ b/server/views/mind_map.py @@ -9,9 +9,10 @@ from server.filters import CooperationMindFilter from server.serializers import * from server.util.mix_util import build_response from server.util.pagination import NewPagination +from server_auth.lib.auth import LoginRequiredJSONMixin -class JsMindView(ListAPIView): +class JsMindView(LoginRequiredJSONMixin, ListAPIView): """ 思维导图view """ @@ -21,7 +22,6 @@ class JsMindView(ListAPIView): filter_class = CooperationMindFilter pagination_class = NewPagination - @login_required(login_url="dashboard") def get(self, request, *args, **kwargs): """ 查询思维导图 @@ -34,7 +34,6 @@ class JsMindView(ListAPIView): return build_response(data=serializer.data) return self.list(request, *args, **kwargs) - @login_required(login_url="dashboard") def post(self, request): """ 创建思维导图 diff --git a/server_auth/lib/auth.py b/server_auth/lib/auth.py index c7378d8..039be0f 100644 --- a/server_auth/lib/auth.py +++ b/server_auth/lib/auth.py @@ -1,8 +1,12 @@ +from django import http +from django.contrib.auth import authenticate, login from django.contrib.auth.backends import ModelBackend # 验证基类 -from server_auth.models import MindUser +from django.contrib.auth.mixins import LoginRequiredMixin from rest_framework.views import APIView -from django.contrib.auth import authenticate, login, logout + +from server.util.constant_util import * from server.util.mix_util import build_response +from server_auth.models import MindUser class AuthBackend(ModelBackend): @@ -43,8 +47,18 @@ class LoginView(APIView): if user.is_active: # 记住登录状态 login(request, user) - return build_response(code=0, msg='登录成功') + return build_response(msg='登录成功') else: - return build_response(code=-1, msg='登录失败') + return build_response(code=CODE_FAILED, msg='登录失败') else: - return build_response(code=-1, msg='登录失败') + return build_response(code=CODE_FAILED, msg='登录失败') + + +class LoginRequiredJSONMixin(LoginRequiredMixin): + """自定义LoginRequiredMixin + 如果用户未登录,响应JSON,且状态码为400 + """ + + def handle_no_permission(self): + # return build_response(code=CODE_FAILED, msg='用户未登录!') + return http.JsonResponse({'code': CODE_FAILED, 'msg': '用户未登录'}) diff --git a/static/html/list.html b/static/html/list.html index 8937579..e1dc8bd 100644 --- a/static/html/list.html +++ b/static/html/list.html @@ -58,6 +58,10 @@ get_param.coop_mind_name = this.get_search_coop_mind_name() } $.get('list', get_param, function (resp) { + if (resp.code !== 0) { + layer.msg(resp.msg) + return + } // 清空数据 $('#layui-table tbody').empty() $('#' + me.opts.page_bar_elem).empty()