cart.js 29.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929
/**
 * @description:  购物车
 * @author: chenglong.wang@yoho.cn
 * @tips: 购物车的商品计价在服务端处理,客户端只要在每次操作后刷新页面重新渲染就可以了
 */

var $ = require('yoho.jquery');
var Handlerbars = require('yoho.handlebars');
var dialog = require('../common/dialog');

var Confirm = dialog.Confirm;
var Alert = dialog.Alert;

var $cartnewSum = $('.cartnew-sum'),
    $cartnewTips = $('.cartnew-tips'),
    $payWapper = $('.pay-wapper'),
    $goodsDetail = $('.goods-detail'),
    $histroy = $('.histroy'),
    $together = $('.together'),
    $shopCart = $('.shop-cart'),
    togetherProductSource,
    togetherProductTemplate,
    togetherProductStr,
    subjoinPage,
    subjoinPageNum,
    subjoinPageAll,
    pageDom,
    selected,
    pageNum = 1,
    delUrl,
    count,
    countJSON,
    dataJSON,
    pacList = 0,
    shopNum = 1,
    shopNumAll,
    productSku,
    promotionId,
    promotionPrice,
    yetCollect,
    collect,
    content,
    productId,
    shopName,
    delNum,
    understock,
    selectArray = [],
    PromotionArray = [],
    perList = false,
    commonList = false,
    hasPromotion = false,
    htmlInfo,
    shopJson,
    oldprice,
    wapperPrice,
    countBusy = false;


// 关闭温馨提示
$cartnewTips.find('.btn_close').click(function() {
    $cartnewTips.fadeOut();
});

// checkbox提交ajax
function choiceOut(data) {
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: '/cart/index/select',
        data: data,
        beforeSend: function() {
            $('.loading').css({
                top: $(document).scrollTop() + 200
            });
            $('.loading').show();
        }
    }).then(function(d) {
        if (d.code === 200) {
            window.history.go(0);
        }
    });
}

//单选提交
$payWapper.one('click', '.common-sell-box input[type="checkbox"], .pre-sell-box input[type="checkbox"]', function() {
    selected = $(this).attr('checked') ? 'Y' : 'N';
    selectArray = [];
    selectArray.push({
        product_sku: $(this).parents('tr').data('id'),
        selected: selected,
        buy_number: $(this).parents('tr').data('productnum'),
        goods_type: $(this).parents('tr').data('goodstype'),
        promotion_id: $(this).parents('tr').data('promotionid') ? $(this).parents('tr').data('promotionid') : 0
    });
    if ($(this).parents('tr').promotion_id) {
        hasPromotion = true;
    } else {
        hasPromotion = false;
    }
    dataJSON = {
        skuList: JSON.stringify(selectArray),
        hasPromotion: hasPromotion
    };
    choiceOut(dataJSON);
});

//多选提交
$cartnewSum.one('click', 'input[type="checkbox"]', function() {
    selectArray = [];
    shopJson = {};
    dataJSON = {};
    selected = $(this).attr('checked') ? 'Y' : 'N';
    $('.pay-wapper:first tr').each(function() {
        if ($(this).data('promotionid')) {
            hasPromotion = true;
        }
        if ($(this).data('id')) {
            selectArray.push({
                product_sku: $(this).data('id'),
                selected: selected,
                buy_number: $(this).data('productnum'),
                goods_type: $(this).data('goodstype'),
                promotion_id: $(this).data('promotionid') ? $(this).data('promotionid') : 0
            });
        }
    });
    dataJSON = {
        skuList: JSON.stringify(selectArray),
        hasPromotion: hasPromotion
    };
    choiceOut(dataJSON);
});

// 滚动到第一个选中的商品
function scrollToFirst() {
    var $selected = $('.pay-wapper tbody tr').find('.cart-item-check:checked');
    var top = 0;

    if ($selected.length > 0) {
        $selected = $selected.eq(0);
        top = $selected.offset().top - 36;
        $('html,body').scrollTop(top);
    }

    return false;
}
function isCheck(obj, className) {
    selectArray = [];
    dataJSON = {};
    selected = obj.attr('checked') ? 'Y' : 'N';
    $('.' + className).each(function() {
        if ($(this).data('promotionid')) {
            hasPromotion = true;
        } else {
            hasPromotion = false;
        }
        if ($(this).data('id')) {
            selectArray.push({
                product_sku: $(this).data('id'),
                selected: selected,
                buy_number: $(this).data('productnum'),
                goods_type: $(this).data('goodstype'),
                promotion_id: $(this).data('promotionid') ? $(this).data('promotionid') : 0
            });
        }
    });
    dataJSON = {
        skuList: JSON.stringify(selectArray),
        hasPromotion: hasPromotion
    };
    choiceOut(dataJSON);
}

