ticket.js 2.19 KB
/**
 * 立即购买controller
 * @author: yyq<yanqing.yang@yoho.cn>
 * @date: 2016/9/23
 */
'use strict';

const headerModel = require('../../../doraemon/models/simple-header');
const ticketService = require('../models/ticket-service');
const _ = require('lodash');
const helpers = global.yoho.helpers;

const stepper = [
    { name: '填写订单', focus: true },
    { name: '付款,完成购买' }
];

const ticketEnsure = (req, res, next) => {
    let uid = req.user.uid;
    let sku = req.query.productSku || 0;
    let buyNumber = req.query.buyNumber || 0;
    let skn = req.query.productSkn || 0;

    ticketService.addTicket(uid, sku, buyNumber).then(result => {
        let header = headerModel.setSimpleHeaderData() || {};

        result.stepper = stepper;

        if (result.error) {
            result.productUrl = helpers.getUrlBySkc(skn);
        }

        res.render('ticket-ensure', {
            title: '填写订单 | ' + (res.locals.title || ''),
            page: 'ticket',
            content: result,
            simpleHeader: header,
            pageClass: 'order-easypay-page'
        });
    }).catch(next);
};

const ticketSubmit = (req, res, next) => {
    let uid = req.user.uid;
    let sku = req.body.sku || 0;
    let count = req.body.count || 0;
    let mobile = req.body.mobile || 0;
    let yohoCoin = req.body.coin || 0;

    if (!sku || !count || !mobile) {
        return res.json({
            code: 410,
            message: '参数错误'
        });
    }

    ticketService.submitTicket(uid, sku, count, mobile, yohoCoin).then(result => {
        return res.json(result);
    }).catch(next);
};

const ticketCompute = (req, res, next) => {
    let uid = req.user.uid;
    let sku = req.body.sku || 0;
    let buyNumber = req.body.count || 0;
    let yohoCoin = req.body.coin || 0;

    ticketService.addTicket(uid, sku, buyNumber, yohoCoin).then(result => {
        if (_.isEmpty(result)) {
            return res.json({
                code: 401,
                message: '不成功'
            });
        }

        return res.json({
            code: 200,
            data: result
        });
    }).catch(next);
};

module.exports = {
    ticketSubmit,
    ticketEnsure,
    ticketCompute
};