cart-api.js 10.4 KB
/**
 * Created by yoho on 2017-01-05.
 */

var $ = require('yoho-jquery');
var dialog = require('../common/dialog');
var Alert = dialog.Alert,
    CART_ITEM_DEL_URL = '/cart/index/remove',
    CART_ITEM_FAV_URL = '/cart/index/fav';

// checkbox提交ajax
function choiceOut(items) {

    var skuList = $.isArray(items) ? items : [items];
    var hasPromotion = false;

    $.each(skuList, function(idx, it) {

        if (it.promotion_id) {
            hasPromotion = true;
            return false;
        }
    });

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

/*
 * 1. 删除购物车商品,把删除的商品移入cookie中
 * 2. 移到收藏夹
 * data: 数据
 * tpe: true - 删除,默认 移入收藏夹
 */
function cartItemDel(items, type, cookieList) {

    var selList = $.isArray(items) ? items : [items];
    var hasPromotion = false;

    $.each(selList, function(idx, it) {
        if (it.promotion_id) {
            hasPromotion = true;
            return false;
        }
    });

    return $.ajax({
        type: 'POST',
        dataType: 'json',
        url: type === true ? CART_ITEM_DEL_URL : CART_ITEM_FAV_URL,
        data: {
            skuList: JSON.stringify(selList),
            hasPromotion: hasPromotion
        },
        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;
            }
        }
    });
}

// 购物车商品增减
var cartItemNumChg = (function() {

    var countBusy = false;   // 保证一次只请求完成前不能再次发起

    return function(data) {

        if (countBusy) {
            return;
        }

        countBusy = true;
        $.ajax({
            type: 'POST',
            dataType: 'json',
            url: '/cart/index/modifyNum',
            data: data
        }).then(function(d) {

            if (d.code === 200) {
                window.history.go(0);
            } else {
                new Alert(d.message === '' ? '加入购物车失败哦~~' : d.message).show();
            }
            countBusy = false;
        });
    };
})();

function getProductInfo(pid, skn) {
    return $.ajax({
        type: 'GET',
        url: '/cart/index/getProductData',  // '/product/item/getProductInfo',
        data: {
            productId: pid,
            skn: skn
        }
    }).done(function(res) {
        return res;
    });
}

// 加入购物车,弹出框中加入购物车
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();
        }
    });
}

function parseProductInfo(productInfo, defaultInfo) {

    var index = 0;
    var colors;
    var colorsLen;
    var color;

    // 前端处理后的集合
    var filterSet = [];

    var sizeIdx;
    var curColor;
    var curSize;
    var hasActiveColor = false;
    var defaultColor = defaultInfo.color;
    var defaultSize = defaultInfo.size;
    var defaultImg;

    // 没有res.code
    if (productInfo.colors) {
        // 获取成功
        colors = productInfo.colors;
        colorsLen = colors.length;
        for (index; index < colorsLen; index++) {
            color = colors[index];

            // 迭代每一种颜色
            filterSet.push({
                pid: productInfo.productId,
                skn: productInfo.skn,
                name: color.name,
                src: color.src,
                focus: color.focus,
                title: color.title,
                sizes: color.size,
                pic: color.thumbs[0].shower,
                selectable: color.total > 0
            });
        }
    }

    // 默认选中用户选择的sku,若已售罄或下架,则选中列表中第一个非售罄的sku
    for (index = 0; index < filterSet.length; index++) {
        curColor = filterSet[index];
        if (!hasActiveColor && String(curColor.name) === String(defaultInfo.color)) {
            curColor.active = true;
            curColor.hasActiveColor = hasActiveColor = true;

            defaultImg = curColor.pic;
        }

        curSize = curColor.sizes;

        for (sizeIdx = 0; sizeIdx < curSize.length; sizeIdx++) {

            if (curColor.hasActiveColor && curSize[sizeIdx].sku === defaultInfo.sku) {
                // console.log(curSize[sizeIdx]);
                curSize[sizeIdx].sizeActive = true;
                break;
            }
        }
    }

    // 若无对应颜色,则选中第一个颜色
    if (!hasActiveColor) {
        filterSet[0].active = true;
        defaultColor = filterSet[0].color;

        defaultImg = filterSet[0].pic;
    }

    return {
        skn: productInfo.skn,
        colors: filterSet,
        defaultColor: defaultColor,
        defaultSize: defaultSize,
        defaultImg: defaultImg
    };
}

function updateCartItem(newSku, oldSku) {

    $.ajax({
        type: 'POST',
        url: '/cart/index/updateProduct',
        data: {
            swapData: JSON.stringify([{
                buy_number: '1',
                selected: 'Y',
                new_product_sku: newSku,
                old_product_sku: oldSku
            }])
        }
    }).then(function(d) {
        if (d.code === 200) {
            window.history.go(0);
        } else {
            new Alert(d.message === '' ? '修改商品失败哦~~' : d.message).show();
        }
    });
}

function updateCartGiftItem(promotionId, newSkn, newSku) {

    $.ajax({
        type: 'POST',
        url: '/cart/index/swapGift',
        data: {
            promotionId: promotionId,
            newSkn: newSkn,
            newSku: newSku
        }
    }).then(function(d) {
        if (d.code === 200) {
            window.history.go(0);
        } else {
            new Alert(d.message === '' ? '修改商品失败哦~~' : d.message).show();
        }
    });
}

function getProductByPromotionId(promotionId) {
    return $.ajax({
        type: 'GET',
        url: '/cart/index/queryPromotionGift',
        data: {
            promotionId: promotionId
        }
    }).done(function(res) {
        return res;
    });
}



function getFineProduct(page) {
    return $.ajax({
        type: 'GET',
        dataType: 'json',
        url: '/cart/data/recommendProduct',
        data: page
    }).then(function(d) {

        // 为您优选埋点
        /* 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;
            });
        }*/

        return d;
    });
}
function getTogetherProduct(page) {
    // var PRDID = [];

    return $.ajax({
        type: 'GET',
        dataType: 'json',
        url: '/cart/data/togetherProduct',
        data: page
    }).then(function(d) {
        return 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;
                });
            }
        }*/
    });
}

module.exports = {
    addcart: addcart,
    getProductInfo: getProductInfo,
    choiceOut: choiceOut,
    cartItemDel: cartItemDel,
    cartItemNumChg: cartItemNumChg,
    parseProductInfo: parseProductInfo,
    updateCartItem: updateCartItem,
    updateCartGiftItem: updateCartGiftItem,
    getProductByPromotionId: getProductByPromotionId,
    getFineProduct: getFineProduct,
    getTogetherProduct: getTogetherProduct
};