coupons-model.js
4.88 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
'use strict';
const Promise = require('bluebird');
const co = Promise.coroutine;
const UserData = require('./user-data');
const helpers = global.yoho.helpers;
const path = require('path');
const _ = require('lodash');
// 使用 product中的分页逻辑
const pagerPath = path.join(global.appRoot, '/apps/product/models/public-handler.js');
const pager = require(pagerPath).handlePagerData;
const UNUSED = 'notuse';
const USED = 'use';
const INVALID = 'overtime';
const getCouponsList = (uid, type, page, limit)=>{
return co(function*() {
let couponsInfo = yield UserData.getCouponsList(uid, type, page, limit);
let result = [];
if (!_.get(couponsInfo, 'data.couponList')) {
return {
list: result
};
}
let coupons = _.get(couponsInfo, 'data.couponList');
if (coupons) {
coupons.forEach(function(item, i) {
result[i] = {};
result[i].id = item.couponId;
result[i].code = item.couponCode;
// 格式化有效日期 "couponValidity": "2016.03.15-2016.03.31"
let dates = item.couponValidity.split('-');
result[i].beginTime = dates[0].replace('.', '-');
result[i].endTime = dates[1].replace('.', '-');
if (!item.couponImageUrl) {
result[i].img = '//static.yohobuy.com/images/v2/activity/default_coupon.jpg';
} else {
result[i].img = item.couponImageUrl;
}
if ((item.overTime - Date.now()) < 259200) {
result[i].endSoon = true;
} else {
result[i].endSoon = false;
}
result[i].value = item.couponValue.toFixed(2);
result[i].validity = item.couponValidity;
result[i].useRemark = item.couponDetailInfomation;
if (type === USED) {
result[i].orderNum = _.get(item, 'orderCode', '');
result[i].orderDetailUrl = helpers.urlFormat('/home/orders/detail', {orderCode: item.orderCode || ''});
result[i].orderSum = _.get(item, 'orderPrice', 0).toFixed(2);
result[i].payment = _.get(item, 'actuallyPaid', 0).toFixed(2);
let data = result[i].usedTime ? moment(result[i].usedTime).format('YYYY-MM-DD') : 0;
if (data) {
result[i].useTime = new Date(data).getTime();
} else {
result[i].useTime = '';
}
} else {
result[i].status = item.isValidity ? '可使用' : '已过期';
}
if (item.couponType) {
result[i].type = item.couponType;
if (item.couponType == 5) {
result[i].value = '免邮';
}
}
});
}
let pageNum = pager(couponsInfo.data.total, {
page: page,
limit: limit,
type: type
})
return {
list: result,
pager: Object.assign({
count: couponsInfo.data.total || 0,
curPage: page,
totalPages: couponsInfo.data.totalPageNum
}, pageNum)
};
})();
};
const couponsData = (uid, params)=>{
let type = params.type || UNUSED;
let page = params.page || 1;
let limit = params.limit || 10;
return co(function*() {
let coupons = yield getCouponsList(uid, type, page, limit);
let data = {};
console.log(coupons);
if (type === UNUSED) {
data.unUseCoupons = !coupons.list.length ? {empty: '您没有优惠券'} : coupons.list;
data.unUse = true;
} else if (type === USED) {
data.usedCoupons = !coupons.list.length ? {empty: '您没有优惠券'} : coupons.list;
data.used = true;
} else if (type === INVALID) {
data.noValidCoupons = !coupons.list.length ? {empty: '您没有优惠券'} : coupons.list;
data.noValid = true;
}
data.tabs = [
{
active: type === UNUSED ? true : false,
url: helpers.urlFormat('/home/coupons', {type: UNUSED}),
name: '未使用优惠券'
},
{
active: type === USED ? true : false,
url: helpers.urlFormat('/home/coupons', {type: USED}),
name: '已使用优惠券'
},
{
active: type === INVALID ? true : false,
url: helpers.urlFormat('/home/coupons', {type: INVALID}),
name: '已失效优惠券'
}
];
data.pager = coupons.pager;
return data;
})();
};
module.exports = {
couponsData
};