order-ensure.js 4.32 KB
import dialog from 'plugin/dialog';
import safeCheckBoxHbs from 'cart/order-ensure/safe-check-box.hbs';
import Page from 'yoho-page';
import tip from 'plugin/tip';

class OrderEnsure extends Page {
    constructor(order) { // 参数为使用哪个 cookie
        super();

        this.order = order;
        this.orderInfo = order.orderInfo;

        this.selector = {
            invoice: $('.invoice'),
            invoiceType: $('.invoice-type'),
            userMobile: $('.user-mobile')
        };

        this.init();
        this.bindEvents();
    }

    init() {
        if (window.getUid() !== this.orderInfo('uid')) {
            this.order.init();
            window.location.reload();
        }
    }

    bindEvents() {
        this.selector.invoice.on('click', '.checkbox', this.needInvoice.bind(this));
    }

    /**
     * 是否需要开发票
     */
    needInvoice(event) {
        let $this = $(event.currentTarget);

        this.orderInfo('invoice', $this.hasClass('icon-cb-radio'));
        if ($this.hasClass('icon-cb-radio')) {
            this.selector.invoice.addClass('focus');
            this.selector.invoiceType.html('电子发票(个人)<i class="iconfont">&#xe614;</i>');
            this.orderInfo('receiverMobile', this.selector.userMobile.val());
            this.orderInfo('invoices_type', 2);
        }
        if ($this.hasClass('icon-radio')) {
            this.selector.invoice.removeClass('focus');
            this.selector.invoiceType.html('');
            this.orderInfo('invoices_title', null);
            this.orderInfo('invoices_type', null);
            this.orderInfo('receiverMobile', null);
            this.orderInfo('buyerTaxNumber', null);
        }
        event.preventDefault();
        event.stopPropagation();
    }

    /**
     * 验证码弹窗
     * @param {手机号} mobile
     * @param {弹窗确定事件的回调} sureCallback
     */
    showSafeCheckDialog(renderData, sureCallback) {
        dialog.showDialog({
            hasHeader: '安全验证',
            dialogText: safeCheckBoxHbs(Object.assign({
                mobile: this.selector.userMobile.val()
            }, renderData)),
            hasFooter: {
                leftBtnText: '取消',
                rightBtnText: '确定'
            }
        });

        dialog.rewriteEvents().then(() => {
            this.sckDialogClickHandle(sureCallback);
        });
    }

    /**
     * 验证码弹窗事件处理
     */
    sckDialogClickHandle(sureCallback) {
        let $dialogWrapper = $('#dialog-wrapper');
        let $verifyCodeInput = $dialogWrapper.find('input[name=verifyCode]');
        let $getVerifyCodeBtn = $('#getVerifyCodeBtn');
        let $cancelBtn = $dialogWrapper.find('.dialog-left-btn');
        let $sureBtn = $dialogWrapper.find('.dialog-right-btn');

        this.countDown();

        $verifyCodeInput.on('input', () => {
            if ($(event.target).val()) {
                $sureBtn.addClass('active');
            } else {
                $sureBtn.removeClass('active');
            }
        });

        $getVerifyCodeBtn.on('click', () => {
            if (!$getVerifyCodeBtn.hasClass('disable')) {
                $verifyCodeInput.val('');
                this.resendSms();
            }
        });

        $cancelBtn.on('click', () => {
            $dialogWrapper.fadeOut();
        });

        $sureBtn.on('click', () => {
            if ($(event.target).hasClass('active')) {
                sureCallback($verifyCodeInput.val());
            }
        });
    }

    /**
     * 重发验证码
     */
    resendSms() {
        this.ajax({
            url: '/cart/index/new/giftCardSendSms'
        }).then(result => {
            if (result.code === 200) {
                this.countDown();
            }
            tip.show(result.message);
        });
    }

    /**
     * 获取验证码倒计时
     */
    countDown() {
        let count = 59;
        let itime;
        let $getVerifyCodeBtn = $('#getVerifyCodeBtn');

        $getVerifyCodeBtn.addClass('disable');

        itime = setInterval(() => {
            if (count === 0) {
                $getVerifyCodeBtn.text('重新获取').removeClass('disable');
                clearInterval(itime);
            } else {
                $getVerifyCodeBtn.text('重新获取 (' + count-- + ')');
            }
        }, 1000);
    }
}

module.exports = OrderEnsure;