coupons.js 2.24 KB

'use strict';

const Promise = require('bluebird');
const co = Promise.coroutine;

let CouponsModel = require('../models/CouponsModel');
const helpers = global.yoho.helpers;


const index = (req, res, next)=>{
    let uid = '8041246';
    let type = req.query.type || CouponsModel.UNUSED;
    let page = '';
    let limit = '';
    co(function*() {
        let coupons = yield CouponsModel.getCouponsList(uid, type, page, limit);
        let data = {};
        data.pager = {
            hasCheckAll: false,
            count: coupons.pager.total || 0,
            curPage: coupons.pager.page || 0,
            totalPages: coupons.pager.pageTotal || 0
        };
        if (type === CouponsModel.UNUSED) {
            if (!coupons.list.length) {
                data.unUseCoupons = {empty: '您没有优惠券'};
            } else {
                data.unUseCoupons = coupons.list;
            }
            data.unUse = true;
        } else if (type === CouponsModel.USED) {
            if (!coupons.list.length) {
                data.usedCoupons = {empty: '您没有优惠券'};
            } else {
                data.usedCoupons = coupons.list;
            }
            data.used = true;
        }
        else if (type === CouponsModel.INVALID) {
            if (!coupons.list.length) {
                data.noValidCoupons = {empty: '您没有优惠券'};
            } else {
                data.noValidCoupons = coupons.list;
            }
            data.noValid = true;
        }

        data.tabs = [
            {
                active: type === CouponsModel.UNUSED ? true : false,
                url: helpers.urlFormat('/home/coupons', {type: CouponsModel.UNUSED}),
                name: '未使用优惠券'
            },
            {
                ctive: type === CouponsModel.USED ? true : false,
                url: helpers.urlFormat('/home/coupons', {type: CouponsModel.USED}),
                name: '已使用优惠券'
            },
            {
                ctive: type === CouponsModel.INVALID ? true : false,
                url: helpers.urlFormat('/home/coupons', {type: CouponsModel.INVALID}),
                name: '已失效优惠券'
            }
        ];
        res.render('coupons', data);
    })();
};

module.exports = {
    index
};