refund.js 5.57 KB
/**
 * 退换货
 * @type {Object}
 */
'use strict';

const _ = require('lodash');
const headerModel = require('../../../doraemon/models/header'); // 头部model
const refundModel = require('../models/refund');
const notLoginCode = 400;
const notLoginTip = '抱歉,您暂未登录!';
const helpers = global.yoho.helpers;

const refund = {
    refund(req, res) {

        let headerData = headerModel.setNav({
            navTitle: '退货申请',
            suggestSub: {
                text: ''
            },
            navBtn: false
        });

        res.render('refund/refund', {
            module: 'home',
            pageHeader: headerData,
            page: 'refund',
            localCss: true,
            title: '退货申请'
        });
    },
    order(req, res, next) {
        const uid = req.user.uid;
        const orderCode = req.query.orderCode;

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

        refundModel.getOrderData(uid, orderCode).then(result => {
            res.json(result);
        }).catch(next);
    },
    submit(req, res, next) {
        const uid = req.user.uid;

        refundModel.submitRefundData(uid, req.body).then(result => {
            res.json(result);
        }).catch(next);
    },

    /**
     * 退换货 - 商品寄回信息
     * @param {*} req
     * @param {*} res
     * @param {*} next
     */
    logistics(req, res) {
        let headerData = headerModel.setNav({
            navTitle: '填写物流信息',
            backUrl: helpers.urlFormat('/home/orders')
        });

        res.render('refund/logistics', {
            module: 'home',
            page: 'refund-logistics',
            pageHeader: headerData,
            applyid: req.query.applyid,
            type: req.query.type,
            localCss: true,
            title: '填写物流信息'
        });
    },

    /**
     * 获取快递公司名称
     * @param {*} req
     * @param {*} res
     * @param {*} next
     */
    getCompanyList(req, res, next) {
        refundModel.getExpressCompany().then(result => {
            res.json(_.get(result, 'data', []));
        }).catch(next);
    },

    saveLogistics(req, res, next) {
        const uid = req.user.uid;
        const applyid = req.body.applyid;

        refundModel.setexpress(applyid, uid, {
            type: req.body.type,
            expressCompany: req.body.expressCompany,
            expressNumber: req.body.expressNumber,
            expressId: req.body.expressId
        }).then(data => {
            return res.json(data);
        }).catch(next);
    },
    refundStatus(req, res) {
        const applyId = req.params.applyId;

        let headerData = headerModel.setNav({
            navTitle: '退货状态',
            backUrl: helpers.urlFormat('/home/return'),
            navBtn: false
        });

        res.render('refund/status', {
            module: 'home',
            pageHeader: headerData,
            page: 'refund-status',
            applyId: applyId,
            type: 'refund',
            localCss: true,
            title: '退货状态'
        });
    },
    exchangeStatus(req, res) {
        const applyId = req.params.applyId;

        let headerData = headerModel.setNav({
            navTitle: '换货状态',
            backUrl: helpers.urlFormat('/home/return'),
            navBtn: false
        });

        res.render('refund/status', {
            module: 'home',
            pageHeader: headerData,
            page: 'refund-status',
            applyId: applyId,
            type: 'exchange',
            localCss: true,
            title: '换货状态'
        });
    },
    statusDetail(req, res, next) {
        const uid = req.user.uid;
        const applyid = req.query.applyid;
        const type = req.query.type;

        if (type === 'refund') {
            refundModel.getRefundDetail(applyid, uid).then(result => {
                return res.json(result);
            }).catch(next);
        } else {
            refundModel.getChangeDetail(applyid, uid).then(result => {
                return res.json(result);
            }).catch(next);
        }
    },

    /**
     * 退换货订单列表
     * @param {*} req
     * @param {*} res
     */
    refundOrders(req, res) {
        let headerData = headerModel.setNav({
            navTitle: '退换货订单列表'
        });

        res.render('refund/refund-order', {
            module: 'home',
            page: 'refund-order',
            pageHeader: headerData,
            localCss: true,
            title: '退换货订单列表'
        });
    },

    /**
     * 获取退换货订单
     * @param req
     * @param res
     * @returns {*|{read, write}}
     */
    getRefundOrders(req, res, next) {
        const uid = req.user.uid;

        if (!uid && req.xhr) {
            return res.json({
                code: notLoginCode,
                message: notLoginTip
            });
        }
        let param = {
            uid: uid,
            page: req.query.page,
            limit: req.query.limit
        };

        refundModel.getRefundOrders(param).then(result => {
            return res.json(result);
        }).catch(next);
    },

    /**
     * 取消申请
     * @param req
     * @param res
     */
    cancelApply(req, res, next) {
        const uid = req.user.uid;
        const id = req.body.id;

        if (!uid && req.xhr) {
            return res.json({
                code: notLoginCode,
                message: notLoginTip
            });
        }
        refundModel.cancelRefundApply(uid, id).then(result => {
            return res.json(result);
        }).catch(next);
    }
};

module.exports = refund;