ticketsConfirm.js 2.25 KB
'use strict';

const headerModel = require('../../../doraemon/models/header'); // 头部model
const ticketsConfirmModel = require('../models/ticketsConfirm');

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

const ticketsConfirm = (req, res) => {
    let headerData = headerModel.setNav({
        navTitle: '确认订单',
        navBtn: false
    });

    let responseData = {
        pageHeader: headerData,
        module: 'cart',
        page: 'tickets-confirm',
        title: '确认订单',

        // pageFooter: true,
        localCss: true,
        navBtn: false,
        width750: true
    };

    let orderInfo;

    try {
        orderInfo = JSON.parse(req.cookies['order-info']);
    } catch (e) {
        orderInfo = {};
        res.cookie('order-info', null, actCkOpthn);
    }

    let params = {
        uid: req.user.uid,
        productSku: req.body.productSku,
        buyNumber: req.body.buyNumber,
        useYohoCoin: orderInfo ? orderInfo.yohoCoin : 0,
        yohoCoinMode: true
    };

    req.ctx(ticketsConfirmModel).ticketsConfirm(params).then(result => {
        res.render('ticketsConfirm', Object.assign(responseData, result));
    });
};

const submitTicket = (req, res) => {
    let params = {
        uid: req.user.uid,
        productSku: req.body.productSku,
        buyNumber: req.body.buyNumber,
        mobile: req.body.mobile,
        useYohoCoin: req.body.useYohoCoin
    };

    req.ctx(ticketsConfirmModel).submitTicket(params).then(result => {
        if (result === {}) {
            result.message = '人太多啦,稍后再试!';
        }

        // 提交成功清除Cookie
        res.cookie('order-info', null, actCkOpthn);
        res.json(result);
    });
};

const checkTickets = (req, res) => {
    let params = {
        uid: req.user.uid,
        productSku: req.body.productSku,
        buyNumber: req.body.buyNumber,
        useYohoCoin: req.body.useYohoCoin
    };

    // 未登录
    if (!req.user.uid) {
        return res.json({
            code: 401,
            redirect: '/signin.html'
        });
    } else {
        req.ctx(ticketsConfirmModel).checkTickets(params).then(result => {
            res.json(result);
        });
    }
};

module.exports = {
    ticketsConfirm,
    submitTicket,
    checkTickets
};