$('.per-list').on('click', 'input[type="checkbox"]', function() {
    isCheck($(this), 'pre-sell-box');
});

$('.common-list').on('click', 'input[type="checkbox"]', function() {
    isCheck($(this), 'common-sell-box');
});

// 凑单商品,最近浏览异步加载
togetherProductSource = $('#togetherProduct').html();
togetherProductTemplate = Handlerbars.compile(togetherProductSource);

function getTogetherProduct($obj, url, page) {
    var PRDID = [];

    $.ajax({
        type: 'GET',
        dataType: 'json',
        url: url,
        data: page
    }).then(function(d) {
        if (d.code === 200 && d.data.item.length > 0) {
            $obj.html(' ');
            togetherProductStr = togetherProductTemplate(d.data);
            $obj.append($(togetherProductStr));
            $('#orderProduct li:last').addClass('end');
            $('.gift').removeClass('none');

            //为您优选埋点
            if ($obj.hasClass('givePoint')) {
                $.each(d.data.item, function(key, val) {
                    PRDID.push(val.id);
                });

                window.givePoint({
                    'REC_POSE': 120003,
                    'PRD_ID': PRDID.join(','),
                    'PRD_NUM': d.data.item.length,
                    'ACTION_ID': 0,
                    'page_num': page && page.page ? page.page : 1
                });

                $('.givePoint ul a').unbind('click').bind('click', function() {
                    window.givePoint({
                        'REC_POSE': 120003,
                        'PRD_ID': $(this).closest('li').find('.btn_view_s').data('id'),
                        'PRD_NUM': $(this).closest('li').index() + 1,
                        'ACTION_ID': 1,
                        'page_num': page && page.page ? page.page : 1
                    });
                    return true;
                });
            }
        }
    });
}

// 为你优选
getTogetherProduct($histroy, '/cart/index/getRecommendProduct');

// 凑单商品
getTogetherProduct($together, '/cart/index/getTogetherProduct');

//凑单商品翻页
$together.on('click', '.pagenext, .pageprev', function() {
    getTogetherProduct($together, '/cart/index/getTogetherProduct', {
        page: Math.ceil(Math.random() * 100) + 1
    });

});

//凑单商品,为你优选商品商品折叠
$shopCart.on('click', '.icon-minus, .icon-add', function() {
    if ($(this).hasClass('icon-minus')) {
        $(this).parents('.title').next('.main').hide();
        $(this).parents('h2').next('.right').hide();
    } else {
        $(this).parents('.title').next('.main').show();
        $(this).parents('h2').next('.right').show();
    }
    $(this).siblings('i').toggleClass('none');
    $(this).toggleClass('none');

});

// 最近浏览翻页
$histroy.on('click', '.pagenext, .pageprev', function() {
    if ($(this).hasClass('pagenext')) {
        if (pageNum !== 5) {
            pageNum++;
        } else {
            pageNum = 1;
        }
    } else {
        --pageNum > 0 || (pageNum = 1);
    }
    getTogetherProduct($histroy, '/cart/index/getRecommendProduct', {
        page: pageNum
    });

});


