cart.js 9.35 KB
/**
 * 购物车模型 model
 * @author: jinhu.dong<jinhu.dong@yoho.cn>
 * @date: 2016/07/04
 */
'use strict';

var _ = require('lodash');

const api = global.yoho.API;

/*
 * 加入购物车
 * @function addToCart
 * @param { String } productSku 商品SKU
 * @param { Number } buyNumber 购买商品数量
 * @param { Number } goodType 商品类型,0表示普通商品,1表示加价购商品
 * @param { Number } isEdit 是否是编辑商品SKU,0表示不是编辑
 * @param { Number } uid 用户UID,可以不传
 * @param { String } shoppingKey 未登录用户唯一识别码,可以不传
 * @return { Array } 加入购物车接口返回的数据
 */
const addToCart = (options) => {
    const productSku = options.productSku;
    const buyNumber = options.buyNumber;
    const goodType = options.goodType || 0;
    const isEdit = options.isEdit || 0;
    const uid = options.uid;
    const shoppingKey = options.shoppingKey;

    let params = {
        method: 'app.Shopping.add',
        product_sku: productSku,
        buy_number: buyNumber,
        goods_type: goodType,
        edit_product_sku: isEdit,
        selected: 'Y',
        promotion_id: null
    };

    if (uid) {
        _.merge(params, {
            uid: uid
        });
    }

    if (shoppingKey) {
        _.merge(params, {
            shoppingKey: shoppingKey
        });
    }

    return api.get('', params);
};

/*
 * 获取购物车数据
 * @function getCartData
 * @param { Number } uid 用户UID,可以不传
 * @param { String } shoppingKey 未登录用户唯一识别码,客户端从cookie中获取
 * @return { Object }
 * {
 *     advance_cart_data: {                  // 预售商品
 *         gift_list: [],                    // 赠品
 *         goods_list: []                    // 商品列表
 *         off_shelves_goods_list: [],       // 下架商品, 失效商品
 *         price_gift: [],                   // 加价购商品
 *         promotion_info: [],               // 促销信息
 *         shopping_cart_data: [],           // 商品价格计算相关信息
 *         sold_out_goods_list: []           // 售罄商品
 *     },
 *     ordinary_cart_data: {                 // 普通商品
 *         gift_list: [],
 *         goods_list: [],
 *         off_shelves_goods_list: [],
 *         price_gift: [],
 *         promotion_info: [],
 *         shopping_cart_data: [],
 *         sold_out_goods_list: []
 *     },
 *     md5: '...',
 *     message: '...'
 * }
 */
const getCartData = (shoppingKey, uid) => {
    console.log('SPK:', shoppingKey);
    console.log('uid:', uid);
    let params = {
        method: 'app.Shopping.cart'
    };

    if (shoppingKey) {
        _.merge(params, {
            shopping_key: shoppingKey
        });
    }

    if (uid) {
        _.merge(params, {
            uid: uid
        });
    }

    return api.get('', params);
};

/*
 * 移入收藏夹
 * @function transferToFavorite
 * @param { Number } uid 用户UID
 * @param { String } sku 商品sku列表
 * @param { Boolean } hasPromotion 是否有促销ID, 默认值是false
 * @return { Array }
 */
const transferToFavorite = (uid, sku, hasPromotion) => {
    let method = hasPromotion ? 'app.Shopping.addfavoriteAndCart' : 'app.Shopping.addfavorite';

    let params = { method };

    _.merge(params, {
        product_sku_list: sku,
        uid // 用户必须登录吗 TODO
    });

    return api.get('', params);
};

/*
 * 商品选择与取消选择
 * @function toggleSelectGoods
 * @param { Number } uid 用户UID
 * @param { String } sku 商品sku列表
 * @param { String } shoppingKey 未登录用户唯一识别码,可以不传
 * @return { Array }
 */
const toggleSelectGoods = (params) => {
    // BLK 没有促销, 新接口
    let method = 'app.Shopping.selectedAndCart';

    _.merge(params, {method});
    return api.get('', params);
};

/*
 * 商品选择与取消选择
 * @function toggleSelectGoods
 * @param { Number } uid 用户UID
 * @param { String } sku 商品sku列表
 * @param { String } shoppingKey 未登录用户唯一识别码,可以不传
 * @return { Array }
 */
const removeGoods = (params) => {
    // BLK 没有促销, 新接口
    let method = 'app.Shopping.removeAndCart';

    _.merge(params, {method});
    return api.get('', params);
};

/*
 * 每次操作处理购物车数据
 * @function toggleSelectGoods
 * @param { Object } result 购物车数据
 * {
 *     advance_cart_data: {                  // 预售商品
 *         gift_list: [],                    // 赠品
 *         goods_list: []                    // 商品列表
 *         off_shelves_goods_list: [],       // 下架商品, 失效商品
 *         price_gift: [],                   // 加价购商品
 *         promotion_info: [],               // 促销信息
 *         shopping_cart_data: [],           // 商品价格计算相关信息
 *         sold_out_goods_list: []           // 售罄商品
 *     },
 *     ordinary_cart_data: {                 // 普通商品
 *         gift_list: [],
 *         goods_list: [],
 *         off_shelves_goods_list: [],
 *         price_gift: [],
 *         promotion_info: [],
 *         shopping_cart_data: [],
 *         sold_out_goods_list: []
 *     },
 *     md5: '...',
 *     message: '...'
 * }
 */
