Blame view

public/js/passport/bind/_third-bind-mobile.js 8.41 KB
郝肖肖 authored
1 2 3 4
import $ from 'yoho-jquery';
import tip from 'plugin/tip';
import Page from 'yoho-page';
import api from '../api';
郝肖肖 authored
5 6
import dialog from 'plugin/dialog';
import bindDialogHbs from 'passport/bind-dialog-tip.hbs';
7
import Validate from 'plugin/validata';
郝肖肖 authored
8
9 10 11 12 13 14
const validate = new Validate('#js-img-check', {
    useREM: {
        rootFontSize: 40,
        picWidth: 150
    }
});
郝肖肖 authored
15 16
const showErrTip = tip.show;
17
class ThirdBindMobile extends Page {
郝肖肖 authored
18 19 20 21 22
    constructor() {
        super();

        this.selector = {
            itime: 0,
23 24
            openId: $.trim($.queryString().openId),
            sourceType: $.trim($.queryString().sourceType),
郝肖肖 authored
25 26 27 28 29 30
            countrySelect: $('.mobile-form-login select.country-select'),
            mobileInput: $('.mobile-form-login input.mobile-input'),
            clearMobile: $('.mobile-form-login .clear'),
            verifyCodeInput: $('.mobile-form-login input.verify-code-input'),
            verifyCodeBtn: $('.mobile-form-login .get-verify-code'),
            loginBtn: $('.mobile-form-login .sms-login-btn'),
31
            $captcha: $('#js-img-check')
郝肖肖 authored
32 33 34 35 36 37
        };

        this.init();
    }

    init() {
38 39 40 41
        if (this.selector.$captcha.data('userverify')) {
            validate.init();
        }
郝肖肖 authored
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
        this.bindEvents();
    }

    bindEvents() {
        this.selector.countrySelect.on('change', this.changeBtnStatus.bind(this));
        this.selector.mobileInput.on('input', this.changeBtnStatus.bind(this));
        this.selector.clearMobile.on('click', this.clearMobile.bind(this));
        this.selector.verifyCodeInput.on('input', this.changeBtnStatus.bind(this, true));
        this.selector.verifyCodeBtn.on('click', this.getVerifyCode.bind(this));
        this.selector.loginBtn.on('click', this.login.bind(this));
    }

    /**
     * 清除输入的手机号
     */
    clearMobile() {
        this.selector.mobileInput.val('');
        this.selector.verifyCodeInput.val('');
        this.selector.clearMobile.addClass('hide');
        this.selector.countrySelect.trigger('change');
    }

    /**
     * 输入监听,改变按钮状态
     */
    changeBtnStatus(isVerifycode) {
郝肖肖 authored
68 69 70
        let areaCode = $.trim(this.selector.countrySelect.val());
        let phoneNum = $.trim(this.selector.mobileInput.val());
        let verifyCode = $.trim(this.selector.verifyCodeInput.val());
郝肖肖 authored
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 110 111 112

        if (!api.phoneRegx[areaCode]) {
            return showErrTip('出错了,请重新刷新页面');
        }

        if (phoneNum) {
            this.selector.clearMobile.removeClass('hide');
        }

        // 验证码输入框单独处理
        if (isVerifycode === true) {
            // 提交表单按钮激活
            if (verifyCode && api.phoneRegx[areaCode].test(phoneNum)) {
                this.selector.loginBtn.addClass('active');
            } else {
                this.selector.loginBtn.removeClass('active');
            }

            return;
        }

        clearInterval(this.selector.itime);
        this.selector.verifyCodeBtn.text('获取验证码');

        if (api.phoneRegx[areaCode].test(phoneNum)) {
            this.selector.verifyCodeBtn.addClass('active');
            if (verifyCode) {
                this.selector.loginBtn.addClass('active');
            }
        } else {
            this.selector.verifyCodeBtn.removeClass('active');
            this.selector.loginBtn.removeClass('active');
        }
    }

    /**
     * 获取验证码倒计时
     */
    countDown(during) {
        let count = parseInt(during, 10) || 59;

        if (!this.selector.verifyCodeBtn.hasClass('active')) {
郝肖肖 authored
113
            return;
郝肖肖 authored
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
        }

        this.selector.verifyCodeBtn.removeClass('active');

        this.selector.itime = setInterval(() => {
            window.setCookie('intTimer', count, {path: '/' });

            if (count <= 0) {
                this.selector.verifyCodeBtn.text('重新获取').addClass('active');
                clearInterval(this.selector.itime);
            } else {
                this.selector.verifyCodeBtn.text('重新获取 (' + count-- + '秒)');
            }

        }, 1000);
    }