//根据id获取商品信息
function productInfo(data) {
    $.ajax({
        type: 'GET',
        dataType: 'html',
        url: '/cart/index/getProductInfo',
        data: data
    }).then(function(d) {
        pacList = 0;
        $goodsDetail.html(' ');
        $goodsDetail.append(d);
        $goodsDetail.show();
        $('.detail-bigpic:not(.none) .con li:first').addClass('active');

        if ($('.showSizeBox:not(.none) span').length < 2) {
            $('.showSizeBox:not(.none) span:first').addClass('atcive');
        }
        if (Number($('#addToCart').val()) !== 1) {
            $('.showSizeBox span').data('num', 0);
        }
        $.each($('.showSizeBox span'), function() {
            if ($(this).data('num') <= 0) {
                $(this).addClass('null-atcivec');
                console.log($('.showSizeBox:not(.none) span:first'));
                $('.showSizeBox:not(.none) span:first').removeClass('atcive');
            }
        });

        if (typeof promotionPrice !== 'undefined') {
            oldprice = $('.detail-info .oldprice del').html() ? $('.detail-info .oldprice del').html() : wapperPrice;
            htmlInfo = '<span class="oldprice">现价:<del>' + oldprice + '</del></span>' +
                        '<span class="newprice">活动价:<b class="promotion-price">' + promotionPrice + '</b></span>';
            $('.detail-info .price').html(htmlInfo);
        }

        $('.detail-bigpic:not(.none) .bigpic:gt(0)').hide();

        $('.showSizeBox:not(.none)').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;
            }
        });

    });
}

function removeInfo() {
    $goodsDetail.html(' ');
    $goodsDetail.css({
        top: $(document).scrollTop() + 30,
        left: $(document).width() / 2 - $goodsDetail.width() / 2
    });

}

//显示加价购商品信息
$payWapper.on('click', '.cart-add-btn', function() {
    productId = $(this).data('id');
    promotionId = $(this).parents('table').data('promotion');
    promotionPrice = $(this).parents('table').find('.subjoin-price').html();
    wapperPrice = $(this).parents('table').find('.wapper-price').html();
    removeInfo();
    productInfo({
        productId: productId
    });
});

//凑单商品,为你优选商品商品记录
$('.clearfix').on('click', '.btn_view_s', function() {
    productId = $(this).data('id');
    promotionId = $(this).parents('table').data('promotion');
    promotionPrice = $(this).parents('table').find('.subjoin-price').html();
    removeInfo();
    productInfo({
        productId: productId
    });
});

//关闭商品信息窗口
$goodsDetail.on('click', '.colse', function() {
    $goodsDetail.hide();
    $goodsDetail.html(' ');
    shopNum = 1;
});

//展开加价购商品列表
function fold(icon) {
    icon.addClass('none').siblings('i').removeClass('none');
    icon.parents('.cart-product-num').siblings('div').toggleClass('fold');
    icon.siblings('.unfold-color').toggleClass('none');
    icon.siblings('.cart-page').toggleClass('none');
}

$payWapper.on('click', '.icon-minusCart, .icon-addCart', function() {
    fold($(this));
});


$payWapper.on('click', '.unfold-color', function() {
    fold($(this));
    $(this).siblings('.icon-minusCart').removeClass('none');
    $(this).siblings('.icon-addCart').addClass('none');
});

//加价购商品分页
$payWapper.on('click', '.cart-btn-next', function() {
    subjoinPage = $(this).siblings('p').find('.f-red');
    subjoinPageAll = Number($(this).siblings('p').find('.subjoinPageNum').html());
    subjoinPageNum = Number(subjoinPage.html());
    subjoinPage.html(subjoinPageNum + 1);
    if (subjoinPageNum >= subjoinPageAll - 1) {
        $(this).hide();
    } else {
        $(this).siblings('i').show();
    }
    pageDom = $(this).parents('.cart-product-num').siblings('div').find('table');
    pageDom.addClass('none').eq(subjoinPageNum).removeClass('none');
});

$payWapper.on('click', '.cart-btn-pre', function() {
    subjoinPage = $(this).siblings('p').find('.f-red');
    subjoinPageAll = Number($(this).siblings('p').find('.subjoinPageNum').html());
    subjoinPageNum = Number(subjoinPage.html());
    subjoinPage.html(subjoinPageNum - 1);
    if (subjoinPageNum <= 2) {
        $(this).hide();
    } else {
        $(this).siblings('i').show();
    }
    pageDom = $(this).parents('.cart-product-num').siblings('div').find('table');
    pageDom.addClass('none').eq(subjoinPageNum - 2).removeClass('none');
});

//删除购物车商品,把删除的商品移入cookie中
function cartDel(data, delUrl, cookieList) {
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: delUrl,
        data: dataJSON,
        beforeSend: function() {
            $('.loading').css({
                top: $(document).scrollTop() + 200
            });
            $('.loading').show();
        }
    }).then(function(d) {
        if (d.code === 200) {
            if (cookieList) {
                window.setCookie('cart-del-list', JSON.stringify(cookieList), {
                    domain: '.yohobuy.com',
                    path: '/'
                });
            }
            window.history.go(0);
        } else if (d.code === 300) {
            $('.loading').hide();
            new Alert(d.message).show();
        } else if (d.code === 403) {
            if (d.data.url) {
                window.location = d.data.url;
            }
        }
    });
}