const filterCartData = (result, uid) => {
    // 预售商品
    let advancedGoods = [],
        selectedAdvanceNum = 0,
        totalAdvanceMoney,
        advancedCartData;

    // 普通商品
    let ordinaryGoods = [],
        selectedOrdinaryNum = 0,
        totalOrdinaryMoney,
        ordinaryCartData;

    // skn=0或者已下架的商品,定义为失效商品
    let invalidGoods = [];

    // 库存不足商品
    let soldOutGoods = [];

    // 商品总数量
    let buyNumber;
    let totalNum = 0;

    // 返回值
    let resData = {
        loggedIn: uid
    };

    if (result.data && result.data.advance_cart_data) {
        // 预售商品
        advancedCartData = result.data.advance_cart_data;
        console.log('---------------advance------:', advancedCartData.goods_list.length);
        totalAdvanceMoney = advancedCartData.shopping_cart_data.last_order_amount;
        advancedGoods = _.concat(advancedGoods, advancedCartData.goods_list);
        invalidGoods = _.concat(invalidGoods, advancedCartData.off_shelves_goods_list);
        soldOutGoods = _.concat(soldOutGoods, advancedCartData.sold_out_goods_list);

        _.forEach(advancedGoods, function(good) {
            buyNumber = parseInt(good.buy_number, 10);
            totalNum += buyNumber;
            if (good.selected === 'Y') {
                selectedAdvanceNum += buyNumber;
            }
        });
    }

    if (result.data && result.data.ordinary_cart_data) {
        // 普通商品
        ordinaryCartData = result.data.ordinary_cart_data;
        console.log('---------------ordinary------:', ordinaryCartData.goods_list.length);
        totalOrdinaryMoney = ordinaryCartData.shopping_cart_data.last_order_amount;
        ordinaryGoods = _.concat(ordinaryGoods, ordinaryCartData.goods_list);
        invalidGoods = _.concat(invalidGoods, ordinaryCartData.off_shelves_goods_list);
        soldOutGoods = _.concat(soldOutGoods, ordinaryCartData.sold_out_goods_list);

        _.forEach(ordinaryGoods, function(good) {
            buyNumber = parseInt(good.buy_number, 10);
            totalNum += buyNumber;
            if (good.selected === 'Y') {
                selectedOrdinaryNum += buyNumber;
            }
        });
    }

    // console.log('ordinaryGoods:', ordinaryGoods);


    return _.merge(resData, {
        hasGoods: advancedGoods.length || ordinaryGoods.length || invalidGoods.length,
        preSalePros: advancedGoods.length ? _.groupBy(advancedGoods, 'brand_id') : [],
        commonPros: ordinaryGoods.length ? _.groupBy(ordinaryGoods, 'brand_id') : [],
        invalidPros: invalidGoods,
        selectedNum: selectedAdvanceNum + selectedOrdinaryNum,
        checkAll: totalNum === (selectedAdvanceNum + selectedOrdinaryNum),
        totalMoney: totalAdvanceMoney + totalOrdinaryMoney

    });
};

/**
 * 增减购物车商品数量
 * @function modifyProductNum
 * @param { Number } uid 用户ID
 * @param { String } sku 商品SKU
 * @param { Number } increaseNum 增加的数目
 * @param { Number } decreaseNum 减少的数目
 * @param { String } shoppingKey 未登录用户唯一识别码
 * @return { Array } 接口返回的数据
 */
const modifyProductNum = (options) => {
    const increaseNum = options.increaseNum;
    const decreaseNum = options.decreaseNum;
    const uid = options.uid;
    const shoppingKey = options.shoppingKey;
    const sku = options.sku;

    let params = {
        product_sku: options.sku
    };

    // 增加
    if (increaseNum) {
        _.merge(params, {
            method: 'app.Shopping.increase',
            increase_number: parseInt(increaseNum, 10)
        });
    }

    // 减少
    if (decreaseNum) {
        _.merge(params, {
            method: 'app.Shopping.decrease',
            decrease_number: parseInt(decreaseNum, 10)
        });
    }

    if (uid) {
        _.merge(params, {uid});
    }

    if (shoppingKey) {
        _.merge(params, {
            shopping_key: shoppingKey
        });
    }

    if (sku) {
        _.merge(params, {
            product_sku: sku
        });
    }

    return api.get('', params);
};

module.exports = {
    addToCart,
    getCartData,
    transferToFavorite,
    toggleSelectGoods,
    removeGoods,
    filterCartData,
    modifyProductNum
};