cart-api.js 8.04 KB
/**
 * cart api
 * @author: lcy<chuanyang.liu@yoho.cn>
 * @date: 2016/12/26
 */
'use strict';

const api = global.yoho.API;
const _ = require('lodash');

/**
 * 获取购物车数据
 * @param uid
 * @param shoppingKey
 * @returns {*}
 */
const cartData = (uid, shoppingKey) => {

    let param = {
        method: 'app.Shopping.queryCart' // old: 'app.Shopping.cart'
    };

    if (uid) {
        param.uid = uid;
    }

    if (shoppingKey) {
        param.shopping_key = shoppingKey;
    }
    return api.get('', param);
};

/**
 * 加入购物车接口
 *
 * @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) => {

    let param = {
        method: 'app.Shopping.add',
        product_sku: productSku,
        buy_number: Number(buyNumber),
        goods_type: goodsType,
        edit_product_sku: isEdit || 0,
        selected: 'Y',
        promotion_id: promotionId || null
    };

    if (uid) {
        param.uid = uid;
    }

    if (shoppingKey) {
        param.shopping_key = shoppingKey;
    }

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

/**
 * 购物车商品选择与取消接口
 *
 * @param int $uid 用户ID
 * @param string $sku 商品sku列表
 * @param string $shoppingKey 未登录用户唯一识别码
 * @param bool $hasPromotion 是否有促销ID
 * @return array 购物车接口返回的数据
 */
const selectGoods = (uid, sku, shoppingKey /* , hasPromotion*/) => {

    let param = {
        // method: hasPromotion ? 'app.Shopping.selectedAndCart' : 'app.Shopping.selected',
        method: 'app.Shopping.selectedAndQryCart',
        product_sku_list: sku
    };

    if (uid) {
        param.uid = uid;
    }

    if (shoppingKey) {
        param.shopping_key = shoppingKey;
    }

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


/**
 * 移出购物车
 *
 * @param int $uid 用户ID
 * @param string $sku 商品sku列表
 * @param string $shoppingKey 未登录用户唯一识别码
 * @param bool $hasPromotion 是否有促销ID
 * @return array 接口返回的数据
 */
const removeFromCart = (uid, shoppingKey, skuList, hasPromotion) =>{

    let param = {
        method: hasPromotion ? 'app.Shopping.removeAndQryCart' : 'app.Shopping.remove',
        product_sku_list: skuList
    };

    if (uid) {
        param.uid = uid;
    }
    if (shoppingKey) {
        param.shopping_key = shoppingKey;
    }

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

/**
 * 移入收藏夹
 *
 * @param int $uid 用户ID
 * @param string $sku 商品sku列表
 * @param bool $hasPromotion 是否有促销ID
 * @return array 接口返回的数据
 */
const addToFav = (uid, skuList /* , hasPromotion*/) =>{

    let param = {
        // method: hasPromotion ? 'app.Shopping.addfavoriteAndCart' : 'app.Shopping.addfavorite',
        method: 'app.Shopping.addfavoriteAndQryCart',
        product_sku_list: skuList,
        uid: uid
    };

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

/**
 * 增减购物车商品数量
 *
 * @param int $uid 用户ID
 * @param string $sku 商品SKU
 * @param int $increaseNum 增加的数目
 * @param int $decreaseNum 减少的数目
 * @param string $shoppingKey 未登录用户唯一识别码
 * @return array 接口返回的数据
 */
const modifyProductNum = (uid, sku, increaseNum, decreaseNum, shoppingKey) => {

    let param = {
        product_sku: sku
    };


    // 增加
    if (increaseNum) {
        param.method = 'app.Shopping.increase';
        param.increase_number = Number(increaseNum);
    }

    // 减少
    if (decreaseNum) {
        param.method = 'app.Shopping.decrease';
        param.decrease_number = Number(decreaseNum);
    }
    if (uid) {
        param.uid = uid;
    }
    if (shoppingKey) {
        param.shopping_key = shoppingKey;
    }

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

/**
 * 购物车数量
 *
 * @param int $uid 用户ID
 * @param string $shoppingKey 未登录用户唯一识别码
 * @return array 购物车接口返回的数据
 */
const cartCount = (uid, shoppingKey) => {

    let param = {
        method: 'app.Shopping.count'
    };

    if (uid) {
        param.uid = uid;
    }
    if (shoppingKey) {
        param.shopping_key = shoppingKey;
    }

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

/**
 * 凑单商品
 *
 * @param int $page 分页页码
 * @return array
 */
const togetherProduct = (page) => {

    let param = {
        method: 'web.product.together',
        client_type: 'web',

        // private_key: Yohobuy::$privateKeyList['web'];
        page: page
    };

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

/**
 * 判断商品列表是否收藏情况
 * @param uid
 * @param pidList
 * @returns {Promise.<T>}
 */
const checkUserIsFavProductList = (uid, pidList) => {

    pidList = _.isArray(pidList) ? pidList : [];

    let promises = _.map(pidList, pid => {
        return api.get('', {
            method: 'app.favorite.isFavorite',
            id: pid,
            uid: uid,
            type: 'product'
        });
    });

    return Promise.all(promises)
        .then(ret => {

            let result = {};

            _.forEach(pidList, (pid, index) => {
                result[pid] = ret[index];
            });

            return result;
        });
};

/**
 * 个人中心页面优选新品数据
 *
 * @param int $channel 频道,1代表男生,2代表女生,3代表潮童,4代表创意生活
 * @param $uid 用户ID
 * @param $udid 设备ID
 * @param $rec_pos 位置码
 * @param $limit 数量限制
 * @return array 接口返回的数据
 */
const newPreference = (channel, uid, udid, recPos, limit) => {

    let param = {
        method: 'app.home.newPreference',
        yh_channel: channel,
        udid: udid,
        rec_pos: recPos,
        limit: limit
    };

    if (uid) {
        param.uid = uid;
    }

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

/**
 * 修改商品颜色和尺寸
 * @function modifyProduct
 * @param { Number } uid 用户ID
 * @param { String } swapData 商品修改信息
 * @param { String } shoppingKey 未登录用户唯一识别码
 * @return { Object } 接口返回单个商品修改结果
 */
const modifyProduct = (options) => {
    let params = {
        method: 'app.Shopping.swap'
    };

    _.merge(params, {
        swap_data: options.swapData
    });

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

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

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

/**
 * 更换加价购,赠品商品
 * @function modifyProduct
 * @param { Number } uid 用户ID
 * @param { String } swapData 商品修改信息
 * @param { String } shoppingKey 未登录用户唯一识别码
 * @return { Object } 接口返回单个商品修改结果
 */
const swapGift = (uid, shoppingKey, promotionId, newSkn, newSku) => {


    let param = {
        method: 'app.Shopping.swapGift',
        promotion_id: promotionId,
        new_product_skn: newSkn,
        new_product_sku: newSku
    };

    if (uid) {
        param.uid = uid;
    }

    if (shoppingKey) {
        param.shopping_key = shoppingKey;
    }


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

/**
 * 查询所有可选的赠品或者加价购活动下的商品
 * @param promotionId array
 * @returns {*}
 */
const queryPromotionGift = (promotionIds) => {
    let param = {
        method: 'app.Shopping.queryPromotionGifts',
        promotion_ids: promotionIds
    };

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

module.exports = {
    cartData,
    addToCart,
    selectGoods,
    removeFromCart,
    addToFav,
    modifyProductNum,
    cartCount,
    togetherProduct,
    checkUserIsFavProductList,
    newPreference,
    modifyProduct,
    swapGift,
    queryPromotionGift
};