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

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

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

var Dialog = dialog.Dialog,
    Alert = dialog.Alert,
    $cartGoodsDetail = $('#goodsTogetherDetail'),
    $sellBox = $('.cart-table .pre-sell-box');

$('.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-recommendGoods'),
        $latestWalk = $('.individual-item-latestWalk');

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

    $('.bottom-tab-slide').on('click', '.bottom-title', function() {
        var $this = $(this),
            index = $this.index();
        console.log(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;
            }
        }
    });
});


// 凑单 底部 轮播
function loadSlideGoods() {
    $.ajax({
        type: 'GET',
        url: '',
        data: {
            // skn: skn,
            size: 20,
            num: 1
        }
    }).then(function(data) {
        // 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 isCheckAll() {

    var isAllCheck = true;
    $('[data-role=pitem] [data-role=cart-item-check]').each(function() {

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

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

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

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

    $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;
                    }
                });
            }

        });
    });
}


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


    isCheckAll();
    isCheckFav();
});

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

// 凑单弹框 商品信息弹出框,加入收藏夹
$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();
        }
    });
});

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