coupons.js 2.6 KB

'use strict';

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

let CouponsModel = require('../models/CouponsModel');
const helpers = global.yoho.helpers;
const setPager = require(`${global.utils}/pager`).setPager;

const index = (req, res)=>{
    let uid = '8041246',
        type = req.query.type || CouponsModel.UNUSED,
        page = req.query.page || 1,
        limit = 10;

    co(function*() {
        let coupons = yield CouponsModel.getCouponsList(uid, type, page, limit);

        let data = {};

        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: '未使用优惠券'
            },
            {

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

        if (!coupons.error) {
            data.pager = {
                hasCheckAll: false,
                count: coupons.pager.total || 0,
                curPage: coupons.pager.page || 0,
                totalPages: coupons.pager.pageTotal || 0
            };

            if (data.pager.totalPages > 1) {
                Object.assign(data.pager, setPager(data.pager.totalPages, {
                    type: type,
                    page: data.pager.curPage
                }));
            }
        }

        res.render('coupons', data);
    })();
};

module.exports = {
    index
};