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
+2
View File
@@ -3,6 +3,7 @@ from django.urls import path
from server.views.mind_map import * from server.views.mind_map import *
from server.views.user import * from server.views.user import *
from server.view_test import * from server.view_test import *
from server_auth.lib.auth import *
urlpatterns = [ urlpatterns = [
# --------------------------------------------------- # ---------------------------------------------------
@@ -21,5 +22,6 @@ urlpatterns = [
# --------------------------------------------------- # ---------------------------------------------------
path('register', MindUserView.as_view()), # 用户注册 path('register', MindUserView.as_view()), # 用户注册
path('login', LoginView.as_view()), # 用户登录
] ]
+8 -1
View File
@@ -1,6 +1,7 @@
from django.http.response import JsonResponse from django.http.response import JsonResponse
from rest_framework.views import APIView from rest_framework.views import APIView
from server.util.mix_util import build_response
from server_auth.models import MindUser from server_auth.models import MindUser
@@ -14,5 +15,11 @@ class MindUserView(APIView):
user_name = data_dict.get('userName') user_name = data_dict.get('userName')
input_password = data_dict.get('inputPassword') input_password = data_dict.get('inputPassword')
print('userName:%s,inputPassword:%s' % (user_name, input_password)) 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='') MindUser.objects.create_user(username=user_name, password=input_password, email='')
return JsonResponse({"code": 0, "msg": "success"}) return JsonResponse({"code": 0, "msg": "注册成功,跳转登录界面中"})
+26 -1
View File
@@ -1,7 +1,8 @@
from django.contrib.auth.backends import ModelBackend # 验证基类 from django.contrib.auth.backends import ModelBackend # 验证基类
from server_auth.models import MindUser from server_auth.models import MindUser
from rest_framework.views import APIView
from django.contrib.auth import authenticate, login, logout from django.contrib.auth import authenticate, login, logout
from server.util.mix_util import build_response
class AuthBackend(ModelBackend): class AuthBackend(ModelBackend):
@@ -15,6 +16,7 @@ class AuthBackend(ModelBackend):
if user is not None and user.check_password(password): if user is not None and user.check_password(password):
return user return user
# 登录方式有多种 # 登录方式有多种
# 1、用户名密码登录:直接匹配数据库的用户名密码 # 1、用户名密码登录:直接匹配数据库的用户名密码
@@ -23,3 +25,26 @@ class AuthBackend(ModelBackend):
# 3、手机号短信验证码登录 1、发送短信验证码 2、用户界面向服务器提交手机号和验证码 3、服务器请求短服务商 4、服务器校验是否有注册,如果没有注册则新增用户 # 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='登录失败')
+71 -14
View File
@@ -59,12 +59,12 @@
set_modal_title('登录'); set_modal_title('登录');
}) })
// 设置窗口名称 /** 设置窗口名称 */
function set_modal_title(title) { function set_modal_title(title) {
$('#exampleModal .modal-title').html(title); $('#exampleModal .modal-title').html(title);
} }
// 注册 /** 注册按钮 */
$('#register_button').click(function () { $('#register_button').click(function () {
const userName = $('#userName').val(); const userName = $('#userName').val();
const inputPassword = $('#inputPassword').val(); const inputPassword = $('#inputPassword').val();
@@ -91,16 +91,30 @@
success: function (data) { success: function (data) {
// 注册成功 // 注册成功
if (data.code === 0) { 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 async: false
}); });
} }
// // validate end
}); });
/** 清除注册时输入内容 */
function clear_reg_info() {
$('#userName').val('');
$('#inputPassword').val('');
$('#inputPassword_').val('');
}
/**校验密码是否输入一致*/
function validate_pw_eq(obj, inputPassword, inputPassword_) { function validate_pw_eq(obj, inputPassword, inputPassword_) {
if (inputPassword !== inputPassword_) { if (inputPassword !== inputPassword_) {
reg_alert('两次密码输入不一样,请确认!') reg_alert('两次密码输入不一样,请确认!')
@@ -109,22 +123,60 @@
return true 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') $('.alert').alert('close')
// //
const uuid = '' + (new Date().getTime().toString(16) + Math.random().toString(16).substr(2)).substr(2, 16) 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 = ` const alert_component = `
<div class="alert alert-danger .alert-dismissible fade show" role="alert" id="${uuid}" style="margin-top: 3px;margin-bottom: 3px;height: 30px;padding-top: 2px;padding-bottom: 2px;"> <div class="alert ${class_name} .alert-dismissible fade show" role="alert" id="${uuid}" style="clear: both;margin-top: 3px;margin-bottom: 3px;height: 30px;padding-top: 2px;padding-bottom: 2px;">
${text} ${text}
</div> </div>
` `
$('#register_button').after(alert_component) $('#' + button_id).parent().after(alert_component)
setTimeout(function () { setTimeout(function () {
$('#' + uuid).fadeOut(1200) $('#' + uuid).fadeOut(1200)
}, 1000) }, 1000)
} }
/**登录按钮*/
$('#login_button').click(function () {
const userName_login = $('#userName_login').val();
const inputPassword_login = $('#inputPassword_login').val();
if (!userName_login || !inputPassword_login) {
login_alert('请输入用户名和密码')
return
}
$.ajax({
type: "post",
url: "login",
data: {username: userName_login, password: inputPassword_login},
dataType: "json",
success: function (data) {
// 注册成功
if (data.code === 0) {
login_alert(data.msg, 'alert-primary')
} else {
// 注册失败
login_alert(data.msg)
}
},
async: false
});
})
// js end // js end
}) })
</script> </script>
@@ -161,19 +213,24 @@
<label for="inputPassword_" class="sr-only"></label> <label for="inputPassword_" class="sr-only"></label>
<input type="password" id="inputPassword_" class="form-control" placeholder="密码" required> <input type="password" id="inputPassword_" class="form-control" placeholder="密码" required>
<button class="btn btn-lg btn-primary btn-block" type="submit" id="register_button">注册</button> <div style="margin-bottom: 10px; text-align:right;">
<button class="btn btn-sm btn-primary" type="submit" id="register_button">注册</button>
</div>
</div> </div>
<!--register end--> <!--register end-->
<!--pw_login start--> <!--pw_login start-->
<div class="modal-body pw_login_group" style="display: none;height: 360px;"> <div class="modal-body pw_login_group" style="display: none;height: 360px;">
<h1 class="h3 mb-3 font-weight-normal">请输入用户名和密码</h1> <h1 class="h3 mb-3 font-weight-normal">请输入用户名和密码</h1>
<label for="userName" class="sr-only">Email address</label> <label for="userName_login" class="sr-only">用户名</label>
<input type="text" id="userName" class="form-control" placeholder="Email address" required autofocus> <input type="text" id="userName_login" name="userName_login" class="form-control" placeholder="用户名" required autofocus>
<label for="inputPassword" class="sr-only">Password</label> <label for="inputPassword_login" class="sr-only">密码</label>
<input type="password" id="inputPassword" class="form-control" placeholder="Password" required> <input type="password" id="inputPassword_login" name="inputPassword_login" class="form-control" placeholder="密码" required>
<button class="btn btn-lg btn-primary btn-block" type="submit">登录</button>
<div style="margin-bottom: 10px; text-align:right;">
<button class="btn btn-sm btn-primary" id="login_button">登录</button>
</div>
</div> </div>
<!--pw_login end--> <!--pw_login end-->