BuyNowController.js 4.17 KB
/*
 * @Author: Targaryen
 * @Date: 2017-06-21 10:15:38
 * @Last Modified by: Targaryen
 * @Last Modified time: 2017-06-22 15:11:44
 */
const co = require('bluebird').coroutine;
const headerModel = require('../../../doraemon/models/header');
const BuyNowModel = require('../models/BuyNowModel');
const utils = '../../../utils';
const paymentProcess = require(`${utils}/payment-process`);
const logger = global.yoho.logger;

// cookie 参数
const actCkOpthn = {
    path: '/cart/index'
};

class BuyNowController {
    /**
     * 确认订单页面
     * @param {*} req
     * @param {*} res
     * @param {*} next
     */
    orderEnsure(req, res, next) {
        let orderInfo;

        try {
            orderInfo = JSON.parse(req.cookies['buynow-order-info']);
        } catch (e) {
            logger.info(`orderEnsure: get buynow-order-info from cookie error:${JSON.stringify(e)}`);
            orderInfo = {};
            res.clearCookie('buynow-order-info', actCkOpthn);
        }

        co(function * () {
            let result = yield req.ctx(BuyNowModel).payment({
                uid: req.user.uid,
                product_sku: req.query.product_sku || '2026740', // TODO
                sku_type: req.query.sku_type,
                buy_number: req.query.buy_number,
            });
            let computeData = yield req.ctx(BuyNowModel).compute({
                uid: req.user.uid,
                product_sku: req.query.product_sku || '2026740', // TODO
                sku_type: req.query.sku_type,
                buy_number: req.query.buy_number,
                use_yoho_coin: parseInt(orderInfo.use_yoho_coin, 10)
            });
            let headerData = headerModel.setNav({
                navTitle: '确认订单',
                navBtn: false
            });

            return res.render('buynow/order-ensure', {
                pageHeader: headerData,
                module: 'buynow',
                page: 'order-ensure',
                title: '确认订单',
                orderEnsure: paymentProcess.tranformPayment(result.data, orderInfo, null, null, computeData.data)
            });

        })().catch(next);
    }

    /**
     * 参数更改,重新运算结算数据
     * @param {*} req
     * @param {*} res
     * @param {*} next
     */
    orderCompute(req, res, next) {
        co(function * () {
            let result = yield req.ctx(BuyNowModel).compute({
                uid: req.user.uid,
                cart_type: req.body.cart_type,
                delivery_way: req.body.delivery_way,
                payment_type: req.body.payment_type,
                product_sku: req.body.product_sku || '2026740', // TODO
                coupon_code: req.body.coupon_code,
                use_yoho_coin: req.body.use_yoho_coin,
            });

            result.data.use_yoho_coin = paymentProcess.transPrice(result.data.use_yoho_coin);
            result.data.yohoCoinCompute = paymentProcess.yohoCoinCompute(result.data);
            return res.json(result.data);
        })().catch(next);
    }

    /**
     * 提交订单
     * @param {*} req
     * @param {*} res
     * @param {*} next
     */
    orderSub(req, res, next) {
        let qhy_union = ''; // TODO

        co(function * () {
            let result = yield req.ctx(BuyNowModel).submit({
                uid: req.user.uid,
                cart_type: req.body.cart_type,
                address_id: req.body.address_id,
                delivery_time: req.body.delivery_time,
                delivery_way: req.body.delivery_way,
                use_yoho_coin: req.body.use_yoho_coin,
                use_red_envelopes: req.body.use_red_envelopes,
                payment_id: req.body.payment_id,
                payment_type: req.body.payment_type,
                product_sku_list: req.body.product_sku_list,
                is_print_price: req.body.is_print_price,
                qhy_union: qhy_union,
                invoices_title: req.body.invoices_title,
                invoices_type_id: req.body.invoices_type_id,
                remark: req.body.remark,
                activity_id: req.body.activity_id
            });

            return res.json(result);
        })().catch(next);
    }
}

module.exports = new BuyNowController();