buy-now-model.js 5.93 KB
/*
 * @Author: Targaryen
 * @Date: 2017-06-21 10:15:45
 * @Last Modified by: Targaryen
 */
const api = global.yoho.API;

class BuyNowModel extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);
    }

    /**
     * 获取用户可用的优惠券张数
     * @param {*} params
     */
    countUsableCoupon(params) {
        return api.post('', {
            method: 'app.Buynow.countUsableCoupon',
            uid: params.uid,
            product_sku: params.product_sku,
            sku_type: params.sku_type || 'I',
            buy_number: params.buy_number
        }, {cache: false});
    }

    /**
     * 获取用户可用和不可用优惠券列表
     * @param {*} params
     */
    listCoupon(params) {
        return api.post('', {
            method: 'app.Buynow.listCoupon',
            uid: params.uid,
            product_sku: params.product_sku,
            sku_type: params.sku_type || 'I',
            buy_number: params.buy_number
        }, {cache: false});
    }

    /**
     * 结算页面数据渲染
     * @param {*} params
     */
    payment(params) {
        let finalParams = {
            method: 'app.Buynow.payment',
            uid: params.uid,
            product_sku: params.product_sku,
            sku_type: params.sku_type || 'I',
            buy_number: params.buy_number,
            yoho_coin_mode: params.yoho_coin_mode || 0, // 是否使用有货币
            yoho_bill_term: 0, // H5 不支持分期
            is_support_apple_pay: 'N', // H5 不支持 APPLE PAY
            activity_id: params.activity_id || 0,
            enable_red_envelopes: 0 // H5 不支持使用红包
        };

        return api.post('', finalParams, { cache: false });
    }

    /**
     * 数据改变,重新计算结算数据
     * @param {*} params
     */
    compute(params) {
        let finalParams = {
            method: 'app.Buynow.compute',
            uid: params.uid,
            product_sku: params.product_sku,
            sku_type: params.sku_type || 'I',
            buy_number: params.buy_number,
            payment_type: params.payment_type || 1,
            delivery_way: params.delivery_way || 1,
            use_red_envelopes: 0,
            yoho_bill_term: 0,
            activity_id: params.activity_id || 0
        };

        if (params.use_yoho_coin) {
            finalParams.use_yoho_coin = params.use_yoho_coin;
        }

        if (params.coupon_code) {
            finalParams.coupon_code = params.coupon_code;
        }

        if (params.promotion_code) {
            finalParams.promotion_code = params.promotion_code;
        }

        if (params.coupon_code) {
            finalParams.coupon_code = params.coupon_code;
        }

        return api.post('', finalParams, {cache: false});
    }

    /**
     * 提交订单
     * @param {*} params
     */
    submit(params) {
        let finalParams = {
            method: 'app.Buynow.submit',
            uid: params.uid,
            product_sku: params.product_sku,
            sku_type: params.sku_type || 'I',
            buy_number: params.buy_number,
            yoho_bill_term: 0,
            address_id: params.address_id,
            delivery_time: params.delivery_time,
            delivery_way: params.delivery_way,
            use_red_envelopes: 0,
            payment_id: params.payment_id,
            payment_type: params.payment_type || 1,
            activity_id: params.activity_id || 0,
            is_print_price: params.is_print_price || 'N'
        };

        // 发票类型:纸质 1,电子 2
        if (params.invoices_type) {
            finalParams.invoices_type = params.invoices_type;
        }

        // 发票抬头
        if (params.invoices_title) {
            finalParams.invoices_title = params.invoices_title;
        }

        // 发票种类写死明细
        if (params.invoice) {
            finalParams.invoice_content = 12;
        }

        // 收票人手机号码
        if (params.receiverMobile) {
            finalParams.receiverMobile = params.receiverMobile;
        }

        // 购买方纳税人识别号,需要开具电子发票且发票抬头为单位信息时为必填项
        if (params.buyerTaxNumber) {
            finalParams.buyerTaxNumber = params.buyerTaxNumber;
        }

        if (params.use_yoho_coin) {
            finalParams.use_yoho_coin = params.use_yoho_coin;
        }

        if (params.remark) {
            finalParams.remark = params.remark;
        }

        if (params.coupon_code) {
            finalParams.coupon_code = params.coupon_code;
        }

        if (params.promotion_code) {
            finalParams.promotion_code = params.promotion_code;
        }

        if (params.qhy_union) {
            finalParams.qhy_union = params.qhy_union;
        }

        return api.post('', finalParams, {
            headers: {
                'X-Forwarded-For': params.ip || '',
                'User-Agent': params.userAgent
            }
        });
    }

    /**
     * 使用优惠券
     * @param {*} params
     */
    useCoupon(params) {
        let finalParams = {
            method: 'app.Buynow.useCoupon',
            uid: params.uid,
            product_sku: params.product_sku,
            sku_type: params.sku_type || 'I',
            buy_number: params.buy_number,
        };

        if (params.coupon_code) {
            finalParams.coupon_code = params.coupon_code;
        }
        return api.post('', finalParams, {cache: false});
    }

    /**
     * 使用优惠码
     * @param {*} params
     */
    usePromotionCode(params) {
        let finalParams = {
            method: 'app.Buynow.usePromotionCode',
            uid: params.uid,
            product_sku: params.product_sku,
            sku_type: params.sku_type || 'I',
            buy_number: params.buy_number
        };

        if (params.promotion_code) {
            finalParams.promotion_code = params.promotion_code;
        }

        return api.post('', finalParams, {cache: false});
    }
}

module.exports = BuyNowModel;