cart.page.js 7.15 KB
/**
 * 订单结算页
 * @author: gxh<xuhui.ge@yoho.cn>
 * @date: 2016/12/27
 */

var $ = require('yoho-jquery');

var lazyLoad = require('yoho-jquery-lazyload');
var Dialog = require('../common/dialog').Dialog;
var Alert = Dialog.Alert;
var $cartGoodsDetail = $('#goodsTogetherDetail'),
    $goodsChoose = $('#goodsChoose'),
    $goodsItem = $('.cart-table li'),
    pacList = 0,
    shopNum = 1,
    productId,
    promotionId,
    shopNumAll;

// 优惠券 显示和隐藏
$('.sale-info').mouseenter(function() {
    $(this).addClass('active').children('ul').removeClass('none');
}).mouseleave(function() {
    $(this).removeClass('active').children('ul').addClass('none');
});

// 推荐、优选、浏览切换
$('.bottom-tab-slide').on('click', '.bottom-title', function() {
    var $this = $(this),
        index = $this.index();
    console.log(index);
    var $togetherGoods = $('.individual-item-togetherGoods'),
        $latestWalk = $('.individual-item-latestWalk'),
        $recommendGoods = $('.individual-item-recommendGoods');

    if ($this.hasClass('cur')) {
        return;
    }

    $this.addClass('cur');
    $this.siblings('.cur').removeClass('cur');

    switch (index) {
        case 0: {

            $togetherGoods.removeClass('none');
            $recommendGoods.addClass('none');
            $latestWalk.addClass('none');
            break;
        }
        case 1: {
            $togetherGoods.addClass('none');
            $recommendGoods.removeClass('none');
            $latestWalk.addClass('none');
            break;
        }
        case 2: {
            $togetherGoods.addClass('none');
            $recommendGoods.addClass('none');
            $latestWalk.removeClass('none');
            break;
        }

    }

});


// 凑单图片懒加载
var togetherDialog = {};
var dialogTpl = require('hbs/cart/cart-togetherGoods.hbs');

lazyLoad($('#details-html img'));

$('.shop-cart').on('click', '.gift-sell .btn-clear', function() {

    var tid = $(this).attr('data-together-id');
    showTogetherGoods(tid);

});

// 凑单
function showTogetherGoods(id) {

    if (!id) {
        return;
    }

    var dialog = togetherDialog[id];
    if (!dialog) {
        dialog = new Dialog({
            content: dialogTpl(),
            className: 'cart-togetherGoods'
        });

        togetherDialog[id] = dialog;
    }

    dialog.show();

}

// 凑单弹框 详情切换
$('body').on('click', '.piclist li', function() {
    console.log(99999);
    pacList = $(this).index();
    $('.detail-bigpic:not(.none) .piclist li').removeClass('active');
    $(this).addClass('active');
    $('.detail-bigpic:not(.none) .bigpic').hide().eq($(this).index()).show();
});

$('body').on('click', '.pre, .next', function() {
    if ($(this).hasClass('next')) {
        if (pacList >= $(this).siblings('.con').find('li').length - 1) {
            return false;
        }
        pacList++;
    } else {
        if (pacList < 1) {
            return false;
        }
        pacList--;
    }
    $(this).siblings('.con').find('li').removeClass('active').eq(pacList).addClass('active');
    $('.detail-bigpic:not(.none) .bigpic').hide().eq(pacList).show();
});
$('body').on('click', '.color', function() {
    pacList = 0;
    $('.color').find('p').removeClass('active');
    $(this).find('p').addClass('active');
    $('.showSizeBox').addClass('none').eq($(this).index()).removeClass('none');
    if ($('.showSizeBox:not(.none) span').length < 2) {
        $('.showSizeBox:not(.none) span:first').addClass('active');
    }
    $('.detail-bigpic').addClass('none').eq($(this).index()).removeClass('none');
    $('.detail-bigpic').eq($(this).index()).find('.bigpic').hide();
    $('.detail-bigpic').eq($(this).index()).find('.bigpic:first').show();
    $('.detail-bigpic').eq($(this).index()).find('.con li').removeClass('active');
    $('.detail-bigpic').eq($(this).index()).find('.con li:first').addClass('active');
    $('.showSizeBox').eq($(this).index()).find('span').each(function() {
        if ($(this).hasClass('null-atcivec')) {
            $('.addcart').addClass('none');
            $('.btn_sellout').removeClass('none');
        } else {
            $('.addcart').removeClass('none');
            $('.btn_sellout').addClass('none');
            return false;
        }
    });
});

$('body').on('click', '.showSizeBox span', function() {
    shopNumAll = $(this).data('num');
    if ($(this).data('num') > 0) {
        $('.showSizeBox:not(.none) span').removeClass('atcivec atcive').eq($(this).index()).addClass('active');
        $('.addcart').removeClass('none');
        $('.btn_sellout').addClass('none');
    } else {
        $('.showSizeBox:not(.none) span').removeClass('atcivec atcive').eq($(this).index()).addClass('active');
        $('.addcart').addClass('none');
        $('.btn_sellout').removeClass('none');
    }
});

$('body').on('click', '.cut, .add', function() {
    count = $(this).hasClass('minus') ? 'decreaseNum' : 'increaseNum';
    if (promotionId) {
        if ($(this).hasClass('cut')) {
            new Alert('-_-,已经是最后一件,不能再减了!').show();
        } else {
            new Alert('最多只能购买一件,您好像购买的太多了!').show();
        }
    } else {
        if ($(this).hasClass('add')) {
            shopNum++;
        } else {
            shopNum--;
        }
        if (shopNum < 1) {
            new Alert('-_-,已经是最后一件,不能在减了!').show();
            shopNum = 1;
            return false;
        }

        $('#num').val(shopNum);
    }

});

// 凑单弹框 商品信息弹出框,加入收藏夹
$cartGoodsDetail.on('click', '.btn-favCount', function() {
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: '',
        data: {
            productId: productId,
            type: 'add'
        }
    }).then(function(d) {
        if (d.code === 200) {
            new Alert(d.message).show();
        } else if (d.code === 403) {
            if (d.data.url) {
                window.location = d.data.url;
            }
        } else {
            new Alert(d.message).show();
        }
    });
});


// 购物车列表 显示隐藏
$goodsItem.on('click', '.pay-pro-info span', function() {
    $('.pay-pro-info').find('span').removeClass('active');
    $(this).addClass('active');
    $('.goods-choose-box').addClass('none');
    $(this).parents('li').children('.goods-choose-box').removeClass('none');
});

// 购物车列表 修改 颜色
$goodsChoose.on('click', '.choose-color .dt', function() {
    pacList = $(this).index();
    $('.choose-color').find('.dt').removeClass('active');
    $(this).addClass('active');
    $('.bigImg').addClass('none').eq($(this).index()).removeClass('none');
});



var giftBtn = '[data-role=gift-view-btn],[data-role=gift-resel-btn],[data-role=gift-sel-btn]';

$('.shop-cart').on('click', giftBtn, function() {
    var mtext = $(this).closest('[data-role=promotion-wrap]').find('textarea[data-role=gifts-cansel]').text();
    var mhtml = $(this).closest('[data-role=promotion-wrap]').find('textarea[data-role=gifts-cansel]').html();
    console.log(mtext);
    console.log(mhtml);
    var textJson = $.parseJSON(mtext);
    var htmlJson = $.parseJSON(mhtml);

    console.log(textJson);
    console.log(htmlJson);

});