login-controller.js 4.34 KB
/**
 * 登录
 */
define(
    "user/login-controller",
    [
        'mk7/controller',
        'mk7/url',
        'mk7/utils',
    ],

    function(ctrl, url, utils) {

        var $$ = Dom7;
        var t7 = Template7;
        var ctrl = new ctrl();
        var getCodeURL = '/user/login/get-code';
        var loginURL = '/user/login/login';
        var clickGetCode = true;
        var loginClick = true;
        var tt = null
        ctrl.run = function () {
            var me = this;
            me.setPageTitle("登录");
            me.codeDuration = 60;
            if (!isGuest) {
                window.location.href = url.to('order/#index');
                return '';
            }
            me.render();
        }
        ctrl.bindEvents = function () {
            var me = this;
            console.log("bindEvents");
            me.getCodeEvent();
            me.gotoRegisterEvent();
            me.loginEvent();
        }

        ctrl.getCodeEvent = function() {
            var me = this;
            $$('#login .get-code-cls').click(function() {
                var mobile = utils.trim($$('#login .mobile').val());
                if ('' == mobile) {
                    utils.toast({content:'手机号码必填'})
                    return false;
                }
                if (!utils.isMobile(mobile)) {
                    utils.toast({content:'手机号码不合格'})
                    return false;
                }
                if (false == clickGetCode) {
                    return false;
                }
                clearInterval(tt);
                clickGetCode = false;
                var pData = me.csrf({action:'login', mobile:mobile})
                utils.httpGet(url.to(getCodeURL), pData, function(res) {
                    if (!res.success) {
                        utils.toast({content:res.message,closeDelay:3000})
                        clickGetCode = true;
                        return false;
                    }
                    if(res.testCode) {
                        $$('#login .code').val(res.testCode);
                    }
                    var codeDuration = res.codeDuration
                    me.codeDuration = codeDuration;
                    tt = setInterval(function(e) {
                        me.codeDuration--;
                        if (0 == me.codeDuration) {
                            $$('#login .get-code-cls').html('获取验证码');
                            clearInterval(tt);
                            me.codeDuration = codeDuration;
                            clickGetCode = true;
                        } else {
                            $$('#login .get-code-cls').html('<span class="count-down-cls">' +me.codeDuration+'s后重新获取</span>');
                        }
                    }, 1000)
                })
            })
        }
        ctrl.loginEvent = function() {
            var me = this;
            $$('#login .login-btn').click(function(e) {

                var mobile = utils.trim($$('#login .mobile').val());
                var code = utils.trim($$('#login .code').val());

                if ('' == mobile) {
                    utils.toast({content:'手机号码必填'})
                    return false;
                }
                if (!utils.isMobile(mobile)) {
                    utils.toast({content:'手机号码不合格'})
                    return false;
                }
                if ('' == code) {
                    utils.toast({content:'验证码必填'})
                    return false;
                }

                if (false == loginClick) {
                    return false;
                }
                loginClick = false;
                var pData = me.csrf({mobile:mobile,code:code})
                utils.httpPost(url.to(loginURL),pData, function(res) {
                    loginClick = true;
                    if (!res.success) {
                        utils.toast({content:res.message,closeDelay:3000})
                        return false;
                    } else {
                        window.location.href = url.to('order/#index');
                    }
                })
            })
        }
        ctrl.gotoRegisterEvent = function() {

            $$('#login .register-btn').click(function(e) {
                window.location.href = url.to('user/#register');
            })
        }
        return ctrl;
    }
);