order.js 7.07 KB
'use strict';
const _ = require('lodash');
const helpers = global.yoho.helpers;
const co = require('bluebird').coroutine;
const cartModel = require('../models/cart');
const headerModel = require('../../../doraemon/models/header');
const userModel = require('../../serverAPI/user');
const addressModel = require('../../serverAPI/user/address');
const orderModel = require('../models/order');


exports.orderEnsure = (req, res, next) => {
    let headerData = headerModel.setNav({
        navTitle: '确认订单',
        navBtn: false
    });

    let uid = req.user.uid;
    let returnUrl = helpers.urlFormat('/cart/index/new');
    let cartType = req.query.cartType || 'ordinary';
    let orderInfo;

    try {
        orderInfo = JSON.parse(req.cookies['order-info']);
    } catch (e) {
        orderInfo = {};
    }

    let cookieCartType = _.get(orderInfo, 'cartType');

    if (cookieCartType) {
        cartType = cookieCartType;
    }

    // 如果传递了code, skn, sku, buy_number 就代表限购商品
    let limitProductCode = req.query.limitproductcode || '';
    let sku = req.query.sku || '';
    let skn = req.query.skn || '';
    let buyNumber = req.query.buy_number || 1;

    if (limitProductCode) {
        headerData.backUrl = req.get('Referer') || returnUrl;
    }

    let orderPromise = cartModel.cartPay(uid, cartType, orderInfo, sku, skn, buyNumber, req.xhr);
    let userProfilePromise = userModel.queryProfile(uid);
    let addressPromise = addressModel.addressData(uid);

    return Promise.all([orderPromise, userProfilePromise, addressPromise]).then(result => {
        let order = result[0];
        let userProfile = result[1];
        let address = result[2];

        if (order.cartUrl) {   // TODO?  普通或者预售商品为空时, BUT WHEN AJAX?
            return res.redirect(order.cartUrl);
        }

        if (req.xhr) {
            return res.json(order);
        }

        // 获取用户完整手机号
        let mobile = _.get(userProfile, 'data.mobile', '');
        let orderAddress = _.get(order, 'address', []);
        let addressList = _.get(address, 'data', []);

        orderAddress.length && addressList.forEach(address => { //eslint-disable-line
            if (address.address_id === orderAddress.address_id) {
                mobile = address.mobile;
            }
        });

        let viewData = {
            orderEnsurePage: true,
            isOrdinaryCart: cartType !== 'advance',
            orderEnsure: order,
            userMobile: mobile,
            pageHeader: headerData,
            pageFooter: true,
            module: 'cart',
            page: 'order-ensure',
            width750: true,
            title: '确认订单',
        };

        res.render('order-ensure', viewData);
    }).catch(next);
};

/**
 * 快速结算,商品不进购物车
 */
exports.fastPay = (req, res, next) => {
    let headerData = headerModel.setNav({
        navTitle: '确认订单',
        navBtn: false
    });
    let uid = req.user.uid;
    let orderPromise = cartModel.easyPayment(uid, req.query.skuList);
    let userProfilePromise = userModel.queryProfile(uid);
    let addressPromise = addressModel.addressData(uid);

    Promise.all([orderPromise, userProfilePromise, addressPromise]).then(result => {
        let order = result[0];
        let userProfile = result[1];
        let address = result[2];

        // 获取用户完整手机号
        let mobile = _.get(userProfile, 'data.mobile', '');
        let orderAddress = _.get(order, 'address', []);
        let addressList = _.get(address, 'data', []);

        orderAddress.length && addressList.forEach(address => { //eslint-disable-line
            if (address.address_id === orderAddress.address_id) {
                mobile = address.mobile;
            }
        });

        let viewData = {
            orderEnsurePage: true,
            isOrdinaryCart: true,
            orderEnsure: order,
            userMobile: mobile,
            pageHeader: headerData,
            pageFooter: true,
            module: 'cart',
            page: 'order-ensure',
            width750: true,
            title: '确认订单',
        };

        res.render('order-ensure', viewData);
    }).catch(next);
};

/**
 *  下单流程 选择优惠券
 */
exports.selectCoupon = (req, res) => {

    let headerData = headerModel.setNav({
        navTitle: '选择优惠券',
        backUrl: helpers.urlFormat('/cart/new/orderEnsure'),
        navBtn: false
    });

    res.render('select-coupon', {
        module: 'cart',
        page: 'select-coupon',
        title: '选择优惠券',
        selectCouponPage: true,
        pageHeader: headerData,
        pageFooter: true
    });
};

/**
 * 购物车结算--获取优惠券列表
 */
exports.couponList = (req, res, next) => {
    let uid = req.user.uid;

    if (req.xhr) {
        return cartModel.getCouponList(uid)
            .then(data => {
                res.json(data);
            });
    } else {
        return next();
    }

};

/**
 * 购物车输入优惠券码使用优惠券
 */
exports.couponSearch = (req, res, next) => {
    let couponCode = req.body.couponCode || '';
    let uid = req.user.uid;

    co(function* () {
        if (req.xhr) {
            let result = yield cartModel.searchCoupon(uid, couponCode);

            res.json(result);
        } else {
            return next();
        }

    })();
};

/**
 * 选择地址
 */
exports.selectAddress = (req, res, next) => {
    let uid = req.user.uid;

    return addressModel.addressData(uid).then(address => {
        let moreUrl = req.get('Referer') || '/cart/index/new/orderEnsure'; // 取跳过来的url

        address = address.data;

        // 购物车订单进来,秒杀进来
        if (
            moreUrl.indexOf('/cart/index/new/orderEnsure') !== -1 ||
            moreUrl.indexOf('/cart/index/seckill') !== -1
        ) {
            req.session.addressMore = moreUrl; // TODO: 注意cookie-session
        }

        // moreUrl = req.session.addressMore;

        let headerData = headerModel.setNav({
            navTitle: '选择地址',
            navBtn: false,
            backUrl: moreUrl
        });

        res.render('select-address', {
            module: 'cart',
            page: 'select-address',
            pageHeader: headerData,
            pageFooter: true,
            moreUrl,
            address
        });
    }).catch(next);
};

/**
 * 发票信息
 */
exports.invoiceInfo = (req, res) => {
    let uid = req.user.uid;
    let cookieData = req.cookies['order-info'];
    let orderInfo = JSON.parse(cookieData);

    co(function* () {
        let userData = yield userModel.queryProfile(uid);
        let mobile = _.get(userData, 'data.mobile', '');
        let addresslist = yield userModel.addressTextData(uid);
        let returnData = orderModel.processInvoiceData(orderInfo, mobile, addresslist);
        let headerData = headerModel.setNav({
            navTitle: '发票信息',
            navBtn: false
        });

        res.render('select-invoice', _.assign(returnData, {
            pageHeader: headerData,
            module: 'cart',
            page: 'select-invoice'
        }));
    })();
};