functions.js 2.77 KB
let $ = require('yoho-jquery'),
    tip = require('plugin/tip');

let functions = {
    /**
     * 添加门票
     */
    addTickets(productSku, buyNumber) {
        var data = {
            productSku: productSku,
            buyNumber: buyNumber
        };

        // 校验电子票
        $.ajax({
            url: '/cart/index/checkTickets',
            dataType: 'json',
            data: data,
            type: 'post',
            success: function(addRestult) {
                if (addRestult.code !== 200) {
                    if (addRestult.code === 401) {
                        window.location.href = '//m.yohobuy.com/signin.html?refer=' + window.location.href;
                    }
                    tip.show(addRestult.message);
                } else {
                    $('#productSku').val(productSku);
                    $('#buyNumber').val(buyNumber);
                    $('#buyNowForm').submit();
                }
            },
            error: function() {
                tip.show('网络异常~');
            }

        });
    },

    /**
     * 添加限购商品
     */
    addLimitCode(limitProductCode, sku, skn, buyNum) {
        let url = $('#limitProductPay').val() + '?limitproductcode=' + limitProductCode + '&sku=' +
            sku + '&skn=' + skn + '&buy_number=' + buyNum;

        // 调用接口判断商品是否可以购买
        $.ajax({
            url: url
        }).then(function(res) {
            // 如果有错,则商品不可购买,执行页面刷新,否则跳到结算页面
            if (res.error) {
                tip.show(res.message);
                setTimeout(function() {
                    location.reload();
                }, 2000);
            } else {
                location.href = url;
            }
        }).fail(function() {
            tip.show('网络异常!');
            setTimeout(function() {
                location.reload();
            }, 2000);
        });
    },

    /**
     * 添加到购物车
     */
    addToCart(sku, skn, buyNum) {
        $.ajax({
            method: 'POST',
            url: '/cart/index/add',
            data: {
                productSku: sku,
                new_product_skn: skn,
                buyNumber: buyNum
            }
        }).done(function(res) {
            var cartNum;

            if (res.code === 200) {
                cartNum = res.data.goods_count;
                if (cartNum > 99) {
                    cartNum = '99+';
                }
                $('.num-tag').html(cartNum).removeClass('hide');
            }

            if (res.message) {
                tip.show(res.message);
            }
        }).fail(function() {
            tip.show('网络出了点问题~');
        }).always(function() {
        });
    }
};

module.exports = functions;