//商品信息弹出框,加入收藏夹
$goodsDetail.on('click', '.fav_count', function() {
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: '/product/item/togglecollect',
        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();
        }
    });
});


//删除商品
$payWapper.on('click', '.cart-del-btn', function() {
    dataJSON = {};
    selectArray = [];
    promotionId = $(this).parents('tr').data('promotionid') ? $(this).parents('tr').data('promotionid') : 0;
    selectArray.push({
        product_sku: $(this).parents('tr').data('id'),
        buy_number: $(this).parents('tr').data('productnum'),
        promotion_id: promotionId
    });

    if (promotionId) {
        hasPromotion = true;
    } else {
        hasPromotion = false;
    }

    dataJSON = {
        skuList: JSON.stringify(selectArray),
        hasPromotion: hasPromotion
    };
    content = '<div><span></span>删除商品</div>' +
              '<p>确定从购物车中删除此商品?</p>';
    if (!$(this).data('gift')) {
        countJSON = {
            productPrice: $(this).parents('tr').find('.productPrice').text(),
            productTitle: $(this).parents('tr').find('.pay-pro-info a').text(),
            link: $(this).parents('tr').find('.pay-pro-info a').attr('href'),
            productNum: $(this).parents('tr').data('productnum'),
            productSku: $(this).parents('tr').data('id'),
            promotionId: $(this).parents('tr').data('promotionid')
        };
    }
    delUrl = '/cart/index/remove';
    new Confirm({
        content: content,
        cb: function() {
                cartDel(dataJSON, delUrl, countJSON);
            }
    }).show();
});

//移入收藏夹
$payWapper.on('click', '.cart-remove-btn', function() {
    dataJSON = {};
    selectArray = [];
    promotionId = $(this).parents('tr').data('promotionid') ? $(this).parents('tr').data('promotionid') : 0;
    selectArray.push({
        product_sku: $(this).parents('tr').data('id'),
        buy_number: $(this).parents('tr').data('productnum'),
        promotion_id: promotionId
    });

    if (promotionId) {
        hasPromotion = true;
    } else {
        hasPromotion = false;
    }

    dataJSON = {
        skuList: JSON.stringify(selectArray),
        hasPromotion: hasPromotion
    };

    cartDel(dataJSON, '/cart/index/fav');
});

//批量删除商品
$cartnewSum.on('click', '.delAll', function() {
    dataJSON = {};
    countJSON = {};
    selectArray = [];
    PromotionArray = [];
    $('.pay-wapper:first tr').each(function() {
        if ($(this).find('input[type="checkbox"]').attr('checked')) {
            promotionId = $(this).data('promotionid') ? $(this).data('promotionid') : 0;
            if (promotionId) {
                hasPromotion = true;
            }
            if ($(this).data('id')) {
                if ($(this).find('input[type="checkbox"]').attr('checked')) {
                    selectArray.push({
                        product_sku: $(this).data('id'),
                        buy_number: $(this).data('productnum'),
                        promotion_id: promotionId
                    });
                }
                countJSON = {
                    productPrice: $(this).find('.productPrice').text(),
                    productTitle: $(this).find('.pay-pro-info a').text(),
                    link: $(this).find('.pay-pro-info a').attr('href'),
                    productNum: $(this).data('productnum'),
                    productSku: $(this).data('id'),
                    promotionId: $(this).data('promotionid')
                };
                PromotionArray.push(countJSON);
            }
        }
    });
    delUrl = '/cart/index/remove';
    content = '<div><span></span>删除商品</div>' +
              '<p>确定从购物车中删除所有选中商品?</p>';
    dataJSON = {
        skuList: JSON.stringify(selectArray),
        hasPromotion: hasPromotion
    };
    if (!$.isEmptyObject(selectArray)) {
        new Confirm({
            content: content,
            cb: function() {
                    cartDel(dataJSON, delUrl, PromotionArray);
                }
        }).show();
    } else {
        new Alert('请至少选择一件商品').show();
    }
});

