shopping.js 3.36 KB
'use strict';
const _ = require('lodash');
const api = global.yoho.API;

/**
 * 购物车结算
 * doc: http://git.yoho.cn/yoho-documents/api-interfaces/blob/master/订单/shopping.md
 * @param int $uid 用户ID
 * @param string $cartType 购物车类型,ordinary表示普通购物车
 * @param int $isUseYohoCoin 是否使用有货币,0不使用, 1使用
 * @param string $skuList 购买限购商品时需要传递的参数
 * @return see doc
 */
exports.cartPayAPI = (uid, cartType, isUseYohoCoin, skuList) => {
    let param = {
        method: 'app.Shopping.payment',
        enable_red_envelopes: 0, // h5不返回红包
        cart_type: cartType,
        yoho_coin_mode: isUseYohoCoin,
        uid: uid
    };

    // 购买限购商品时需要传递product_sku_list参数
    if (!_.isEmpty(skuList)) {
        param.product_sku_list = skuList;
    }

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


/**
 * 购物车结算--支付方式和配送方式选择以及是否使用有货币接口返回的数据处理
 * doc: http://git.yoho.cn/yoho-documents/api-interfaces/blob/master/订单/shopping.md
 * @param int $uid 用户ID
 * @param string $cartType 购物车类型,ordinary表示普通购物车
 * @param int $deliveryWay 配送方式,1表示普通快递,2表示顺丰速运
 * @param int $paymentType 支付方式,1表示在线支付,2表示货到付款
 * @param string $couponCode 优惠券码
 * @param mixed $yohoCoin 使用的有货币数量
 * @param string $skuList 购买限购商品时需要传递的参数
 * @return see doc
 *
 *
 * Note:
 *  1. 大多数情况下,调用完此接口,请显示的调用 utils/payment-process.js@yohoCoinCompute;
 *  2. 除非同时调用app.Shopping.payment 与该接口,用改接口的data值替换app.Shopping.payment相关值,
 *  显示调用utils/payment-process.js@tranformPayment(他会帮你调yohoCoinCompute)处理payment的data;
 */
exports.orderComputeAPI = (uid, cartType, deliveWay, paymentType, couponCode, yohoCoin, skuList) => {
    let param = {
        method: 'app.Shopping.compute',
        cart_type: cartType,
        delive_way: deliveWay,
        payment_type: paymentType
    };

    if (couponCode) {
        param.coupon_code = couponCode;
    }

    if (yohoCoin) {
        param.use_yoho_coin = yohoCoin;
    }

    if (!_.isEmpty(skuList)) {
        param.product_sku_list = skuList;
    }

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


/**
 *  获取用户可用的优惠券张数
 *  doc: http://git.yoho.cn/yoho-documents/api-interfaces/blob/master/订单/shopping.md
 *  @param uid int 用户uid
 *  @return see doc
 */
exports.getValidCouponCount = uid => {
    let param = {
        method: 'app.Shopping.countUsableCoupon',
        uid
    };

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


/**
 *  获取用户优惠券列表,可用和不可用
 *  doc: http://git.yoho.cn/yoho-documents/api-interfaces/blob/master/订单/shopping.md
 *  @param uid int 用户uid
 *  @return see doc
 */
exports.listCoupon = uid => {
    let param = {
        method: 'app.Shopping.listCoupon',
        uid
    };

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

/**
 * 购物车结算--使用优惠券
 * @param uid int   用户uid
 * @param couponCode string 优惠券代码
 * @return
 */
exports.useCoupon = (uid, couponCode) => {
    let param = {
        method: 'app.Shopping.useCoupon',
        coupon_code: couponCode,
        uid
    };

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