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

var $ = require('yoho-jquery'),
    lazyLoad = require('yoho-jquery-lazyload'),
    dialog = require('../common/dialog');

var Alert = dialog.Alert;

var $cartGoodsDetail = $('#goodsTogetherDetail'),
    $paySum = $('#pay-sum'),
    shopNum = 1,
    productId,
    promotionId,
    $sellBox = $('.cart-table .pre-sell-box');

require('../plugins/slider');
require('../common/center-slider');
require('../common');

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

$('.side-img-dd').slider2({
    shownum: 5,
    isCircle: true
});

// var dialogTpl = require('hbs/cart/cart-togetherGoods.hbs');

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


// ----------------底部 为你优选,最近浏览 begin--------------

// 推荐、优选、浏览切换
$(function() {
    var $togetherGoods = $('.individual-item-togetherGoods'),
        $recommendGoods = $('.individual-item-recommend-goods'),
        $latestWalk = $('.individual-item-latestwalk');

    if ($togetherGoods.hasClass('none')) {
        $recommendGoods.show();
    }

    $('.bottom-tab-slide').on('click', '.bottom-title', function() {
        var $this = $(this),
            index = $this.index();

        $togetherGoods.hide();
        $recommendGoods.hide();

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

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

        switch (index) {
            case 0: {
                $togetherGoods.slideDown();
                $recommendGoods.hide();
                $latestWalk.hide();
                break;
            }
            case 1: {
                $togetherGoods.hide();
                $recommendGoods.slideDown();
                $latestWalk.hide();
                break;
            }
            case 2: {
                $togetherGoods.hide();
                $recommendGoods.hide();
                $latestWalk.slideDown();
                break;
            }
            default: {
                break;
            }
        }
    });
});


// 凑单 底部 轮播
function loadSlideGoods() {
    $.ajax({
        type: 'GET',
        url: '',
        data: {
            // skn: skn,
            size: 20,
            num: 1
        }
    }).then(function(data) { //eslint-disable-line
        // var pro = data.data.products;

        // if (data.code === 200) {
        //     if (pro.length === 0) {
        //         return;
        //     }

        $('#recommend-shop').removeClass('none');

        $('.individual-item ').slider2({
            shownum: 5,
            isCircle: true
        });

        // } else {
        //         $('.bottom-title').filter('.change').addClass('none');
        // }
    });
}
loadSlideGoods();

$(function() {
    $('.bottom-tab .change').click(function() {
        $('.img-brand-switch .next').trigger('click');
    });

    // 判断商品是否已收藏,是否全选
    function isCheckFav() {
        var pidArray = [],
            isAllCheck = true;

        $sellBox.each(function() {
            var $checkItem = $(this).find('.cart-item-check');

            // 有一个没选中,跳出循环
            if (!$checkItem.hasClass('cart-item-checked')) {
                isAllCheck = false;
                return false;
            }
        });

        if (isAllCheck) {
            // 头部全选
            $('.cart-title .cart-item-check').addClass('cart-item-checked');

            // 底部全选
            $('#cbSelAllGoods').addClass('cart-item-checked');
        }

        $sellBox.each(function() {
            pidArray.push($(this).data('pid'));
        });

        $.ajax({
            type: 'POST',
            url: '/cart/index/checkFav',
            data: {
                pidList: pidArray.join(',')
            }
        }).then(function(result) {
            $.each(result.data, function(pid, obj) {

                if (obj.code !== 200) {
                    return false;
                }

                if (obj.data) {
                    $sellBox.each(function() {

                        if (String($(this).data('pid')) === String(pid)) {
                            $(this).find('.cart-col-btn').addClass('has-col-btn').html('已收藏');
                            return false;
                        }
                    });
                }

            });
        });
    }

    isCheckFav();
});

$('body').on('click', '.cut, .add', function() {
    // var 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-fav-count', 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();
        }
    });
});

// 结算
$paySum.on('click', '.btn-account', function() {
// 结算
    // var understock = '';

    // if ($('.pay-wapper .cart-item-checked').parents('.tr').find('.tipNoStore').length > 0) {
    //     shopName = $('.pay-wapper .cart-item-checked').parents('.tr').find('.tipNoStore');
    //     $.each(shopName.parents('.tr').find('.pay-pro-info a'), function() {
    //         understock += $(this).html();
    //     });
    //     new Alert(understock + '库存不足').show();
    // } else {
    //     if ($('.zp').length > 0 && !$(this).attr('title')) {
    //         $(this).attr('title', '1');

    // new Confirm({
    //     className: 'shopCart-del-dialog',
    //     content: '<h4><i class="iconfont">&#xe6c2;</i>您还未选择赠品</h4>' +
    //     '<small>是否去选择赠品?</small>',
    // }).show();

    //     } else {
    //         if ($('input:checked').length > 0) {
    //
    //             if ($('.pre-sell-box .cart-item-checked').length > 0) {
    //                 window.location.href = '/cart/index/orderEnsure?type=2';
    //             } else {
    //                 window.location.href = '/cart/index/orderEnsure?type=1';
    //             }
    //         } else {

    // new Alert('请至少选择一件商品').show();

    //         }
    //     }
    // }
    $('.cart-presell-dialog').show();

    // $('.togethor').show();

    $('.cart-together-goods').show();

});

$('.shop-cart').on('click', '[data-role="cart-gift-win"] .close', function() {
    $(this).closest('[data-role="cart-gift-win"]').hide();
});

require('./cart-action');
require('./cart-color-panel');
require('./cart-goods-win');