bundle.page.js 5.53 KB
/**
 * Created by targaryen on 2016/11/28.
 */
require('scss/product/bundle/detail.page.scss');

let $ = require('yoho-jquery'),
    tip = require('js/plugin/tip'),
    Swiper = require('yoho-swiper');

require('js/common');
let share = require('js/common/share');

let chosePanel = require('js/common/chose-panel-new');

let $selectBtn = $('.select-btn'),
    $buyNowBtn = $('#bundle-buy-now');

// cookie 参数
let actCkOpthn = {
    path: '/cart/index'
};

$(function() {
    window.setCookie('activity-info', '', actCkOpthn);
    if ($('#shareUrl').length) {
        share({
            imgUrl: $('#shareImgUrl').val(),
            title: decodeURIComponent($('#shareTitle').val()),
            link: $('#shareUrl').val(),
            desc: decodeURIComponent($('#shareSubTitle').val())
        });
    }
    if ($('.bundle-tabs').find('.swiper-slide').length > 3) {
        new Swiper('.bundle-tabs', {
            freeMode: true,
            slidesPerView: 'auto',
            lazyLoading: true
        });
    }
});

/**
 * 选择商品颜色等
 */
$selectBtn.on('touchend', function() {
    let self = $(this);

    $.get('/product/detail/newinfo', {
        id: self.data('id'),
        productSkn: self.data('skn')
    }, function(data) {
        chosePanel.show({
            data,
            disableNum: true,
            buttonText: {
                text: '确认'
            }
        }).then(result => {
            if (result && result.sku) {
                let product = $('#gskn-' + self.data('skn'));
                let colorName = result.sku.prop.color.valName;

                colorName = colorName.substring(0, 4) + '...';

                product.data('sku', result.sku.skuId);
                product.html('颜色:' + colorName + ' ' +
                result.sku.prop.size.skuTitle + ':' + result.sku.prop.size.valName);
            }
        }, () => {});
    });
});

$('.goods-img').on('touchend', function() {
    let packGood = $(this).parent().find('.select-btn');

    if (window._yas && window._yas.sendCustomInfo) {
        window._yas.sendCustomInfo({
            op: 'YB_GDS_PACKAGE_GOODS_C',
            param: JSON.stringify({
                C_ID: window._ChannelVary[window.cookie('_Channel')],
                PRD_ID: window.queryString.productId,
                TAB_ID: window.queryString.index || 1,
                PACKAGE_GOODS: packGood.data('id'),
                PACKAGE_INDEX: packGood.data('index') + 1
            })
        }, true);
    }

});

$('.bundle-tabs').on('click', '.swiper-slide', (e) => {
    let $tab = $(e.currentTarget);

    if ($tab.hasClass('selected')) {
        return;
    }
    if (window._yas && window._yas.sendCustomInfo) {
        window._yas.sendCustomInfo({
            op: 'YB_GDS_PACKAGE_TAB_C',
            param: JSON.stringify({
                C_ID: window._ChannelVary[window.cookie('_Channel')],
                PRD_ID: window.queryString.productId,
                TAB_ID: $(e.currentTarget).data('index') + 1,
                PACKAGE_DETAIL: 2
            })
        }, true);
    }
    let href = $tab.data('href');

    window.location.href = href;
});

/**
 * 立即购买
 * 大概 4 月份改成了加入购物车
 */
$buyNowBtn.on('touchstart', function() {
    let bundleGoods = [],
        bundleId = $('#activityId').val(),
        gskn = $('span[id^="gskn"]'),
        gsknSelected = false,
        yasGid = [];

    if (gskn.length < 1) {
        tip.show('非法请求!');
        return;
    }

    gskn.each(function() {
        if (!$(this).data('sku')) {
            gsknSelected = false;
            tip.show('请为套餐商品选择属性');
            return;
        }

        gsknSelected = true;

        bundleGoods.push({
            type: 'bundle',
            sku: $(this).data('sku'),
            buy_number: 1
        });

        yasGid.push($(this).data('id'));
    });

    if (window._yas && window._yas.sendCustomInfo) {
        let id = [],
            skn = [],
            num = [],
            sku = [];

        $('.goods-info .select-btn').each(
            function() {
                skn.push($(this).data('skn'));
                id.push($(this).data('id'));
                sku.push($(this).data('sku'));
                num.push(1);
            }
        );
        window._yas.sendCustomInfo({
            op: 'YB_GDS_DT_ADD_TO_SC',
            param: JSON.stringify({
                C_ID: window._ChannelVary[window.cookie('_Channel')],
                PRD_ID: id.join(','),
                PRD_NUM: num.join(','),
                PRD_SKN: skn.join(','),
                PRD_SKU: sku.join(','),
                POS_ID: 101
            })
        }, true);
    }

    if (gsknSelected) {
        // window.setCookie('_cartType', 'bundle', actCkOpthn);
        // window.setCookie('activity-info', JSON.stringify({
        //     product_sku_list: bundleGoods,
        //     activity_id: bundleId
        // }), actCkOpthn);

        $.ajax({
            method: 'post',
            url: '/product/bundle/cart/add',
            data: {
                product_sku_list: JSON.stringify(bundleGoods),
                activity_id: bundleId
            },
            success: function(result) {
                tip.show(result.message);

                // if (result.code === 200) {
                //     setTimeout(function() {
                //         window.location.href = '/cart/index/index?cartType=ordinary';
                //     }, 500);
                // }
            },
            error: function() {
                tip.show('请求失败,请重试!');
            }
        });
    }
});