index.js 8.05 KB
/**
 * 购物车
 * @author: feng.chen<feng.chen@yoho.cn>
 * @date: 2016/12/21
 */

'use strict';
const api = global.yoho.API;
const cartProcess = require(global.utils + '/cart-process');

/**
 * 获取购物车信息
 *
 * @param integer $uid 用户ID
 * @param string $shoppingKey 未登录用户唯一识别码
 * @param string $cartType 购物车类型,默认是是所有购物车,ordinary为普通购物车,advance为预售购物车
 * @return array|mixed 处理之后的购物车数据
 */
const indexData = (uid, shoppingKey, saleChannel, cartType) => {
    return api.get('', {
        method: 'app.Shopping.queryCart',
        uid: uid,
        shopping_key: shoppingKey,
        sale_channel: saleChannel,
        is_support_mlp: 'Y'
    }).then((data) => {
        return data.code === 200 ? cartProcess.processData(data, cartType) : void 0;
    });
};

/**
 * 购物车商品选择与取消接口
 *
 * @param int $uid 用户ID
 * @param string $sku 商品sku列表
 * @param string $shoppingKey 未登录用户唯一识别码
 * @return array 购物车接口返回的数据
 */
const selectGood = (uid, sku, shoppingKey, cartType) => {
    return api.get('', {
        method: 'app.Shopping.selectedAndQryCart',
        product_sku_list: sku,
        uid: uid,
        shopping_key: shoppingKey,
        is_support_mlp: 'Y'
    }).then((data) => {
        return data.code === 200 ? cartProcess.processData(data, cartType) : void 0;
    });
};

/**
 * 移出购物车
 *
 * @param int $uid 用户ID
 * @param string $sku 商品sku列表
 * @param string $shoppingKey 未登录用户唯一识别码
 * @return array 接口返回的数据
 */
const removeFromCart = (uid, sku, shoppingKey, cartType) => {
    return api.get('', {
        method: 'app.Shopping.removeAndQryCart',
        product_sku_list: sku,
        uid: uid,
        shopping_key: shoppingKey,
        is_support_mlp: 'Y'
    }).then((data) => {
        return data.code === 200 ? cartProcess.processData(data, cartType) : void 0;
    });
};

/**
 * 获取购物车商品数据
 *
 * @param int $uid 用户ID
 * @param int $skn 商品skn
 * @param int $num 购物车商品数量
 * @return array 接口返回的数据
 */
const cartProductData = (uid, skn, num) => {
    return api.get('', {
        method: 'app.product.data',
        product_skn: skn,
        uid: uid,
        showcomment: 'N'
    }).then((data) => {
        return data.code === 200 ? cartProcess.procGoodsDetail(data.data, num) : void 0;
    });
};

/**
 * 修改购物车商品数量-增加
 *
 * @param int $uid 用户ID
 * @param string $sku 商品SKU
 * @param int $increaseNum 增加的数目
 * @param string $shoppingKey 未登录用户唯一识别码
 * @return array 接口返回的数据
 */
const increaseProductNum = (uid, sku, increaseNum, shoppingKey) => {
    return api.get('', {
        method: 'app.Shopping.increase',
        product_sku: sku,
        increase_number: increaseNum,
        uid: uid,
        shopping_key: shoppingKey
    });
};

/**
 * 修改购物车商品数量-减少
 *
 * @param int $uid 用户ID
 * @param string $sku 商品SKU
 * @param int $decreaseNum 减少的数目
 * @param string $shoppingKey 未登录用户唯一识别码
 * @return array 接口返回的数据
 */
const decreaseProductNum = (uid, sku, decreaseNum, shoppingKey) => {
    return api.get('', {
        method: 'app.Shopping.decrease',
        product_sku: sku,
        decrease_number: decreaseNum,
        uid: uid,
        shopping_key: shoppingKey
    });
};

/**
 * 修改购物车商品数据
 *
 * @param int $uid 用户ID
 * @param string $param 要更改的数据
 * @param string $shoppingKey 未登录用户唯一识别码
 * @return array 接口返回的数据
 */
const modifyCartProduct = (uid, swapData, shoppingKey) => {
    return api.get('', {
        method: 'app.Shopping.swap',
        swap_data: JSON.stringify(swapData),
        uid: uid,
        shopping_key: shoppingKey
    });
};

