order.js 6.43 KB
/**
 * 结算页
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2016/7/13
 */

'use strict';

const _ = require('lodash');

const api = global.yoho.API;
const helper = global.yoho.helpers;

/**
 * 结算页面展示
 * @param uid 用户id
 */

const _orderApi = uid => api.get('', {
    method: 'app.Shopping.payment',
    'cart_type': 'ordinary', // eslint-disable-line
    'yoho_coin_mode': 0,  // eslint-disable-line
    uid: uid
});


const index = uid => {
    return _orderApi(uid).then(data => {
        if (data.code === 200) {

            // do data package
            let theData = data.data;

            let goods = theData.goods_list;

            let shopping = theData.shopping_cart_data;

            let packageList = shopping.package_list;

            _.forEach(goods, theGoods => {
                let splitName = theGoods.product_name.split(' ');

                // split brand name form product_name
                theGoods.brandName = splitName.shift();
                theGoods.name = splitName.join(' ');

                // format goods link
                theGoods.link = helper.urlFormat(
                    `/product/pro_${theGoods.product_id}_${theGoods.goods_id}/${theGoods.cn_alphabet}.html`);

                // TODO:format brand url
                // theGoods.brandUrl = helper.urlFormat(`/product/shop/${theGoods.brand_name}`);

                // 处理商品是否是赠品
                if (theGoods.goods_type === 'gift') {
                    theGoods.isGift = true;
                }
            });

            theData.yoho_coin *= 100; // 有货币稀释

            shopping.balanceCoin = theData.use_yoho_coin;

            // 拆单后的邮费优惠计算
            if (packageList) {
                let shippingOrigin = 0;
                let shippingCut = 0;

                _.forEach(packageList, i => {
                    shippingOrigin += (i.shopping_orig_cost >>> 0);
                    shippingCut += (i.shopping_cut_cost >>> 0);
                });

                // 对运费的数据进行扩展
                shippingCut === 0 || Object.assign(
                    _.filter(shopping.promotion_formula_list, i => i.promotion === '运费'), {
                        shippingCut: shippingCut,
                        shippingOrigin: shippingOrigin
                    }
                );
            }

            return data;
        }
        return data;
    });
};

/**
 * 结算
 * @param number uid user id
 * @param number $deliveryWay 配送方式,1表示普通快递,2表示顺丰速运
 * @param int $paymentType 支付方式,1表示在线支付,2表示货到付款
 * @param number $yohoCoin 使用的YOHO币数量
 * */
const _computeApi = (uid, deliveryWay, paymentType, yohoCoin) => api.get('', {
    method: 'app.Shopping.compute',
    uid: uid,
    delivery_way: deliveryWay,
    payment_type: paymentType,
    use_yoho_coin: yohoCoin
});

const compute = (uid, yohoCoin) => {
    // 目前仅支持普通快递和在线支付
    let deliveryWay = 1;
    let paymentType = 1;

    let coin;

    // YOHO币稀释
    if (yohoCoin) {
        coin = yohoCoin / 100;
    }

    return _computeApi(uid, deliveryWay, paymentType, coin).then(result => result);
};

/**
 * 提交订单(电子发票)
 * @param number uid user id
 * @param number address_id 地址id
 * @param number yohoCoin 使用的有货币
 * @param number invoices_type 发票类型(1-纸质发票,2-电子发票)
 * @param string invoices_title 发票抬头
 * @param int invoices_content 发票内容
 * @param string receiverMobile 收票人手机
 * @param string remark 备注
 * @param boolean isPrintPrice 是否打印价格
 * @param number delivery_time 送货时间
 * @param number delivery_way 送货方式(1-普通,2-顺丰)
 * @param number payment_id 支付id
 * @param number payment_type 支付类型
 */
// const _submitElInvoice = (uid, other) => api.get('', {
//     method: 'app.Shopping.submit',
//     uid: uid,
//     address_id: other.address_id,
//     yohoCoin: other.yohoCoin,
//     invoices_type: other.invoices_type,
//     invoices_title: other.invoices_title,
//     invoices_content: other.invoices_content,
//     recevierMobile: other.recevierMobile,
//     remark: other.remark,
//     isPrintPrice: other.isPrintPrice,
//     delivery_time: other.delivery_time,
//     delivery_way: other.delivery_way,
//     payment_id: other.payment_id,
//     payment_type: other.payment_type
// });

/**
 * 提交订单
 * @param number uid user id
 * @param number address_id 地址id
 * @param number yohoCoin 使用的有货币
 * @param string invoices_title 发票抬头
 * @param int invoices_type_id 发票内容
 * @param string remark 备注
 * @param boolean isPrintPrice 是否打印价格
 * @param number delivery_time 送货时间
 * @param number delivery_way 送货方式(1-普通,2-顺丰)
 * @param number payment_id 支付id
 * @param number payment_type 支付类型
 */
const _submit = (uid, other) => {
    let apiParms = {
        method: 'app.Shopping.submit',
        uid: uid,
        address_id: other.address_id,
        yohoCoin: other.yohoCoin,
        remark: other.remark,
        isPrintPrice: other.isPrintPrice,
        delivery_time: other.delivery_time,
        delivery_way: other.delivery_way,
        payment_id: other.payment_id,
        payment_type: other.payment_type
    };

    if (other.invoices_title) {
        Object.assign(apiParms, {
            invoices_title: other.invoices_title,
            invoices_type_id: other.invoices_type_id
        });
    }

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

const submit = (uid, other) => {
    let theOther = {};
    let coin = other.coin;

    Object.assign(theOther, other, {
        delivery_time: 2,  // 平时和周末都送货
        delivery_way: 1, // 普通快递
        payment_id: 1, // 支付宝
        payment_type: 1 // 在线支付
    });

    // 有货币稀释
    if (coin) {
        coin = coin / 100;
    }

    return _submit(uid, theOther).then(result => result);
};

const orderDetail = (uid, code) => {
    return api.get('', {
        method: 'app.SpaceOrders.detail',
        uid: uid,
        order_code: code
    }, {code: 200});
};

const updateOrderPayment = (code, payment, uid) => {
    return api.get('', {
        method: 'app.SpaceOrders.updateOrdersPaymentByCode',
        order_code: code,
        payment: payment,
        uid: uid
    });
};

module.exports = {
    index,
    compute,
    submit,
    orderDetail,
    updateOrderPayment
};