orderDetail.js 5.84 KB
/**
 * 个人中心 controller
 * @author: chengyao.guo<chengyao.guo@yoho.cn>
 * @date: 2016/08/10
 */

'use strict';

const helpers = global.yoho.helpers;
const _ = require('lodash');
const headerModel = require('../../../doraemon/models/header'); // 头部model
const orderDetailModel = require('../models/orderDetail');
const orderModel = require('../models/order');
const payModel = require('../../cart/models/pay');
const addressModel = require('../models/address');
const addressProcess = require(global.utils + '/address-process');

exports.orderDetailData = (req, res, next) => {
    let uid = req.user.uid;
    let orderCode = req.query.order_code;
    let serviceUrl = helpers.urlFormat('/service/im');

    if (req.query.openId) {
        // 微信支付成功,发送支付确认接口
        req.ctx(payModel).sendPayConfirm(orderCode, 22, uid);
    }

    Promise.all([
        req.ctx(orderDetailModel).orderDetailData(uid, orderCode),
        req.ctx(orderDetailModel).closeReasons(),
        req.ctx(orderModel).refundReason()
    ]).then(result => {
        let orderDetail = result[0];
        let cancelReason = result[1];
        let refundReason = result[2];

        if (!orderDetail || _.isEmpty(orderDetail)) {
            return next(404);
        }

        Object.assign(orderDetail, {
            cancelReason: cancelReason
        });

        orderDetail.serviceUrl = serviceUrl;
        res.render('orderDetail', {
            module: 'home',
            page: 'orderdetail',
            pageHeader: headerModel.setNav({
                navTitle: '订单详情'
            }),
            title: '订单详情',
            pageFooter: true,
            orderDetail: orderDetail,
            refundReason: refundReason,
            localCss: true,
        });
    }).catch(next);

};

// 删除订单
exports.delOrder = (req, res, next) => {
    let orderCode = req.query.id;
    let uid = req.user.uid;

    req.ctx(orderDetailModel).delOrder(orderCode, uid).then((result) => {
        res.json(result);
    }).catch(next);
};

// 再次购买
exports.readdData = (req, res, next) => {
    let orderCode = req.query.orderCode;
    let uid = req.user.uid;

    req.ctx(orderDetailModel).readdData(orderCode, uid).then((result) => {
        res.json(result);
    }).catch(next);
};

// 取消订单
exports.cancelOrder = (req, res, next) => {
    let orderCode = req.query.id;
    let uid = req.user.uid;
    let reasonId = req.query.reasonId || 1;
    let gender = req.query.gender || '1,3';
    let channel = req.query.channel || 1;
    let reason = req.query.reason;

    req.ctx(orderDetailModel).cancelOrder(orderCode, uid, reasonId, gender, channel, reason).then((result) => {
        res.json(result);
    }).catch(next);
};

/**
 * 申请退款
 */
exports.refundApply = (req, res, next) => {
    let orderCode = req.query.id;

    if (!orderCode) {
        return res.json({
            code: 404,
            msg: '订单号不存在!'
        });
    }
    req.ctx(orderDetailModel).refundApply({
        orderCode: req.query.id,
        uid: req.user.uid,
        reasonId: req.query.reasonId || 1,
        reason: req.query.reason
    }).then(result => {
        res.json(result);
    }).catch(next);
};

/**
 * 我的订单-查看物流信息
 */
exports.logistic = (req, res, next) => {
    req.ctx(orderDetailModel).logistics({
        order_code: req.query.order_code,
        uid: req.user.uid,
        type: req.query.type,
        id: req.query.id
    }).then(result => {
        res.render('logistic', _.assign(result, {
            pageHeader: headerModel.setNav({
                navTitle: '物流信息',
                navBtn: false
            }),
            module: 'home',
            page: 'logistic',
            logisticInfoPage: true,
            title: '物流信息'
        }));
    }).catch(next);
};

/**
 * 订单修改地址页面
 */
exports.addressModify = (req, res, next) => {
    let uid = req.user.uid;
    let orderCode = req.query.orderCode;
    let areaCode = req.query.areaCode;
    let provinceAreaCode = req.query.provinceAreaCode || false;

    req.ctx(addressModel)._getAddressData({uid: uid}).then(result => {
        let userAddressList = result;
        let addressData = addressProcess.getAddressByAreaCode(areaCode, _.get(userAddressList, 'data', [])) || {};

        Object.assign(addressData, {
            orderCode: orderCode,
        });

        if (provinceAreaCode) {
            // 不允许修改省份,字符串拆分
            let areaArray = addressData.area ? addressData.area.split(' ') : [];
            let area = _.slice(areaArray, 1);

            Object.assign(addressData, {
                changeProvince: false,
                provinceCode: provinceAreaCode,
                province: areaArray[0],
                area: area
            });
        } else {
            Object.assign(addressData, {
                changeProvince: true,
            });
        }

        res.render('order-address-modify', Object.assign(addressData, {
            pageHeader: headerModel.setNav({
                navTitle: '修改地址'
            }),
            module: 'home',
            page: 'order-address-modify',
            localCss: true
        }));
    }).catch(next);
};

/**
 * 修改订单地址
 * @param {*} req
 * @param {*} res
 * @param {*} next
 */
exports.changeAddress = (req, res, next) => {
    let uid = req.user.uid;
    let udid = req.cookies._yasvd || 'yoho';

    if (!uid) {
        return res.json({
            code: 401,
            message: '用户数据不存在!'
        });
    }

    req.ctx(orderDetailModel).changeAddress({
        uid: req.user.uid,
        orderCode: req.body.orderCode,
        username: req.body.username,
        areaCode: req.body.area_code,
        address: req.body.address,
        mobile: req.body.mobile,
        udid: udid
    }).then(result => {
        res.json(result);
    }).catch(next);
};