/**
 * 修改购物车赠品、加价购数据
 *
 * @param int $uid 用户ID
 * @param string $param 要更改的数据
 * @param string $shoppingKey 未登录用户唯一识别码
 * @return array 接口返回的数据
 */
const modifyCartPriceGiftProduct = (uid, newProductSku, newProductSkn, promotionId, shoppingKey) => {
    return api.get('', {
        method: 'app.Shopping.swapGift',
        promotion_id: promotionId,
        new_product_sku: newProductSku,
        new_product_skn: newProductSkn,
        uid: uid,
        shopping_key: shoppingKey
    });
};

/**
 * 移入收藏夹
 *
 * @param int $uid 用户ID
 * @param string $sku 商品sku列表
 * @return array 接口返回的数据
 */
const addToFav = (uid, sku, shoppingKey, cartType) => {
    return api.get('', {
        method: 'app.Shopping.addfavoriteAndQryCart',
        product_sku_list: sku,
        uid: uid,
        shopping_key: shoppingKey,
        is_support_mlp: 'Y'
    }).then((data) => {
        return data.code === 200 ? cartProcess.processData(data, cartType) : void 0;
    });
};

/**
 * 加入购物车
 *
 * @param int $productSku 商品SKU
 * @param int $buyNumber 购买数量
 * @param int $goodsType 商品类型,0表示普通商品,1表示加价购商品
 * @param int $isEdit 是否是编辑商品SKU,0表示不是编辑
 * @param null|int $promotionId 促销id,默认null(加价购有关)
 * @param null|int $uid 用户UID,可以不传
 * @param string $shoppingKey 未登录用户唯一识别码,可以不传
 * @return array 加入购物车接口返回的数据
 */
const addToCart = (productSku, buyNumber, goodsType, isEdit, promotionId, uid, shoppingKey) => {
    return api.get('', {
        method: 'app.Shopping.add',
        product_sku: productSku,
        buy_number: buyNumber,
        goods_type: goodsType,
        edit_product_sku: isEdit,
        selected: 'Y',
        promotion_id: promotionId,
        uid: uid,
        shopping_key: shoppingKey
    });
};

/**
 * 获取商品的套餐量贩信息
 * @param $productSkn
 * @return mixed
 */
const handleBundleInfo = (productSkn) => {
    return api.get('', {
        method: 'app.query.bundleSkn',
        product_skn: productSkn,
    }).then(data => {
        return data.code === 200 ? cartProcess.handleBundleInfo(data) : void 0;
    });
};

/**
 * 获取加价购商品数据
 *
 * @param int $skn 商品skn
 * @param int $promotionId 加价购商品促销ID
 * @return array 接口返回的数据
 */
const giftProductData = (skn, promotionId) => {
    return api.get('', {
        method: 'app.product.gift',
        product_skn: skn,
        promotion_id: promotionId
    }).then(data => {
        return data.code === 200 ? cartProcess.procGoodsDetail(data.data) : void 0;
    });
};

/**
 * [获取赠品、加价购促销商品列表]
 */
const getPriceGiftList = (promotionIds, promotionType) => {
    return api.get('', {
        method: 'app.Shopping.queryPromotionGifts',
        promotion_ids: promotionIds
    }).then((data) => {
        return data.code === 200 ? cartProcess.procPriceGiftData(data.data, promotionType) : {};
    });
};

/**
 * 增加套餐数量
 * @param {*} params
 */
const incrBundle = (params) => {
    return api.post('', {
        method: 'app.Shopping.incrBundle',
        uid: params.uid,
        shopping_key: params.shopping_key,
        increase_number: params.increase_number || '1',
        activity_id: params.activity_id,
        sku_ids: params.sku_ids,
        batch_no: params.batch_no
    });
};

/**
 * 减少套餐数量
 * @param {*} params
 */
const decrBundle = (params) => {
    return api.post('', {
        method: 'app.Shopping.decrBundle',
        uid: params.uid,
        shopping_key: params.shopping_key,
        decrease_number: params.decrease_number || '1',
        activity_id: params.activity_id,
        sku_ids: params.sku_ids,
        batch_no: params.batch_no
    });
};

module.exports = {
    indexData,
    selectGood,
    removeFromCart,
    cartProductData,
    addToFav,
    increaseProductNum,
    decreaseProductNum,
    addToCart,
    handleBundleInfo,
    modifyCartProduct,
    giftProductData,
    getPriceGiftList,
    modifyCartPriceGiftProduct,
    incrBundle,
    decrBundle
};