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

let functions = {
    /**
     * 添加门票
     */
    addTickets(productSku, buyNumber) {
        let 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) {
        if (!yoho.isLogin()) {
            let preInfo = `${sku}_${skn}_${buyNum}`;
            let actCkOpthn = {
                path: '/',
                expires: 1
            };

            window.setCookie('tmp-cart-info', preInfo, actCkOpthn);
            window.location.href = '//m.yohobuy.com/signin.html?refer=' + encodeURIComponent(window.location.href);
            return false;
        }
        if (window._yas && window._yas.sendCustomInfo) {
            window._yas.sendCustomInfo({
                op: 'YB_GDS_DT_ADD_TO_SC',
                param: JSON.stringify({
                    C_ID: window._ChannelVary[window.cookie('_Channel')],
                    PRD_ID: $('#productId').val(),
                    PRD_NUM: buyNum,
                    PRD_SKN: skn,
                    PRD_SKU: sku
                })
            }, true);
        }
        $.ajax({
            method: 'POST',
            url: '//m.yohobuy.com/cart/index/add',
            data: {
                productSku: sku,
                new_product_skn: skn,
                buyNumber: buyNum
            },
            xhrFields: {
                withCredentials: true
            },
        }).done(function(res) {
            let cartNum;

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

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

module.exports = functions;