This commit is contained in:
chenyang
2022-07-01 18:12:01 +08:00
parent 132432c7d5
commit 1b621f7bc4
4 changed files with 107 additions and 16 deletions
+26 -1
View File
@@ -1,7 +1,8 @@
from django.contrib.auth.backends import ModelBackend # 验证基类
from server_auth.models import MindUser
from rest_framework.views import APIView
from django.contrib.auth import authenticate, login, logout
from server.util.mix_util import build_response
class AuthBackend(ModelBackend):
@@ -15,6 +16,7 @@ class AuthBackend(ModelBackend):
if user is not None and user.check_password(password):
return user
# 登录方式有多种
# 1、用户名密码登录:直接匹配数据库的用户名密码
@@ -23,3 +25,26 @@ class AuthBackend(ModelBackend):
# 3、手机号短信验证码登录 1、发送短信验证码 2、用户界面向服务器提交手机号和验证码 3、服务器请求短服务商 4、服务器校验是否有注册,如果没有注册则新增用户
class LoginView(APIView):
"""
"""
def post(self, request):
username = request.POST.get('username')
password = request.POST.get('password')
# django内置认证来校验登录用户
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
# 记住登录状态
login(request, user)
return build_response(code=0, msg='登录成功')
else:
return build_response(code=-1, msg='登录失败')
else:
return build_response(code=-1, msg='登录失败')