order-ensure-handle.js 3.85 KB
/**
 * 订单确认数据处理
 * @author: yyq<yanqing.yang@yoho.cn>
 * @date: 2016/11/28
 */
'use strict';

const _ = require('lodash');
const helper = global.yoho.helpers;

module.exports = class extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);
    }

    handleViewPrice(list) {
        if (list) {
            _.forEach(list, val => {
                if (val.promotion === '商品金额') {
                    val.promotion = '商品总价';
                    val.promotion_amount = `+ ${val.promotion_amount}`;
                } else {
                    val.promotion_amount = _.replace(val.promotion_amount, '+', '+ ');
                    val.promotion_amount = _.replace(val.promotion_amount, '-', '- ');
                }
            });
        }
    }

    handleUseYohoCoin(info) {
        let resData = {};
        let limitCoin = _.get(info, 'yoho_coin_pay_rule.num_limit', 0);

        if (info) {
            Object.assign(resData, {
                yoho_coin: info.yoho_coin ? info.yoho_coin.toFixed(2) : 0,
                use_yoho_coin: info.use_yoho_coin,
                total_yoho_coin_num: info.total_yoho_coin_num,
                yoho_coin_pay_rule: info.yoho_coin_pay_rule,
                canUseCoinNum: _.round(info.yoho_coin * 100),
                usedCoinNum: _.round(info.use_yoho_coin * 100)
            });

            if (!resData.canUseCoinNum) {
                let coinErrorTip = '';

                if (info.total_yoho_coin_num > limitCoin) {
                    coinErrorTip = '抱歉,您的订单实付款不满足有货币使用条件';
                } else {
                    coinErrorTip = `抱歉,您的有货币不足,有货币满${limitCoin}个方可使用`;
                }

                resData.coinErrorTip = coinErrorTip;
            }
        }

        return resData;
    }

    handlePaymentInfo(d, address) {
        let resData = {};
        let defAddrId = _.get(d, 'delivery_address.address_id', 0);

        _.forEach(address, dd => {
            if (dd.is_default === 'Y') {
                dd.default = true;
            }

            dd.id = dd.address_id;

            // 设置默认选中地址
            if (dd.id === defAddrId) {
                dd.selected = true;
            }

            // 删除uid,用户数据保密
            _.unset(dd, 'address_id');
            _.unset(dd, 'uid');
        });

        resData.deliveryAddress = address;

        d.shopping_cart_data.hasCoin = _.round(d.yoho_coin * 100); // 有货币稀释

        _.forEach(d.goods_list, g => {
            g.last_vip_price = g.last_vip_price && Number(g.last_vip_price).toFixed(2) || '';
            g.sales_price = g.sales_price && Number(g.sales_price).toFixed(2) || '';

            // link to goods
            g.linkToGoods = helper.urlFormat(`/product/pro_${g.product_id}_${g.goods_id}/${g.cn_alphabet}.html`,
                '', 'item');
            g.productPrice = g.sales_price;
            g.isVipPrice = g.discount_tag === 'V';
            g.isStuPrice = g.discount_tag === 'S';

            // 赠品、加价购
            if (g.goods_type === 'gift' || g.goods_type === 'price_gift') {
                g.productPrice = g.last_price;
            }
        });
        resData.goodsList = d.goods_list;

        if (d.shopping_cart_data && d.shopping_cart_data.promotion_formula_list) {
            this.handleViewPrice(d.shopping_cart_data.promotion_formula_list);
        }

        Object.assign(resData, {
            paymentWay: d.payment_way,
            deliveryTime: d.delivery_time,
            deliveryWay: d.delivery_way,
            shoppingCartData: Object.assign(d.shopping_cart_data, this.handleUseYohoCoin(d), {
                redEnvelopes: d.red_envelopes,
                useRedEnvelopes: d.use_red_envelopes
            }),
            invoices: d.invoices
        });

        return resData;
    }
};