orderDetail.js 5.92 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');

const orderDetailData = (req, res, next) => {
    let uid = req.user.uid;
    let orderCode = req.query.order_code;
    let serviceUrl = _.get(req.app.locals.wap, 'clientService.new', false) ?
        helpers.urlFormat('/service/im') :
        'http://chat8.live800.com/live800/chatClient/chatbox.jsp?companyID=620092&configID=149091&jid=8732423409&info=';

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

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

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

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

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

};

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

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

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

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

// 取消订单
const 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;

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

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

    if (!orderCode) {
        return res.json({
            code: 404,
            msg: '订单号不存在!'
        });
    }
    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);
};

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

/**
 * 订单修改地址页面
 */
const 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;

    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.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: '修改地址',
                navBtn: false
            }),
            module: 'home',
            page: 'order-address-modify',
            localCss: true
        }));
    }).catch(next);
};

/**
 * 修改订单地址
 * @param {*} req
 * @param {*} res
 * @param {*} next
 */
const changeAddress = (req, res, next) => {
    let uid = req.user.uid;

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

    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
    }).then(result => {
        res.json(result);
    }).catch(next);
};

module.exports = {
    orderDetailData,
    delOrder,
    readdData,
    cancelOrder,
    refundApply,
    logistic,
    addressModify,
    changeAddress
};