returns.js 4.31 KB
/**
 * 退换货controller
 * @author: yyq<yanqing.yang@yoho.cn>
 * @date: 2016/7/18
 */
'use strict';

const mcHandler = require('../models/menu-crumb-handler');
const returns = require('../models/returns');
const _ = require('lodash');


const index = (req, res, next) => {
    const uid = global.yoho.uid;
    const page = req.query.page;

    returns.getUserReturn(uid, page).then(result => {
        res.display('index', {
            page: 'return-list',
            isMe: true,
            content: Object.assign({
                nav: mcHandler.getMeCrumb('我的退/换货'),
                navigation: mcHandler.getSideMenu('我的退/换货'),
                banner: 'http://placehold.it/{width}x{height}'
            }, result)
        });
    }).catch(next);
};

const refund = (req, res, next) => {
    let uid = req.user.uid;
    let code = parseInt(req.params.orderCode, 10);

    if (!uid || !code) {
        return next();
    }

    returns.getRefundGoodsData(code, uid).then(result => {
        res.display('index', {
            page: 'refund',
            content: result
        });
    }).catch(next);
};

const refundApply = (req, res, next) => {
    let orderCode = req.body.orderCode,
        uid = req.user.uid,
        goods = req.body.goods,
        payment = req.body.payment;

    if (!orderCode || _.isEmpty(goods) || _.isEmpty(payment)) {
        return res.json({
            code: 203,
            message: '非法提交'
        });
    }

    returns.saveRefund(orderCode, uid, goods, payment).then(result => {
        res.json(result);
    }).catch(next);

};

const refundDetail = (req, res, next) => {
    let applyId = parseInt(req.params.applyId, 10),
        uid = req.user.uid;

    if (!uid || !applyId) {
        return next();
    }

    returns.getRefundDetailData(applyId, uid).then(result => {
        res.display('index', {
            page: 'refund-detail',
            content: result
        });
    }).catch(next);
};

const exchange = (req, res, next) => {
    const code = req.params.orderCode;
    const uid = req.user.uid || 8050882;

    returns.getChangeGoodsList(code, uid).then(result => {
        res.display('index', {
            page: 'exchange',
            isMe: true,
            content: Object.assign({
                nav: mcHandler.getMeCrumb('我的退/换货'),
                navigation: mcHandler.getSideMenu('我的退/换货'),
                banner: 'http://placehold.it/{width}x{height}'
            }, result)
        });
    }).catch(next);
};

const getProductInfo = (req, res, next) => {
    const productId = req.query.productId;
    const productSkn = req.query.productSkn;

    returns.getProductInfo(productId, productSkn).then(result => {
        res.json(result);
    }).catch(next);
};

const exchangeDeatail = (req, res) => {
    let id = parseInt(req.params.applyId, 10),
        uid = req.user.uid;

    // let id = 125946;
    // let uid = 8050560;

    returns.getExchangeDetailData(id, uid).then(result => {

        res.display('index', {
            page: 'exchange-detail',
            isMe: true,
            content: {
                nav: mcHandler.getMeCrumb('我的退/换货'),
                navigation: mcHandler.getSideMenu('我的退/换货'),
                banner: 'http://placehold.it/{width}x{height}',
                returns: {
                    exchange: result.exchangeDetail
                }
            }
        });

    });
};

const exchangeSubmit = (req, res, next) => {
    const uid = req.user.uid || '8050882';

    returns.submitChange(req.query, uid).then(result => {
        res.json(result);
    }).catch(next);
};

const cancelApply = (req, res, next) => {
    const uid = req.user.uid;
    const id = req.body.id;
    const type = req.body.type === 'exchange';

    if (!uid || !id) {
        return next();
    }

    returns.cancelReturnApply(id, uid, type).then(result => {
        res.json(result);
    }).catch(next);
};

const setEepress = (req, res, next) => {
    const uid = req.user.uid;
    const param = req.body;

    if (!uid) {
        return next();
    }

    returns.setBackEepress(uid, param).then(result => {
        res.json(result);
    }).catch(next);
};

module.exports = {
    index,
    refund,
    refundApply,
    exchange,
    getProductInfo,
    refundDetail,
    exchangeDeatail,
    exchangeSubmit,
    cancelApply,
    setEepress
};