This commit is contained in:
chenyang
2022-07-01 21:39:55 +08:00
parent 19e477a9ca
commit 0a244b34cc
5 changed files with 29 additions and 9 deletions
+19 -5
View File
@@ -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': '用户未登录'})