BBS用户登录

BBS用户登录

login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>欢迎登录</title>
    <link rel="stylesheet" href="/static/bootstrap/css/bootstrap.min.css">
    {#    <link rel="stylesheet" href="/static/my_style.css">#}
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        #content {
            margin-top: 100px;
        }

        #login_content {
            margin-top: 100px;
        }

        #blog_avatar {
            width: 30px;
            height: 30px;
            margin-left: 500px;
        }

        #reg_home {
            margin-left: 550px;
        }

        #login_content {
            margin-top: 300px;
        }

        .home {
            background-image: url(https://github.com/Justinshu/my_statics/blob/master/cnblogs/blue.jpg?raw=true);
            background-size: cover;
            height: 975px;
        }
    </style>
</head>
<body>
<div class="home">
    <nav class="navbar navbar-info">
        <div class="container-fluid">
            <div class="navbar-header">
                <a class="navbar-brand" href="#">
                    <img id="blog_avatar" alt="Brand" src="/static/img/hmbb.png">
                </a>
                <a class="navbar-brand" href="#">BLOG</a>
            </div>
            <div class="navbar-header">
                <a class="navbar-brand" id="reg_home" href="/home/">首页</a>
            </div>
            <div class="navbar-header">
                <a class="navbar-brand" id="reg_login" href="/login/">登录</a>
            </div>
            <div class="navbar-header">
                <a class="navbar-brand" id="reg_reg" href="/reg/">注册</a>
            </div>
        </div>
    </nav>
    <div class="container" id="login_content">
        <div class="row col-md-6 col-md-offset-3 ">

            <h3>用户登录</h3>
            <hr>
            <form class="form-horizontal login-form">
                {% csrf_token %}
                <div class="form-group">
                    <label for="username" class="col-sm-2 control-label">用户名</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="username" name="username" placeholder="用户名">
                    </div>
                </div>
                <div class="form-group">
                    <label for="password" class="col-sm-2 control-label">密&nbsp&nbsp&nbsp&nbsp码</label>
                    <div class="col-sm-10">
                        <input type="password" class="form-control" id="password" name="password" placeholder="密码">
                    </div>
                </div>
                <!--极验科技滑动验证-->

                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <button type="button" class="btn btn-default" id="login-button">登&nbsp&nbsp&nbsp&nbsp录</button>
                        <span class="login-error"></span>
                    </div>
                </div>
                <div id="popup-captcha"></div>
            </form>
        </div>
    </div>
</div>

<script src="/static/jquery-3.3.1.js"></script>
<script src="/static/bootstrap/js/bootstrap.min.js"></script>
<script src="http://static.geetest.com/static/tools/gt.js"></script>
<script>
    {#    $("#login-button").click(function () {#}
    {#        // 1. 取到用户填写的用户名和密码 -> 取input框的值#}
    {#        var username = $("#username").val();#}
    {#        var password = $("#password").val();#}
    {#        var valid_code = $("#valid_code").val();#}
    {#        // 2. 用AJAX发送到服务端#}
    {#        $.ajax({#}
    {#            url: "/login/",#}
    {#            type: "post",#}
    {#            data: {#}
    {#                "username": username,#}
    {#                "password": password,#}
    {#                "valid_code": valid_code,#}
    {#                "csrfmiddlewaretoken": $("[name='csrfmiddlewaretoken']").val()#}
    {#            },#}
    {#            success: function (data) {#}
    {#                console.log(data);#}
    {#                if (data.status) {#}
    {#                    // 有错误,在页面上提示#}
    {#                    $(".login-error").text(data.msg);#}
    {#                } else {#}
    {#                    // 登陆成功#}
    {#                    location.href = data.msg;#}
    {#                }#}
    {#            }#}
    {#        })#}
    {#    });#}

    {#    // 当input框获取焦点时将之前的错误清空#}
    {#    $("#username,#password").focus(function () {#}
    {#        // 将之前的错误清空#}
    {#        $(".login-error").text("");#}
    {#    });#}
    {##}
    {#    // 点击验证码图片 刷新验证码#}
    {#    $("#valid-img").click(function () {#}
    {#        $(this)[0].src += "?";#}
    {#    });#}


    //滑动验证js代码
    var handlerPopup = function (captchaObj) {
        // 成功的回调
        captchaObj.onSuccess(function () {
            var validate = captchaObj.getValidate();
            $.ajax({
                url: "/login/", // 进行二次验证
                type: "post",
                dataType: "json",
                data: {
                    username: $('#username').val(),
                    password: $('#password').val(),
                    "csrfmiddlewaretoken": $("[name='csrfmiddlewaretoken']").val(),
                    geetest_challenge: validate.geetest_challenge,
                    geetest_validate: validate.geetest_validate,
                    geetest_seccode: validate.geetest_seccode
                },
                success: function (data) {
                    console.log(data);
                    if (data.status) {
                        // 有错误,在页面上提示
                        $(".login-error").text(data.msg);
                    } else {
                        // 登陆成功
                        location.href = data.msg;
                    }
                }
            });
        });
        $("#login-button").click(function () {
            captchaObj.show();
        });
        // 将验证码加到id为captcha的元素里
        captchaObj.appendTo("#popup-captcha");
        // 更多接口参考:http://www.geetest.com/install/sections/idx-client-sdk.html
    };
    $("#username,#password").focus(function () {
        // 将之前的错误清空
        $(".login-error").text("");
    });

    // 验证开始需要向网站主后台获取id,challenge,success(是否启用failback)
    $.ajax({
        url: "/pc-geetest/register?t=" + (new Date()).getTime(), // 加随机数防止缓存
        type: "get",
        dataType: "json",
        success: function (data) {
            // 使用initGeetest接口
            // 参数1:配置参数
            // 参数2:回调,回调的第一个参数验证码对象,之后可以使用它做appendTo之类的事件
            initGeetest({
                gt: data.gt,
                challenge: data.challenge,
                product: "popup", // 产品形式,包括:float,embed,popup。注意只对PC版验证码有效
                offline: !data.success // 表示用户后台检测极验服务器是否宕机,一般不需要关注
                // 更多配置参数请参见:http://www.geetest.com/install/sections/idx-client-sdk.html#config
            }, handlerPopup);
        }
    });

</script>
</body>
</html>

效果:

views.py

pc_geetest_id = "b46d1900d0a894591916ea94ea91bd2c"
pc_geetest_key = "36fc3fe98530eea08dfc6ce76e3d24c4"
#极速验证科技校验视图
def login(request):
    # if request.is_ajax():  # 如果是AJAX请求
    if request.method == "POST":
        # 初始化一个给AJAX返回的数据
        ret = {"status": 0, "msg": ""}
        # 从提交过来的数据中 取到用户名和密码
        username = request.POST.get("username")
        pwd = request.POST.get("password")
        # 获取极验 滑动验证码相关的参数
        gt = GeetestLib(pc_geetest_id, pc_geetest_key)
        challenge = request.POST.get(gt.FN_CHALLENGE, '')
        validate = request.POST.get(gt.FN_VALIDATE, '')
        seccode = request.POST.get(gt.FN_SECCODE, '')
        status = request.session[gt.GT_STATUS_SESSION_KEY]
        user_id = request.session["user_id"]
        if status:
            result = gt.success_validate(challenge, validate, seccode, user_id)
        else:
            result = gt.failback_validate(challenge, validate, seccode)
        if result:
            # 验证码正确
            # 利用auth模块做用户名和密码的校验
            user = auth.authenticate(username=username, password=pwd)
            if user:
                # 用户名密码正确
                # 给用户做登录
                auth.login(request, user)
                ret["msg"] = "/index/"
            else:
                # 用户名密码错误
                ret["status"] = 1
                ret["msg"] = "用户名或密码错误!"
        else:
            ret["status"] = 1
            ret["msg"] = "验证码错误"

        return JsonResponse(ret)
    return render(request, "login.html")


# 处理极验 获取验证码的视图
def get_geetest(request):
    user_id = 'test'
    gt = GeetestLib(pc_geetest_id, pc_geetest_key)
    status = gt.pre_process(user_id)
    request.session[gt.GT_STATUS_SESSION_KEY] = status
    request.session["user_id"] = user_id
    response_str = gt.get_response_str()
    return HttpResponse(response_str)

 

posted @ 2019-03-09 00:03  樵夫-justin  阅读(503)  评论(0)    收藏  举报