diff --git a/server/urls.py b/server/urls.py index acd7d87..ce452ac 100644 --- a/server/urls.py +++ b/server/urls.py @@ -3,6 +3,7 @@ from django.urls import path from server.views.mind_map import * from server.views.user import * from server.view_test import * +from server_auth.lib.auth import * urlpatterns = [ # --------------------------------------------------- @@ -21,5 +22,6 @@ urlpatterns = [ # --------------------------------------------------- path('register', MindUserView.as_view()), # 用户注册 + path('login', LoginView.as_view()), # 用户登录 ] diff --git a/server/views/user.py b/server/views/user.py index c93c4f0..775a546 100644 --- a/server/views/user.py +++ b/server/views/user.py @@ -1,6 +1,7 @@ from django.http.response import JsonResponse from rest_framework.views import APIView +from server.util.mix_util import build_response from server_auth.models import MindUser @@ -14,5 +15,11 @@ class MindUserView(APIView): user_name = data_dict.get('userName') input_password = data_dict.get('inputPassword') print('userName:%s,inputPassword:%s' % (user_name, input_password)) + + # 根据用户名查询 MindUser + db_user = MindUser.objects.filter(username=user_name).first() + if db_user is not None: + return build_response(code=-1, msg='用户名:%s 已经存在,请更换!' % user_name) + MindUser.objects.create_user(username=user_name, password=input_password, email='') - return JsonResponse({"code": 0, "msg": "success"}) + return JsonResponse({"code": 0, "msg": "注册成功,跳转登录界面中"}) diff --git a/server_auth/lib/auth.py b/server_auth/lib/auth.py index 880928d..c7378d8 100644 --- a/server_auth/lib/auth.py +++ b/server_auth/lib/auth.py @@ -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='登录失败') diff --git a/static/html/dashboard.html b/static/html/dashboard.html index a8c88ab..03e3b16 100644 --- a/static/html/dashboard.html +++ b/static/html/dashboard.html @@ -59,12 +59,12 @@ set_modal_title('登录'); }) - // 设置窗口名称 + /** 设置窗口名称 */ function set_modal_title(title) { $('#exampleModal .modal-title').html(title); } - // 注册 + /** 注册按钮 */ $('#register_button').click(function () { const userName = $('#userName').val(); const inputPassword = $('#inputPassword').val(); @@ -91,16 +91,30 @@ success: function (data) { // 注册成功 if (data.code === 0) { - + clear_reg_info() + reg_alert(data.msg, 'alert-primary') + setTimeout(function () { + $('#p_login_a').click() + }, 1500) + } else { + // 注册失败 + reg_alert(data.msg) } }, async: false }); } - // - + // validate end }); + /** 清除注册时输入内容 */ + function clear_reg_info() { + $('#userName').val(''); + $('#inputPassword').val(''); + $('#inputPassword_').val(''); + } + + /**校验密码是否输入一致*/ function validate_pw_eq(obj, inputPassword, inputPassword_) { if (inputPassword !== inputPassword_) { reg_alert('两次密码输入不一样,请确认!') @@ -109,22 +123,60 @@ return true } - function reg_alert(text) { + /** 注册提示 */ + function reg_alert(text, class_str) { + common_alert('register_button', text, class_str) + } + + /** 登录提示 */ + function login_alert(text, class_str) { + common_alert('login_button', text, class_str) + } + + function common_alert(button_id, text, class_str) { // 清除之前的弹窗 $('.alert').alert('close') // const uuid = '' + (new Date().getTime().toString(16) + Math.random().toString(16).substr(2)).substr(2, 16) + const class_name = class_str ? class_str : 'alert-danger' + const alert_component = ` -