order-ensure.js 2.77 KB
/**
 * 订单确认model
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2016/8/26
 */
'use strict';

const _ = require('lodash');

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

const crypto = global.yoho.crypto;

// const config = global.yoho.config;

const ensureApi = require('./order-ensure-api');
const addressApi = require('./address-api');

const ensureHandle = require('./order-ensure-handle');

const index = (uid, cartType) => {
    let resData = {};

    return Promise.all([
        ensureApi.getOrderPaymentAsync(uid, cartType, 0),
        addressApi.getAddressListAsync(uid),
        ensureApi.getUserProfileAsync(uid)
    ]).then(result => {
        let data = _.get(result, '[0].data', false);
        let address = _.get(result, '[1].data', []);
        let receiver = _.get(result, '[2].data.mobile', '');

        if (data) {
            Object.assign(resData, ensureHandle.handlePaymentInfo(data, address), {
                receiverMobile: receiver,
                hideReceiverMobile: _.replace(receiver, /(\d{3})\d{4}(\d{4,9})/, '$1****$2')
            });
        }

        return resData;
    });
};

// 获取优惠券列表
const getCoupons = (uid) => ensureApi.getUesrCouponAsync(uid);

// 订单计算
const compute = (uid, cartType, pa) => {
    return ensureApi.getOrderComputeAsync(uid, cartType, pa.paymentType, pa.deliveryWay, pa).then(result => {
        if (result.code === 200) {
            if (_.has(result, 'data.last_order_amount')) {
                result.data.last_order_amount = (result.data.last_order_amount).toFixed(2);
            }

            if (_.has(result, 'data.promotion_formula_list')) {
                ensureHandle.handleViewPrice(result.data.promotion_formula_list);
            }

            result.data = Object.assign(result.data, ensureHandle.handleUseYohoCoin(result.data));
        }

        return result;
    });
};

// 订单提交
const submit = (uid, cartType, p, remoteIp) => {
    if (p.addressId) {
        p.addressId = crypto.decrypt('', `${p.addressId}`);
    }

    // 5.8.1 发票优化需求
    // 只接受电子发票(1 纸质 2 电子),发票内容为明细(id 12:明细)
    if (p.invoicesType) {
        Object.assign(p, {
            invoicesType: 2,
            invoicesContent: 12
        });
    }

    return ensureApi.orderSubmitAsync(uid, cartType, p.addressId, p.deliveryTime,
        p.deliveryWay, p.paymentType, p.paymentId, p.printPrice, p, remoteIp).then(result => {
            if (result.code === 200) {
                let d = result.data;

                d.url = helper.urlFormat('/shopping/newpay', {
                    ordercode: d.order_code
                });
            }

            return result;
        }
    );
};

module.exports = {
    index,
    getCoupons,
    compute,
    submit
};