coupon.js
5.47 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/**
* activity model
* @author: shenzm<zhimin.shen@yoho.cn>
* @date: 2016/09/29
*/
'use strict';
const _ = require('lodash');
const Promise = require('bluebird');
const api = global.yoho.API;
const crypto = global.yoho.crypto;
const helpers = global.yoho.helpers;
const HeaderModel = require('../../../doraemon/models/header');
const homeService = require('../../product/models/home-service');
const config = global.yoho.config;
exports.getCouponData = (channel, params) => {
return Promise.coroutine(function*() {
const result = {
pathNav: [homeService.getHomeChannelNav(channel), {
name: '领券频道'
}],
footerTop: true,
topBanner: {
list: []
},
categories: []
};
const requestData = yield Promise.all([
api.get('', Object.assign(params, {
method: 'app.promotion.queryCouponCenter'
}), config.apiCache),
HeaderModel.requestHeaderData(channel)
]);
const coupon = requestData[0];
result.headerData = requestData[1].headerData;
do { // eslint-disable-line
if (!coupon.data || !Array.isArray(coupon.data) || coupon.data.length === 0) {
break;
}
coupon.data.forEach(function(val, index) {
// 头部banner
if (val.templateName === 'focus') {
val.data.forEach(function(item) {
result.topBanner.list.push({
href: item.url.replace('http:', ''), // banner跳转链接
img: item.src // banner图片
});
});
} else if (val.template_name === 'getCoupon' && val.data.length) {
// 优惠券楼层
const obj = {
coupons: []
};
// 楼层标题
if (_.get(coupon, `data[${index - 1}].template_name`, '') === 'text') {
obj.title = _.get(coupon, `data[${index - 1}].data.text`, '');
}
val.data.forEach(function(item) {
obj.coupons.push({
id: crypto.encryption('yoho9646abcdefgh', item.couponID), // 加密优惠券号
img: helpers.image(item.image.src, 0, 0), // 优惠券图片
url: item.image.url.replace('http:', '') // 去逛逛链接
});
});
result.categories.push(obj);
}
});
}
while (false);
return result;
})();
};
exports.getCouponStatus = (params) => {
return Promise.coroutine(function*() {
const coupon = yield api.get('', Object.assign(params, {
method: 'app.promotion.queryCouponCenter'
}));
const result = {
code: coupon.code,
categories: []
};
do { // eslint-disable-line
if (!coupon.data || !Array.isArray(coupon.data) || coupon.data.length === 0) {
break;
}
coupon.data.forEach(function(val) {
if (val.template_name === 'getCoupon' && val.data.length) {
// 优惠券楼层
val.data.forEach(function(item) {
const status = Number(item.status);
if ([2, 3].indexOf(status) > -1) {
const cou = {
id: crypto.encryption('yoho9646abcdefgh', item.couponID) // 加密优惠券号
};
if (status === 2) {
cou.empty = true; // 优惠券已抢光
} else if (status === 3) {
cou.got = true; // 优惠券已领取
}
result.categories.push(cou);
}
});
}
});
}
while (false);
return result;
})();
};
exports.sendcoupon = (couponId, uid) => {
let returnData = {};
couponId = crypto.decrypt('yoho9646abcdefgh', couponId);
// 领取优惠券
return api.get('', {
method: 'app.promotion.getCoupon',
couponId: couponId,
uid: uid
}).then(result => {
switch (result.code) {
case 200:
returnData = {
code: 200,
message: '恭喜您,成功领取优惠券',
url: helpers.urlFormat('/home/coupons')
};
break;
case 401:
returnData = {
code: 401,
message: '您已领取过优惠券'
};
break;
case 315:
returnData = {
code: 315,
message: '优惠券已过期'
};
break;
case 300:
returnData = {
code: 300,
message: '请求参数错误'
};
break;
default:
returnData = {
code: 500,
message: result.message
};
break;
}
return returnData;
});
};