//批量移入收藏夹商品
$cartnewSum.on('click', '.removeAll', function() {
    dataJSON = {};
    countJSON = {};
    selectArray = [];
    $('.pay-wapper:first tr').each(function() {
        if ($(this).find('input[type="checkbox"]').attr('checked')) {
            promotionId = $(this).data('promotionid') ? $(this).data('promotionid') : 0;
            if (promotionId) {
                hasPromotion = true;
            }

            if ($(this).data('id')) {
                if ($(this).find('input[type="checkbox"]').attr('checked')) {
                    selectArray.push({
                        product_sku: $(this).data('id'),
                        buy_number: $(this).data('productnum'),
                        promotion_id: promotionId
                    });
                }
            }
        }
    });
    dataJSON = {
        skuList: JSON.stringify(selectArray),
        hasPromotion: hasPromotion
    };
    if (!$.isEmptyObject(selectArray)) {
        cartDel(dataJSON, '/cart/index/fav');
    } else {
        new Alert('请至少选择一件商品').show();
    }
});

//购物车商品增减
function countAJAX(data) {
    countBusy = true;
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: '/cart/index/modify',
        data: data
    }).then(function(d) {
        if (d.code === 200) {
            window.history.go(0);
        } else {
            new Alert(d.message === '' ? '加入购物车失败哦~~' : d.message).show();
        }
        countBusy = false;
    });
}

$payWapper.on('click', '.minus, .plus', function() {
    countJSON = {};
    count = $(this).hasClass('minus') ? 'decreaseNum' : 'increaseNum';
    countJSON['' + count] = 1;
    if (countBusy || ($(this).siblings('input').val() === '1' && $(this).hasClass('minus'))) {
        return false;
    }

    countAJAX($.extend(countJSON,
        {
            sku: $(this).parents('tr').data('id')
        }
    ));
});

$('.gift').on('click', '.up, .down', function() {
    $(this).siblings('i').toggleClass('none');
    $(this).toggleClass('none');
});

//加入购物车

function addcart(data, cookieList) {
    $.ajax({
        type: 'POST',
        url: '/cart/index/add',
        data: data
    }).then(function(d) {
        if (d.code === 200) {
            window.history.go(0);
            if (cookieList) {
                window.setCookie('cart-del-list', JSON.stringify(cookieList), {
                    domain: '.yohobuy.com',
                    path: '/'
                });
            }
        } else {
            new Alert(d.message === '' ? '加入购物车失败哦~~' : d.message).show();
        }
    });
}

$goodsDetail.on('click', '.addcart', function() {
    if ($('.showSizeBox:not(.none) .atcive').length !== 0) {
        shopNumAll = $('.showSizeBox:not(.none) .atcive').data('num');
        productSku = $('.showSizeBox:not(.none) .atcive').data('sku');
        dataJSON = {
            productSku: productSku,
            buyNumber: $('#mnum').val(),
            promotionId: promotionId
        };
    } else {
        new Alert('请选择尺码').show();
        return false;
    }

    if ($('#mnum').val() > shopNumAll) {
        new Alert('库存不足,目前还有' + shopNumAll + '个库存').show();
    } else {
        if (Number($('#addToCart').val()) === 1) {
            addcart(dataJSON);
        } else {
            new Alert('该商品无法加入购物车').show();
        }
    }
});

$goodsDetail.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;
        }

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

});

$goodsDetail.on('click', '.color', function() {
    pacList = 0;
    $('.color').find('p').removeClass('atcive');
    $(this).find('p').addClass('atcive');
    $('.showSizeBox').addClass('none').eq($(this).index()).removeClass('none');
    if ($('.showSizeBox:not(.none) span').length < 2) {
        $('.showSizeBox:not(.none) span:first').addClass('atcive');
    }
    $('.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;
        }
    });
});

$goodsDetail.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('atcive');
        $('.addcart').removeClass('none');
        $('.btn_sellout').addClass('none');
    } else {
        $('.showSizeBox:not(.none) span').removeClass('atcivec atcive').eq($(this).index()).addClass('atcivec');
        $('.addcart').addClass('none');
        $('.btn_sellout').removeClass('none');
    }
});

