coupon-new.js
3.27 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
const co = Promise.coroutine;
const _ = require('lodash');
const headerModel = require('../../../doraemon/models/header'); // 头部model
const CouponNewModel = require('../models/coupon-new');
const helpers = global.yoho.helpers;
/**
* 优惠券数据处理
*/
const _couponProcess = (couponList, type) => {
_.map(couponList, coupon => {
if (type === 'use') {
coupon.is_used = true;
}
if (type === 'use' || coupon.is_overtime === 'Y' || coupon.is_invalid === 'Y') {
coupon.usedOvertimeOrInValid = true;
}
coupon.useNowLink = helpers.urlFormat(`/list/cc${coupon.coupon_code}-cd${coupon.coupon_id}?intro_text=以下商品可使用【${coupon.coupon_name}】优惠券&title=优惠活动商品`, '', null); // eslint-disable-line
return coupon;
});
return couponList;
};
class CouponNew {
page(req, res, next) {
let uid = req.user.uid;
co(function* () {
let [couponsApi, couponNumApi] = yield Promise.all([
req.ctx(CouponNewModel).couponGet({ uid }),
req.ctx(CouponNewModel).getCouponNums({ uid })
]);
let pageData = _.get(couponsApi, 'data', {});
let couponNum = _.get(couponNumApi, 'data', {});
let couponNumStr = {};
let couponList = _.get(pageData, 'couponList', []);
if (couponList && couponList.length) {
pageData.couponList = _couponProcess(couponList);
}
if (couponNum.notuse > 99) {
couponNumStr.notuse = '99+';
} else {
couponNumStr.notuse = couponNum.notuse + '';
}
if (couponNum.use > 99) {
couponNumStr.use = '99+';
} else {
couponNumStr.use = couponNum.use + '';
}
if (couponNum.overtime > 99) {
couponNumStr.overtime = '99+';
} else {
couponNumStr.overtime = couponNum.overtime + '';
}
res.render('coupons/page', {
module: 'home',
page: 'coupon-new',
localCss: true,
width750: true,
pageData,
couponNumStr,
pageHeader: headerModel.setNav({
navTitle: '优惠券',
myConponPageNavBtn: true,
navBtn: false
})
});
})().catch(next);
}
/**
* 优惠券数据
*/
coupons(req, res, next) {
let uid = req.user.uid;
let type = req.body.type;
let filter = req.body.filter;
let page = req.body.page;
co(function* () {
let couponApi = yield req.ctx(CouponNewModel).couponGet({
uid, type, filter, page
});
let couponsList = _.get(couponApi, 'data.couponList', []);
if (couponsList && couponsList.length) {
couponsList = _couponProcess(couponsList, type);
res.render('coupons/coupons', {
couponsList,
layout: false
});
} else {
res.json([]);
}
})().catch(next);
}
}
module.exports = new CouponNew();