
/**
 * Created by admin on 2021/4/5.
 * 
 */
define(['jquery', "Tools"], function ($, Tools) {
    //数据存储
    var module = {
        uuid: ''  //验证码uuid
    };
    //自定义公共方法
    var tools = new Tools();

    //默认进入页面加载方法
    module.init = function (page) {
        //点击登录
        $('#login-submit').on('click', module.login)
        //点击图形验证码
        $('#graphicImg').on('click', module.verificationCode)
        //图形验证码加载
        module.verificationCode()
    };

    /*-----------------------------自定义方法-------------------------------------*/
    //图形验证码
    module.verificationCode = function () {
        tools.doGet(captchaImage_get, {}, module.verificationAjax, true)
    }
    module.verificationAjax = function (data) {
        if (data.code == 200) {
            $('#graphicImg').attr('src', 'data:image/gif;base64,' + data.img)
            module.uuid = data.uuid;
        }
    }
    //用户登录
    module.login = function () {
        if (module.check()) {
            var data = {};
            var usernameVal = $('#username').val();
            var passwordVal = $('#password').val();
            var codeVal = $('#code').val();
            data['username'] = usernameVal;
            data['password'] = passwordVal;
            data['code'] = codeVal;
            data['uuid'] = module.uuid;
            tools.doPost(login_post, data, module.loginData, true)
        }
    };
    //手动验证表单
    module.check = function () {
        var usernameVal = $('#username').val();
        var passwordVal = $('#password').val();
        var codeVal = $('#code').val();
        /* 手机号 */
        if (usernameVal == '') {
            $('#username')[0].focus()
            tools.initTips('请输入用户名', 'right', $('#username')[0], 2000)
            return false;
        }
        /* 密码 */
        if (passwordVal == '') {
            $('#password')[0].focus()
            tools.initTips('请输入密码', 'right', $('#password')[0], 2000)
            return false;
        } else if (parseInt(passwordVal.length) < 6 || parseInt(passwordVal.length) > 18) {
            $('#password')[0].focus()
            tools.initTips('请输入正确格式密码', 'right', $('#password')[0], 2000)
            return false;
        }
        /*图形验证码*/
        if (module.uuid == '' || codeVal == '') {
            $('#code')[0].focus()
            tools.initTips('请输入图形验证码', 'right', $('#code')[0], 2000)
            return false;
        }
        return true;
    }
    //登录校验
    module.loginData = function (data) {
        if (data.code == 500) {
            module.verificationCode()
        } else {
            tools.setCookie('Admin-Token', data.token, 24 * 60 * 60)
            tools.skip('/')
        }
    }
    return module;
});