//撤销删除
$('.goBack').on('click', function() {
    dataJSON = {};
    delNum = $('.dev-revocation .goBack').index(this);
    selectArray = [];
    $('.dev-revocation tr').each(function(index) {
        if (delNum !== index) {
            countJSON = {
                productPrice: $(this).find('.productPrice').text(),
                productTitle: $(this).find('.title').text(),
                link: $(this).find('.title').attr('href'),
                productNum: $(this).data('productnum'),
                productSku: $(this).data('productsku'),
                promotionId: $(this).data('promotionid')
            };
            selectArray.push(countJSON);
        }
    });
    dataJSON = {
        productSku: $(this).parents('tr').data('productsku'),
        promotionId: $(this).parents('tr').data('promotionid'),
        buyNumber: $(this).parents('tr').data('productnum')
    };
    addcart(dataJSON, selectArray);
});

$goodsDetail.on('click', '.piclist li', function() {
    pacList = $(this).index();
    $('.detail-bigpic:not(.none) .piclist li').removeClass('active');
    $(this).addClass('active');
    $('.detail-bigpic:not(.none) .bigpic').hide().eq($(this).index()).show();
});

function isSollect() {
    $('.gift-wrapper .fold').show();
    if ($('.pre-sell-box input:checked').length === $('.pre-sell-box input[type="checkbox"]').length) {
        $('.per-list input[type="checkbox"]').attr('checked', true);
    }
    if ($('.common-sell-box input:checked').length === $('.common-sell-box input[type="checkbox"]').length) {
        $('.common-list input[type="checkbox"]').attr('checked', true);
    }
    if ($('.common-list input[type="checkbox"]').attr('checked')) {
        commonList = true;
    }
    if ($('.per-list input[type="checkbox"]').attr('checked')) {
        perList = true;
    }
    if ($('.per-list').length !== 0 && $('.common-list').length !== 0) {
        if (commonList && perList) {
            $('.batch_div input[type="checkbox"]').attr('checked', true);
        }
    } else {
        if (commonList || perList) {
            $('.batch_div input[type="checkbox"]').attr('checked', true);
        }
    }

    $('.cart-page').each(function() {
        if ($(this).data('pageall') === 1) {
            $('.cart-btn-pre', this).hide();
            $('.cart-btn-next', this).hide();
        } else {
            $('.cart-btn-pre', this).hide();
        }
    });

    if ($('.pre-sell-box input:checked').length > 0 && $('.common-sell-box input:checked').length > 0) {
        $('.go_cash_tips').show();
        $('.btn_account_disabled').show();
        $('.btn_account').hide();
    } else {
        $('.go_cash_tips').hide();
        $('.btn_account').show();
        $('.btn_account_disabled').hide();
    }

    $('.pay-wapper tr').each(function() {
        if ($(this).data('pid')) {
            selectArray.push($(this).data('pid') + '');
        }

    });
    $.ajax({
        type: 'POST',
        url: '/cart/index/checkFav',
        data: {
            pidList: JSON.stringify(selectArray)
        }
    }).then(function(d) {
        yetCollect = '<span class="cart-yetremove-btn">已收藏</span>';
        collect = '<a href="javascript:void(0);" class="cart-remove-btn"><span>移入收藏</span></a>';
        $.each(d.data, function(key, val, index) {
            $('.pay-wapper tr').each(function() {
                if (Number($(this).data('pid')) === Number(key)) {
                    if (val) {
                        $(this).find('.cart-operation').append(yetCollect);
                    } else {
                        $(this).find('.cart-operation').append(collect);
                    }
                }
            });
        });
    });
}

isSollect();

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

//结算
$('.btn_account').on('click', function() {
    understock = '';
    if ($('.pay-wapper input:checked').parents('tr').find('.tipNoStore').length > 0) {
        shopName = $('.pay-wapper input: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 Alert('您有赠品没有选择,请选择完再结算!').show();
        } else {
            if ($('input:checked').length > 0) {
                if ($('.pre-sell-box input:checked').length > 0) {
                    window.location.href = '/cart/index/orderEnsure?type=2';
                } else {
                    window.location.href = '/cart/index/orderEnsure?type=1';
                }
            } else {
                new Alert('请至少选择一件商品').show();
            }
        }
    }
});

$payWapper.on('selectstart', function() {
    return false;
});
$goodsDetail.on('selectstart', function() {
    return false;
});

scrollToFirst();