Blame view

public/js/cart/cartbuynow/order-ensure.js 4.32 KB
郭成尧 authored
1 2
import dialog from 'plugin/dialog';
import safeCheckBoxHbs from 'cart/order-ensure/safe-check-box.hbs';
郭成尧 authored
3 4
import Page from 'yoho-page';
import tip from 'plugin/tip';
郭成尧 authored
5
郭成尧 authored
6
class OrderEnsure extends Page {
郭成尧 authored
7
    constructor(order) { // 参数为使用哪个 cookie
郭成尧 authored
8 9
        super();
郭成尧 authored
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
        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();
    }
郭成尧 authored
58 59 60

    /**
     * 验证码弹窗
郭成尧 authored
61 62
     * @param {手机号} mobile
     * @param {弹窗确定事件的回调} sureCallback
郭成尧 authored
63
     */
64
    showSafeCheckDialog(renderData, sureCallback) {
郭成尧 authored
65 66
        dialog.showDialog({
            hasHeader: '安全验证',
郭成尧 authored
67
            dialogText: safeCheckBoxHbs(Object.assign({
68
                mobile: this.selector.userMobile.val()
郭成尧 authored
69
            }, renderData)),
郭成尧 authored
70 71 72 73 74
            hasFooter: {
                leftBtnText: '取消',
                rightBtnText: '确定'
            }
        });
郭成尧 authored
75 76 77 78

        dialog.rewriteEvents().then(() => {
            this.sckDialogClickHandle(sureCallback);
        });
郭成尧 authored
79 80 81 82 83 84 85
    }

    /**
     * 验证码弹窗事件处理
     */
    sckDialogClickHandle(sureCallback) {
        let $dialogWrapper = $('#dialog-wrapper');
郭成尧 authored
86
        let $verifyCodeInput = $dialogWrapper.find('input[name=verifyCode]');
郭成尧 authored
87 88 89 90 91 92
        let $getVerifyCodeBtn = $('#getVerifyCodeBtn');
        let $cancelBtn = $dialogWrapper.find('.dialog-left-btn');
        let $sureBtn = $dialogWrapper.find('.dialog-right-btn');

        this.countDown();
郭成尧 authored
93
        $verifyCodeInput.on('input', () => {
郭成尧 authored
94 95 96 97 98 99 100 101 102
            if ($(event.target).val()) {
                $sureBtn.addClass('active');
            } else {
                $sureBtn.removeClass('active');
            }
        });

        $getVerifyCodeBtn.on('click', () => {
            if (!$getVerifyCodeBtn.hasClass('disable')) {
郭成尧 authored
103
                $verifyCodeInput.val('');
郭成尧 authored
104 105 106 107 108 109 110 111 112 113
                this.resendSms();
            }
        });

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

        $sureBtn.on('click', () => {
            if ($(event.target).hasClass('active')) {
郭成尧 authored
114
                sureCallback($verifyCodeInput.val());
郭成尧 authored
115 116 117 118
            }
        });
    }
郭成尧 authored
119 120 121
    /**
     * 重发验证码
     */
郭成尧 authored
122
    resendSms() {
郭成尧 authored
123 124 125 126 127 128 129 130
        this.ajax({
            url: '/cart/index/new/giftCardSendSms'
        }).then(result => {
            if (result.code === 200) {
                this.countDown();
            }
            tip.show(result.message);
        });
郭成尧 authored
131 132 133 134 135 136
    }

    /**
     * 获取验证码倒计时
     */
    countDown() {
137
        let count = 59;
郭成尧 authored
138 139 140 141 142 143 144 145 146 147
        let itime;
        let $getVerifyCodeBtn = $('#getVerifyCodeBtn');

        $getVerifyCodeBtn.addClass('disable');

        itime = setInterval(() => {
            if (count === 0) {
                $getVerifyCodeBtn.text('重新获取').removeClass('disable');
                clearInterval(itime);
            } else {
郭成尧 authored
148
                $getVerifyCodeBtn.text('重新获取 (' + count-- + ')');
郭成尧 authored
149 150
            }
        }, 1000);
郭成尧 authored
151
    }
郭成尧 authored
152 153 154
}

module.exports = OrderEnsure;