ticket-ensure.js 11.6 KB
/**
 * @description: 订单保存
 * @time: 2015/12/21
 */

var $ = require('yoho.jquery');

var dialog = require('../common/dialog');

var accMul = function(arg1, arg2) {
    var m = 0,
 s1 = arg1.toString(),
 s2 = arg2.toString();

    try {
        m += s1.split('.')[1].length;
    } catch (e) {
    }
    try {
        m += s2.split('.')[1].length;
    } catch (e) {
    }
    return Number(s1.replace('.', '')) * Number(s2.replace('.', '')) / Math.pow(10, m);
};

//立即购买-门票
var TicketCat = {
    el: {
        $pan: $('.pan'),
        $biok: $('#bisubmit'),
        $loading: $('.loading'),
        $payTotal: $('.play-total'),
        $toPay: $('.ticket-play em'),
        $errorTip: $('.errbitip'),
        $tbody: $('table tbody', '.pay-wapper'),
        $yohoCoinHelp: $('.yoho-coin-help'),

        //门票js
        $showTicketMobile: $('.show-ticket-mobile'),
        $ticketMobile: $('.ticket-mobile'),
        $ticketModifyBtn: $('.ticket-modify-btn'),
        $setTicketMobile: $('.set-ticket-mobile'),
        $ticketMobileBtn: $('.ticket-mobile-btn'),
        $ticketMobileInput: $('.ticket-mobile-input'),

        //变量
        phoneReg: /^(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/,
        numberReg: /^\d{11,30}$/
    },
    Data: {
        buyNumber: 0,//购买总数量
        productSku: 0
    },
    Common: {
        enterNUM: function(str1, str2) {
            str1 = str1.replace(/[^\d]/g, '');
            if (str1 && str2) {
                str1 = parseInt(str1);
                str2 = parseInt(str2);
                if (str1 > str2) {
                    str1 = str2;
                }
            }
            return str1;
        },
        winShow: function(pan) {
            pan.find('dt').show();
            pan.find('dd').show();
            if (pan.attr('class').match(/(-bi-)|(-juan-)/g)) {
                pan.find('dt').hide();
            }
        },
        winHide: function(pan) {
            pan.find('dt').show();
            pan.find('dd').hide();
        },
        strFixed: function(str) {
            return parseFloat(str).toFixed(2);
        }
    },
    init: function() {
        var _this = this,
            el = this.el,
            c = this.Common,
            productSubtotal = 0;

        //初始化产品价格(表格中的价格)
        el.$tbody.find('tr').each(function(index) {
            var td = $(this).find('td');
            var len = el.$tbody.find('tr').length;
            var subtotal = parseFloat(td.eq(1).html()) * parseInt(td.eq(3).html());

            _this.Data.productSku = $(this).data('sku');
            _this.Data.buyNumber += parseInt(td.eq(3).html());
            td.eq(1).html('¥' + td.eq(1).html());

            if (td.hasClass('xforone')) {
                td.eq(4).find('del').html('¥' + c.strFixed(subtotal));
                subtotal = 0;
            } else {
                td.eq(4).html('¥' + c.strFixed(subtotal));
            }
            productSubtotal += subtotal;
            if (!index) {
                td.last().attr('rowspan', len);
            } else {
                td.last().remove();
            }
            if (index === len - 1) {
                el.$tbody.find('tr').eq(0).find('td').last().html('<strong>¥' +
                    c.strFixed(productSubtotal) + '</strong>');
            }
        });

        //修改手机号按钮
        el.$ticketModifyBtn.click(function() {
            el.$showTicketMobile.addClass('hide');
            el.$setTicketMobile.removeClass('hide');
        });

        //保存手机号按钮
        el.$ticketMobileBtn.click(function() {
            var val = el.$ticketMobileInput.val();

            if (!el.numberReg.test(val)) {
                new dialog.Alert('请输入正确的手机号!').show();
                return;
            }

            el.$showTicketMobile.removeClass('hide');
            el.$setTicketMobile.addClass('hide');
            el.$ticketMobile.text(val);

            window.setCookie('ticketMobileInput', val, {
                domain: '.yohobuy.com',
                path: '/'
            });
        });

        //手机号,保存cookie中,防止用户刷新
        if (el.numberReg.test(window.cookie('ticketMobileInput'))) {
            el.$showTicketMobile.removeClass('hide');
            el.$setTicketMobile.addClass('hide');
            el.$ticketMobile.text(window.cookie('ticketMobileInput'));
        }

        // 使用优惠券、使用有货币点击展开
        el.$pan.find('dt').click(function() {
            var pan = $(this).parent('dl.pan');

            if ($(this).hasClass('not-btn')) {
                return false;
            }

            if (pan.find('dd').is(':hidden')) {
                c.winShow(pan);
                $(this).addClass('active');
            } else {
                c.winHide(pan);
                $(this).removeClass('active');
            }
        });

        //使用有货币确认按钮
        el.$biok.click(function() {
            var pan = $(this).parents('dl.pan');
            var selectData = {};

            $('#biprice').addClass('is-select');
            selectData = _this.getSelectData();

            el.$loading.show();
            $.ajax({
                type: 'POST',
                url: '/cart/index/setTicket',
                data: selectData,
                success: function(data) {
                    var result = data.data;
                    $('.errbitip').html('');
                    el.$loading.hide();
                    if (data.code === 200) {
                        if (result.yohoCoinCompute && !result.yohoCoinCompute.yohoCoinClick) {
                            $('.errbitip').html(result.yohoCoinCompute.yohoCoinMsg);
                            $('#biprice').removeClass('is-select');
                            $('.yohoCoinTip').addClass('coin-gray');
                            return false;
                        }
                        _this.render(result);
                        c.winHide(pan);
                    } else {
                        $('.errbitip').html(data.message);
                        $('#biprice').removeClass('is-select');
                    }
                }
            });
        });

        // 修改有货币
        $(document).on('click', '.bi-modify', function() {
            var $pan = $('.play-bi-pan');

            c.winShow($pan);
        });

        //键盘事件,计算有币货抵多少钱
        $('#biprice').keyup(function() {
            var val = $(this).val();

            if (!/^\d+$/.test(val + '')) {
                el.$errorTip.html('请输入一个正整数');
            } else if (val * 1 > $(this).data('bi') * 1) {
                el.$errorTip.html('您的有货币不足');
            } else {
                el.$errorTip.html('抵扣' + (val / 100).toFixed(2) + '元');
            }
        });

        // 取消yoho 币
        $('.cancel-bi').click(function() {
            var $pan = $('.play-bi-pan'),
                selectData = {};

            $('.errbitip').html('');
            el.$loading.show();
            selectData = _this.getSelectData();
            selectData.yohoCoin = 0;
            $.ajax({
                type: 'POST',
                url: '/cart/index/setTicket',
                data: selectData,
                success: function(data) {
                    el.$loading.hide();
                    if (data.code === 200) {
                        _this.render(data.data);
                        c.winHide($pan);

                        $('#biprice').val('0');
                        $('#biprice').removeClass('is-select');
                        $('.errbitip').html('');
                    } else {
                        $('.errbitip').html(data.message);
                    }
                }
            });
        });

        // 去支付
        $('.ticket-play input.submit').click(function() {
            var $this = $(this);
            var selectData = _this.getSelectData();

            selectData.mobile = el.$ticketMobile.text();

            if (!el.numberReg.test(selectData.mobile)) {
                new dialog.Alert('手机号格式不正确!').show();
                return;
            }

            el.$loading.show();

            if (!!$this.attr('disabled')) {
                return;
            }

            $this.attr('disabled', 'disabled');
            $.ajax({
                type: 'POST',
                dataType: 'json',
                url: '/cart/index/ticketOrderSub',
                data: selectData
            }).then(function(data) {
                el.$loading.hide();

                if (data.code === 200) {

                    //域名不一样,导直连接错误
                    window.location.href =
                        '//www.yohobuy.com/shopping/pay?ordercode=' + data.data.order_code;
                } else {
                    new dialog.Alert(data.message).show();
                }
            }).always(function() {
                el.$loading.hide();
                $this.removeAttr('disabled');
            });
        });

        //有货币帮助弹框
        el.$yohoCoinHelp.hover(function() {
            $(this).find('.yoho-coin-help-dialog').removeClass('hide');
        }, function() {
            $(this).find('.yoho-coin-help-dialog').addClass('hide');
        });
    },

    //渲染价格明细
    render: function(data) {
        var e = this.el,
            i,
            dArr = data.shopping_cart_data.promotion_formula_list,
            str = '',
            isExpress = '';

        for (i = 0; i < dArr.length; i++) {

            if (dArr[i].promotion === '有货币') {
                dArr[i].promotion = '有货币<span class="bi-modify">[修改]</span>:';
            }

            if (dArr[i].isExpress) {
                isExpress = 'total-express-w';
            } else {
                isExpress = '';
            }

            str += '<li class="' + isExpress + '">' +
                '<label>' + dArr[i].promotion + '</label>' +
                '<em>' + dArr[i].promotion_amount + '</em>' +
                '</li>';
        }

        e.$payTotal.html(str);
        e.$toPay.html((data.shopping_cart_data.last_order_amount).toFixed(2));

        //有货币清零状态更新
        $('dl.play-bi-pan').find('dt').removeClass('not-btn');
        $('.not-btn-tip').addClass('hide');
        $('.yohoCoinTip').removeClass('coin-gray');
        if (!data.yohoCoinCompute.yohoCoinClick) {
            //不可以使用状态
            $('dl.play-bi-pan').find('dt').addClass('not-btn');
            $('.not-btn-tip').removeClass('hide');
            //如果有货币按钮为打开状态,则关闭
            _this.Common.winHide($('.play-bi-pan'));
        }
        $('#biprice').data('yoho-coin-click', data.yohoCoinCompute.yohoCoinClick);
        $('#biprice').find('.num-limit-coin').html(data.yohoCoinCompute.yoho_coin_pay_rule.num_limit);
        $('#biprice').find('.totalYohoCoinNum').html(data.yohoCoinCompute.totalYohoCoinNum);
        $('#biprice').find('.yohoCoinNum').html(data.yohoCoinCompute.yohoCoinNum);
        $('#biprice').find('.yohoCoin').html(data.yohoCoinCompute.yohoCoin);
    },
    getSelectData: function() {
        var expressVal = $('.express-list input[name="carriagegroup"]:checked').val();
        var biVal = $('#biprice.is-select').data('yoho-coin-num') * 1;
        var cartType = $('.address-manage').attr('cart-type');
        var resultData;

        resultData = {
            cartType: cartType,
            deliveryWay: expressVal,
            yohoCoin: biVal,
            productSku: this.Data.productSku,
            buyNumber: this.Data.buyNumber
        };
        return resultData;
    }
};

TicketCat.init();