coupons.js
2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
'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
};