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

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

const EnsureApi = require('./order-ensure-api');
const AddressApi = require('./address-api');
const EnsureHandle = require('./order-ensure-handle');

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

    index(uid, cartType) {
        let resData = {};
        let addressApiModel = new AddressApi(this.ctx);
        let ensureApiModel = new EnsureApi(this.ctx);
        let ensureHandleModel = new EnsureHandle(this.ctx);

        return Promise.all([
            ensureApiModel.getOrderPaymentAsync(uid, cartType, 0),
            addressApiModel.getAddressListAsync(uid),
            ensureApiModel.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 (+_.get(result, '[0].code') === 9999992) {
                resData.pageErrorTip = _.get(result, '[0].message', '');

                return resData;
            }

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

            return resData;
        });
    }

    // 获取优惠券列表
    getCoupons(uid) {
        return new EnsureApi(this.ctx).getUesrCouponAsync(uid).then(result => {
            if (_.isEmpty(_.get(result, 'data.usable_coupons', [])) &&
                _.isEmpty(_.get(result, 'data.usable_frees_coupons', []))) {
                _.set(result, 'data.emptyUsable', true);
            }
            return result;
        });
    }

    // 兑换优惠券
    convertCoupons(uid, code) {
        return new EnsureApi(this.ctx).getCouponByCodeAsync(uid, code);
    }

    // 订单计算
    compute(uid, cartType, pa) {
        return new EnsureApi(this.ctx).getOrderComputeAsync(
            uid, cartType, pa.paymentType, pa.deliveryWay, pa
        ).then(result => {
            if (result.code === 200) {
                let ensureHandleModel = new EnsureHandle(this.ctx);

                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')) {
                    ensureHandleModel.handleViewPrice(result.data.promotion_formula_list);
                }

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

            return result;
        });
    }

    // 获取礼品卡列表
    getGiftCards(uid) {
        const ensureApiModel = new EnsureApi(this.ctx);

        return Promise.all([
            ensureApiModel.getGiftCardAsync(uid),
            ensureApiModel.getUserProfileAsync(uid)
        ]).then(result => {
            let resData = result[0];

            if (!_.isEmpty(_.get(resData, 'data.usable_giftCards', []))) {
                _.map(resData.data.usable_giftCards, value => {
                    return Object.assign(value, {price: _.replace(value.remainAmount, /[^(0-9.)]/ig, '')});
                });
            }

            if (result[1].code === 200) {
                _.set(resData, 'data.userMobile', _.get(result[1], 'data.mobile'));

            }

            return resData;
        });
    }

    // 发送礼品卡使用校验短信
    sendGiftCardCkeckSms(uid, udid) {
        return new EnsureApi(this.ctx).sendGiftCardCkeckSmsAsync(uid, udid);
    }

    // 校验礼品卡使用短信验证码
    checkGiftCardSmsCode(uid, code, udid) {
        return new EnsureApi(this.ctx).checkGiftCardSmsCodeAsync(uid, code, udid);
    }

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

        return new EnsureApi(this.ctx).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;
            }
        );
    }
};