Blame view

public/js/passport/sms-check.page.js 4.19 KB
1
'use strict';
陈轩 authored
2
lijing authored
3
let tip, checkPoint;
4
lijing authored
5
let $resendBtn,
陈轩 authored
6 7
    $nextBtn,
    $smsCode,
陈轩 authored
8
    $resetBtn,
陈轩 authored
9
    mobile, area;
lijing authored
10
let page;
陈轩 authored
11
12
require('common');
13 14 15 16
tip = require('plugin/tip');
checkPoint = require('./smslogin/check-point');

page = {
陈轩 authored
17 18
    disableAjax: false,
陈轩 authored
19
    time: 60,
陈轩 authored
20 21 22 23 24
    resendText: '重发验证码',
    timerId: null,
    init: function() {
        this.domInit();
        this.bindEvents();
陈轩 authored
25 26
        if (window.countdown > 0) {
            this.countDown(window.countdown);
陈轩 authored
27
        }
陈轩 authored
28 29 30 31 32 33
    },


    domInit: function() {
        $resendBtn = $('#resend-sms');
        $nextBtn = $('#btn-next');
陈轩 authored
34
        $resetBtn = $('.clear-input');
陈轩 authored
35 36 37 38 39 40 41
        $smsCode = $('#sms-code');
        mobile = $('#mobile').val();
        area = $('#area').val();
    },


    bindEvents: function() {
lijing authored
42
        let self = this;
陈轩 authored
43 44 45 46 47 48

        $resendBtn.on('click', function() {
            self.resendSMS();
        });

        $smsCode.on('input', function() {
lijing authored
49
            let hasVal = Boolean($.trim(this.value));
陈轩 authored
50
陈轩 authored
51
            $nextBtn.trigger('toggleDisable', !hasVal);
陈轩 authored
52
            $resetBtn.toggle(hasVal);
陈轩 authored
53 54 55
        });

        $nextBtn.on('click', function() {
陈轩 authored
56
            self.submit();
陈轩 authored
57
        });
陈轩 authored
58 59 60 61

        $resetBtn.on('click', function() {
            $smsCode.val('');
            $resetBtn.hide();
陈轩 authored
62
            $nextBtn.trigger('toggleDisable');
陈轩 authored
63
        });
陈轩 authored
64
陈轩 authored
65 66 67 68 69 70
        $nextBtn.on('toggleDisable', function(event, bool) {
            if (bool === void 0) {
                bool = true;
            }

            $nextBtn.toggleClass('disable', bool);
陈轩 authored
71
            $nextBtn.prop('disabled', bool);
陈轩 authored
72 73
        });
    },
陈轩 authored
74
陈轩 authored
75
    countDown: function(during) {
lijing authored
76 77
        let self = this;
        let second = this.time;
陈轩 authored
78
陈轩 authored
79 80 81 82
        if (during) {
            clearInterval(this.timerId);
            second = during;
        } else if (this.timerId) {
陈轩 authored
83 84 85
            return;
        }
陈轩 authored
86
        $resendBtn.prop('disabled', true);
郭成尧 authored
87
        $resendBtn.text('重新获取(' + second + ')');
陈轩 authored
88
        this.timerId = setInterval(function() {
lijing authored
89
            let txt = self.resendText;
陈轩 authored
90 91 92 93 94 95

            second = second - 1;

            if (second < 0) {
                clearInterval(self.timerId);
                self.timerId = null;
陈轩 authored
96
                $resendBtn.prop('disabled', false);
陈轩 authored
97
            } else {
郭成尧 authored
98
                txt = '重新获取(' + second + '秒)';
陈轩 authored
99 100 101 102 103 104 105 106 107
            }

            $resendBtn.text(txt);
        }, 1000);

    },


    resendSMS: function() {
lijing authored
108
        let self = this;
陈轩 authored
109
陈轩 authored
110
        if ($resendBtn.prop('disabled')) {
陈轩 authored
111 112 113
            return;
        }
陈轩 authored
114
        $.get('/passport/sms_login/token.json', {
陈轩 authored
115
            area: area,
姜枫 authored
116
            mobile: mobile
陈轩 authored
117
        })
陈轩 authored
118 119 120 121
            .done(function(res) {
                if (res.code === 200) {
                    self.countDown();
                    return;
陈轩 authored
122 123
                } else {
                    res.during && (self.countDown(res.during));
陈轩 authored
124 125 126 127
                }

                tip.show(res.message);
            })
陈轩 authored
128
            .fail(function() {
陈轩 authored
129
                tip.show('出错啦~休息一下');
陈轩 authored
130 131 132 133
            });
    },

    submit: function() {
lijing authored
134
        let code = $.trim($smsCode.val());
陈轩 authored
135
陈轩 authored
136 137 138 139
        if ($nextBtn.prop('disabled')) {
            return;
        }
陈轩 authored
140
        $nextBtn.prop('disabled', true);
陈轩 authored
141
        $.get('/passport/sms_login/check.json', {
陈轩 authored
142 143 144 145
            code: code
        })
            .done(function(res) {
                if (res.code === 200) {
146
                    checkPoint('YB_MOBILE_LOGIN_C'); // 埋点
陈轩 authored
147 148

                    if (res.newer) {
149 150
                        res.redirect = res.redirect + '&registerCode=' + res.registerCode;
周少峰 authored
151
                        // 暂时跳注册 TODO
152 153 154 155 156
                        // tip.show('请先注册!');
                        // setTimeout(function(){
                        //     location.href = '//m.yohobuy.com/reg.html';
                        // }, 2000);
                        // return;
陈轩 authored
157 158
                    }
陈轩 authored
159 160 161 162 163 164 165 166
                    location.href = res.redirect;
                    return;
                }

                tip.show(res.message);
            })
            .fail(function() {
                tip.show('出错了, 请重试');
陈轩 authored
167 168
            })
            .always(function() {
陈轩 authored
169
                $nextBtn.prop('disabled', false);
陈轩 authored
170 171 172 173 174 175 176
            });
    }
};

$(function() {
    page.init();
});