ticket.page.js 3.64 KB
/**
 * Created by TaoHuang on 2017/6/22.
 */


var $ = require('jquery');

var yas = require('../common/data-yas'),
    dialog = require('../common/dialog');

var $orderPrice = $('#order-price');
var order = {};

var yohoCoin;
var submitting = false;

require('../common');
require('../simple-header');

function errorInfo(info, url) {
    new dialog.Dialog({
        content: info,
        className: 'ensure-back-alert',
        btns: [{
            id: 'back-sure',
            btnClass: ['back-sure'],
            name: '返回商品详情页',
            cb: function() {
                window.jumpUrl(url);
            }
        }]
    }).show();
}

if ($('.error').length === 1) {
    errorInfo(
        $('.error').find('.info').text(),
        $('.error').find('.url').text()
    );
}

function validateUserInfo(info) {
    var errTip = '';

    if (!info.mobile) {
        errTip = '您还没有填写手机号';
    }

    if (!errTip && !/^\d{11}$/ig.test(info.mobile)) {
        errTip = '手机号只能是11位数字';
    }

    if (errTip) {
        new dialog.Alert((errTip)).show();
        return false;
    }

    return true;
}

function handleOrderInfo(info) {
    var $goods = $('.goods-item');

    info.sku = $goods.data('sku');
    info.count = $goods.data('num');
    info.mobile = $('.ticket-mobile-input').val();

    return info;
}

function compute(coin) {
    var req;

    order = handleOrderInfo(order);
    req = $.extend({}, order, {
        coin: coin || 0
    });

    $.ajax({
        type: 'POST',
        url: '/cart/ticketcompute',
        data: req
    }).then((result) => {
        if (result.code === 200) {
            order.coin = result.data.usedCoinNum;
            yohoCoin.maxCoin = result.data.canUseCoinNum;

            $orderPrice.html('¥ ' + result.data.last_order_amount);
        }
    });
}

// 有货币
yohoCoin = {
    $el: $('#yoho-coin-box'),
    init: function() {
        var data;

        if (!this.$el.length) {
            return;
        }

        data = this.$el.data();

        if (data) {
            order.coin = data.coin || 0;
            this.maxCoin = data.max;
            this.totalCoin = data.total;
        }

        this.eventBind();
    },
    eventBind: function() {
        var that = this;

        this.$el.on('click', '.coin-use-btn', function() {
            if (order.coin * 1 !== this.maxCoin * 1) {
                compute(that.maxCoin);
            }

            that.close();
        }).on('click', '.coin-cancel-btn', function() {
            if (order.coin * 1 !== 0) {
                order.coin = 0;
                compute();
            }

            that.close();
        });
    },
    close: function() {
        this.$el.prev().children('.locker-switch').trigger('click');
    }
};

$('.locker-switch').click(function() {
    var $this = $(this),
        $par = $this.parent();

    $par.toggleClass('open');
});


$('#order-submit').on('click', function() {
    var $this = $(this);

    if (submitting) {
        return;
    }

    order = handleOrderInfo(order);

    if (!validateUserInfo(order)) {
        return;
    }

    submitting = true;
    $.ajax({
        type: 'POST',
        url: '/cart/ticketSubmit',
        data: order
    }).then(function(data) {
        if (data.code === 200) {
            window.location.href = data.data.refer;
        } else if (data.code === 500) {
            errorInfo(data.message, $this.data('url'));
        }
    }).always(function() {
        submitting = false;
    });
});

yohoCoin.init();

// 获取用户是否新客(品众统计)写cookie
$.ajax({type: 'GET', url: '/home/newuser'});

// 订单确认页默认埋点
yas.givePoint('YB_SC_ORDER_ENSURE');