ticket-ensure.js 8.5 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: $('.to-play em'),
        $errorTip: $('.errbitip'),
        $tbody: $('table tbody', '.pay-wapper'),
        //门票js
        $showTicketMobile: $('.show-ticket-mobile'),
        $ticketMobile: $('.ticket-mobile'),
        $ticketModifyBtn: $('.ticket-modify-btn'),
        $setTicketMobile: $('.set-ticket-mobile'),
        $ticketMobileBtn: $('.ticket-mobile-btn'),
        $ticketMobileInput: $('.ticket-mobile-input')
    },
    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());

            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(),
                phoneReg = /^(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/;

            if (!phoneReg.test(val)) {
                new dialog.Alert('请输入正确的手机号!').show();
                return;
            }
            el.$showTicketMobile.removeClass('hide');
            el.$setTicketMobile.addClass('hide');
            el.$ticketMobile.text(val);
        });

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

            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();

            if (!/^\d+$/.test(selectData.yohoCoin + '') || selectData.yohoCoin * 1 === 0) {
                $('.errbitip').html('请输入一个正整数');
                $('#biprice').removeClass('is-select');
            } else if (selectData.yohoCoin * 1 > $('.play-pan em').text() * 1) {
                $('.errbitip').html('您的YOHO币不足');
                $('#biprice').removeClass('is-select');
            } else {
                el.$loading.show();
                $.ajax({
                    type: 'POST',
                    url: '/cart/index/orderCompute',
                    data: selectData,
                    success: function(data) {
                        el.$loading.hide();
                        if (data.code === 200) {
                            _this.render(data.data);
                            c.winHide(pan);
                        } else {
                            $('.errbitip').html(data.message);
                            $('#biprice').removeClass('is-select');
                        }
                    }
                });
            }
        });

        // 修改yoho币
        $(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('您的YOHO币不足');
            } 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);
                    }
                }
            });
        });
    },
    //渲染价格明细
    render: function(data) {
        // 下面这段是在服务端算价格
        var e = this.el,
            i,
            dArr = data.promotion_formula_list,
            str = '',
            isExpress = '';

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

            if (dArr[i].promotion === 'YOHO币:') {
                dArr[i].promotion = 'YOHO币<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.last_order_amount).toFixed(2));

        //有货币清零状态更新
        $('#biprice').val(accMul(100, data.use_yoho_coin));
        if (data.use_yoho_coin === 0) {
            $('.errbitip').text('');
        } else {
            $('.errbitip').text('抵扣' + data.use_yoho_coin.toFixed(2) + '元');
        }

    },
    getSelectData: function() {
        var expressVal = $('.express-list input[name="carriagegroup"]:checked').val();
        var biVal = $('#biprice.is-select').val();
        var cartType = $('.address-manage').attr('cart-type');
        var resultData;

        resultData = {
            cartType: cartType,
            deliveryWay: expressVal,
            yohoCoin: biVal
        };
        return resultData;
    }
};

TicketCat.init();