Blame view

web/src/js/user/login-controller.js 4.39 KB
f0f58ad7   xu   app-wx(v0.1.0 bui...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/**
 * 登录
 */
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;
3a892ee0   xu   app-wx(v0.1.0 bui...
26
            if (!isGuest) {
1de3211f   xu   app-wx(v0.1.0 bui...
27
28
                window.location.href = url.to('order/#index');
                return '';
3a892ee0   xu   app-wx(v0.1.0 bui...
29
            }
f0f58ad7   xu   app-wx(v0.1.0 bui...
30
31
32
33
34
35
36
37
38
39
40
41
42
            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() {
3a892ee0   xu   app-wx(v0.1.0 bui...
43
44
45
46
47
48
49
50
51
                var mobile = utils.trim($$('#login .mobile').val());
                if ('' == mobile) {
                    utils.toast({content:'手机号码必填'})
                    return false;
                }
                if (!utils.isMobile(mobile)) {
                    utils.toast({content:'手机号码不合格'})
                    return false;
                }
f0f58ad7   xu   app-wx(v0.1.0 bui...
52
53
54
55
56
                if (false == clickGetCode) {
                    return false;
                }
                clearInterval(tt);
                clickGetCode = false;
3a892ee0   xu   app-wx(v0.1.0 bui...
57
                var pData = me.csrf({action:'login', mobile:mobile})
f0f58ad7   xu   app-wx(v0.1.0 bui...
58
59
60
61
62
63
                utils.httpGet(url.to(getCodeURL), pData, function(res) {
                    if (!res.success) {
                        utils.toast({content:res.message,closeDelay:3000})
                        clickGetCode = true;
                        return false;
                    }
32926e46   xu   app-wx(v0.1.0 bui...
64
                    utils.toast({content:res.message});
3a892ee0   xu   app-wx(v0.1.0 bui...
65
66
67
                    if(res.testCode) {
                        $$('#login .code').val(res.testCode);
                    }
f0f58ad7   xu   app-wx(v0.1.0 bui...
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
                    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) {
3a892ee0   xu   app-wx(v0.1.0 bui...
110
                    loginClick = true;
f0f58ad7   xu   app-wx(v0.1.0 bui...
111
112
                    if (!res.success) {
                        utils.toast({content:res.message,closeDelay:3000})
f0f58ad7   xu   app-wx(v0.1.0 bui...
113
114
                        return false;
                    } else {
3a892ee0   xu   app-wx(v0.1.0 bui...
115
                        window.location.href = url.to('order/#index');
f0f58ad7   xu   app-wx(v0.1.0 bui...
116
117
118
119
120
121
122
123
124
125
126
127
128
                    }
                })
            })
        }
        ctrl.gotoRegisterEvent = function() {

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