    /**
     * 获取验证码
     */
    getVerifyCode() {
        let pn = $.trim(this.selector.mobileInput.val());
        let area = $.trim(this.selector.countrySelect.val());
郝肖肖 authored
139 140
        if (!this.selector.verifyCodeBtn.hasClass('active') ||
            this.selector.verifyCodeBtn.data('oneClick')) {
郝肖肖 authored
141 142 143 144 145 146 147 148 149
            return;
        }

        if (!api.phoneRegx[area]) {
            return showErrTip('出错了,请重新刷新页面');
        } else if (!api.phoneRegx[area].test(pn)) {
            return showErrTip('手机号格式不正确,请重新输入');
        }
郝肖肖 authored
150
        this.selector.verifyCodeBtn.data('oneClick', true);
151
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
        validate.getResults().then(result => {
            let params = {
                area: area.replace('+', ''),
                mobile: pn
            };

            $.extend(params, result);

            this.ajax({
                url: '/passport/bind/thirdSendMsg',
                type: 'POST',
                data: params
            }).then(codeResult => {
                this.selector.verifyCodeBtn.data('oneClick', false);
                validate.type === 2 && validate.refresh();

                if (codeResult.code === 200) {
                    this.countDown();
                    return;
                } else {
                    showErrTip(codeResult.message);
                }

                (codeResult.changeCaptcha && validate.type !== 2) && validate.refresh();
            }).catch(() => {
                this.selector.verifyCodeBtn.data('oneClick', false);
                showErrTip('出错了,请重试');
                validate.refresh();
            });
181 182
        }).catch(() => {
            this.selector.verifyCodeBtn.data('oneClick', false);
郝肖肖 authored
183 184 185 186 187 188
        });
    }

    login() {
        let pn = $.trim(this.selector.mobileInput.val());
        let area = $.trim(this.selector.countrySelect.val());
郝肖肖 authored
189
        let verifyCode = $.trim(this.selector.verifyCodeInput.val());
郝肖肖 authored
190 191 192 193 194

        if (!this.selector.loginBtn.hasClass('active')) {
            return;
        }
郝肖肖 authored
195 196 197 198 199 200
        if (!api.phoneRegx[area]) {
            return showErrTip('出错了,请重新刷新页面');
        } else if (!api.phoneRegx[area].test(pn)) {
            return showErrTip('手机号格式不正确,请重新输入');
        }
201 202 203 204
        let params = {
            area: area.replace('+', ''),
            mobile: pn,
            code: verifyCode,
205 206
            open_id: this.selector.openId,
            source_type: this.selector.sourceType,
207
        };
郝肖肖 authored
208
209
        this.ajax({
210
            url: '/passport/bind/thirdMobileCheck',
211 212 213 214 215 216 217 218 219 220 221 222 223
            type: 'POST',
            data: params
        }).then(codeResult => {
            if (codeResult.code === 200) {
                this.continueBind(
                    $.extend({isBind: 'N', isRegister: 'N'}, codeResult.data),
                    params
                );
            } else {
                showErrTip(codeResult.message);
            }
        }).catch(() => {
            showErrTip('出错了,请重试');
郝肖肖 authored
224 225 226 227 228
        });
    }

    continueBind(codeResult, params) {
        let that = this;
郝肖肖 authored
229
        let goUrl = '/passport/bind/success';
郝肖肖 authored
230 231 232 233 234 235 236 237 238
        let bindParams = {
            isBind: codeResult.isBind === 'Y',
            isRegister: codeResult.isRegister === 'Y'
        };

        // 如果没有绑定没有注册,直接注册
        if (!(bindParams.isBind || bindParams.isRegister)) {
            document.location.href = goUrl;
            return;
郝肖肖 authored
239
        }
郝肖肖 authored
240 241

        dialog.showDialog({
242
            dialogText: bindDialogHbs(bindParams),
郝肖肖 authored
243 244 245 246 247 248 249
            fast: true,
            hasFooter: {
                leftBtnText: '去登录',
                rightBtnText: '继续'
            }
        }, function() {
            return that.ajax({
250
                url: '/passport/bind/thirdContinueMobile',
郝肖肖 authored
251 252 253 254 255 256 257 258 259 260 261 262 263
                type: 'POST',
                data: params
            }).then(cresult => {
                if (cresult.code === 200) {
                    document.location.href = goUrl;
                    return;
                } else {
                    showErrTip(cresult.message);
                }
            }).catch(() => {
                showErrTip('出错了,请重试');
            });
        }, function() {
264 265
            document.location.href = '//m.yohobuy.com/signin.html?area=' +
                params.area + '&mobile=' + params.mobile;
郝肖肖 authored
266 267 268
        });

        $('.dialog-wrapper').addClass('s-dialog-bind');
郝肖肖 authored
269 270 271
    }
}
272
module.exports = ThirdBindMobile;