<script type="text/html" template>
    <link rel="stylesheet" href="{{ layui.setter.base }}style/login.css?v={{ layui.admin.v }}-1" media="all">
</script>

<script src="js/md5.min.js"></script>

<div class="layadmin-user-login layadmin-user-display-show" id="LAY-user-login" style="display: none;">
    <div class="layadmin-user-login-main">
        <div class="layadmin-user-login-box layadmin-user-login-header">
            <h2>RtspLiveServer</h2>
        </div>
        <div class="layui-card">
            <div class="layui-card-header">
                用户登录
            </div>
            <div class="layui-card-body layui-row ">
                <div class="layadmin-user-login-box layadmin-user-login-body layui-form">
                    <div class="layui-form-item">
                        <label class="layadmin-user-login-icon layui-icon layui-icon-username"
                            for="LAY-user-login-username"></label>
                        <input type="text" name="user" id="LAY-user-login-username" maxlength="16"
                            lay-verify="required" placeholder="用户名" class="layui-input">
                    </div>
                    <div class="layui-form-item">
                        <label class="layadmin-user-login-icon layui-icon layui-icon-password"
                            for="LAY-user-login-password"></label>
                        <input type="password" name="pwd" id="LAY-user-login-password" maxlength="16"
                            lay-verify="required" placeholder="密码" class="layui-input">
                    </div>

                    <!--<div class="layui-form-item" style="margin-bottom: 20px;">
                        <input type="checkbox" name="remember" lay-skin="primary" title="记住密码">-->
                    <!--<a lay-href="/user/forget" class="layadmin-user-jump-change layadmin-link" style="margin-top: 7px;">忘记密码？</a>-->
                    <!--</div>-->
                    <div class="layui-form-item">
                        <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="LAY-user-login-submit"
                            id="login">登录</button>
                    </div>
                </div>
            </div>
        </div>

    </div>

    <div class="layui-trans layadmin-user-login-footer">

        <p>© 2021 rtsps.com</p>
    </div>
</div>

<script>
    layui.use(['admin', 'form', 'login'], function () {
        var $ = layui.$
            , setter = layui.setter
            , admin = layui.admin
            , form = layui.form
            , router = layui.router()
            , search = router.search;

        form.render();

        $("input[name='user']").focus();
        //提交
        form.on('submit(LAY-user-login-submit)', function (obj) {

            var field = obj.field; //获取提交的字段
            field.pwd = md5(field.pwd);
            $.ajax(
                {
                    type: "POST",
                    url: '/api/login',
                    cache: false,//禁止缓存
                    data: field,
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        var res = XMLHttpRequest.responseJSON;
                        if (res != undefined)
                            layer.msg('用户名或密码不正确!', { icon: 0, time: 2000 });
                    },
                    success: function (data) {
                        //请求成功后，写入 token
                        //layui.sessionData 页面关闭失效；layui.data数据会永久存在，除非物理删除。
                        layui.data(setter.tableName, {
                            key: setter.request.userId,
                            value: field.user
                        });
                        //标记
                        layui.data(setter.tableName, {
                            key: setter.request.tokenName
                            , value: true
                        });
                        layer.msg('验证成功', {
                            offset: '15px'
                            , icon: 1
                            , time: 1000
                        }, function () {
                            location.hash = search.redirect ? decodeURIComponent(search.redirect) : '/';
                        });
                    }
                });

        });
        $('input[name="pwd"]').keydown(function (e) {
            if (e.keyCode === 13) {
                $('#login').click();
            }
        })

    });
</script>