sale-handler.js
7.78 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
* @Author: Targaryen
* @Date: 2016-05-25 18:16:59
* @Last Modified by: Targaryen
* @Last Modified time: 2016-06-20 21:31:11
*/
'use strict';
const helpers = global.yoho.helpers;
const _ = require('lodash');
const anHour = 3600000;
const aDay = anHour * 24;
const timeFormat = {
d: '剩{d}天',
h: '剩{h}小时',
m: '剩{m}分钟',
s: '剩{s}秒',
dh: '剩{d}天{h}小时',
dhms: '剩{d}天{h}小时{m}分钟{s}秒',
hms: '剩{h}小时{m}分钟{s}秒',
ms: '剩{m}分钟{s}秒'
};
/**
* 折扣专场专题列表过期时间处理
* @param time 接口返回的时间
* @returns {*|string}
*/
const processTime = (time) => {
let data = {};
let type = '';
if (time < anHour) {
data.warnColor = true;
type = 'm';
data.time = helpers.dayHourMinuteFromDiff(timeFormat[type], time, 'ms');
} else {
if (time > aDay) {
type = 'dh';
} else {
type = 'h';
}
data.time = helpers.dayHourMinuteFromDiff(timeFormat[type], time, 'ms');
}
return data.time;
};
/**
* 折扣文本切割
* @param text 要切割的文本
* @param targetString 目标字符,为null时按字符数字切割
* @returns {{discount: *, discountText: *}}
*/
const discountSplit = (text, targetString) => {
let endNum = 0,
i;
for (i = 0; i < text.length; i++) {
if (targetString !== null) {
if (text[i] !== targetString) {
endNum = i + 1;
}
} else {
if (/^([0-9]|\%)*$/.test(text[i])) {
endNum = i + 1;
}
}
}
return {
discount: text.slice(0, endNum),
discountText: text.slice(endNum, text.length)
};
};
/**
* 处理折扣专区活动数据
* @param origin 原始数据
* @param channel 频道 boys girls kids lifestyle
* @returns {{}}
*/
exports.handleSaleActivityData = (origin, channel) => {
let dest = {
big: [],
normal: []
};
_.forEach(origin, function(value, key) {
let activity = {
link: helpers.urlFormat('/product/sale/discount/detail', {id: value.id, channel: channel}),
img: value.web_cover_url,
time: processTime(parseInt(value.left_time, 10) * 1000),
brand: value.logo_url,
title: value.title
};
activity = Object.assign(activity, discountSplit(value.promotion_name), null);
if (key < 3) {
dest.big.push(activity);
} else if (key < 27) {
dest.normal.push(activity);
}
});
return dest;
};
/**
* 处理折扣专区标题折扣倒计时等数据
* @param origin 要处理的原始数据
* @returns {{}}
*/
exports.handleDiscountTitleData = (origin) => {
let dest = {
title: origin.title,
up: true,
time: parseInt(origin.left_time, 10) * 1000
};
Object.assign(dest, discountSplit(origin.promotion_name, '起'));
return dest;
};
/**
* 处理首页 banner 数据
* @param origin 要处理的原始数据
* @returns {{}}
*/
exports.handleSaleBannerData = (origin) => {
let dest = {
list: []
};
_.forEach(origin, function(value) {
if (value.template_name === 'focus') {
_.forEach(value.data, function(subValue) {
let banner = {
bannerHeight: 450,
href: subValue.url,
img: subValue.src
};
dest.list.push(banner);
});
}
});
return dest;
};
/**
* 处理首页 banner 小图 threePicture楼层和small_pic楼层都去一遍,产品确认只会有一个接口有数据
* @param origin 要处理的原始数据
* @returns {Array}
*/
exports.handleSaleBannerSmallData = (origin) => {
let dest = [];
let count = 0;
let hasThreePic = false;
_.forEach(origin, function(value) {
if (value.template_name === 'threePicture') {
hasThreePic = true;
}
});
_.forEach(origin, function(value) {
if (hasThreePic) {
if (value.template_name === 'threePicture') {
_.forEach(value.data, function(picList) {
if (count++ < 3) {
let smallPic = {
link: picList.url,
icon: picList.src,
title: picList.title,
desc: picList.alt
};
dest.push(smallPic);
}
});
}
} else {
if (value.template_name === 'small_pic') {
_.forEach(value.data, function(picList) {
if (count++ < 4) {
let smallPic = {
link: picList.url,
icon: picList.src,
title: picList.title,
desc: picList.alt
};
dest.push(smallPic);
}
});
}
}
});
return dest;
};
/**
* 处理首页楼层分类数据
* 修改为取大类前 4 个
* @param origin 要处理的原始数据
* @param saleType 获取数据分属哪个类目:断码区,会员专享,折扣专场等
* @param channel 频道
* @param breakingSizeSort 为断码去额外拼接参数,接口比较垃圾嘛,没办法
* @returns {{}}
*/
exports.handleSaleCategoryData = (origin, saleType, channel, breakingSizeSort) => {
let dest = {
navItem: [
{
title: '今日推荐',
vip: true
}
]
};
let urlLocation = {};
// title urlLocation 处理
switch (saleType) {
case '5':
dest.title = '断码区';
urlLocation = {
url: '?saleType=' + saleType + '&order=s_t_desc&channel=' + channel +
'&breakSize=' + breakingSizeSort.breakSize + '&breakSort=' + breakingSizeSort.breakSort,
localUrl: '?saleType=' + saleType + '&order=s_t_desc&channel=' + channel,
limit: 14
};
break;
case '2':
dest.title = 'VIP会员专享';
urlLocation = {
url: '?saleType=' + saleType + '&order=s_t_desc&channel=' + channel,
limit: 11
};
break;
case '3':
dest.title = '最新降价';
urlLocation = {
url: '?saleType=0&order=s_t_desc&channel=' + channel,
limit: 14
};
break;
default:
break;
}
dest.urlLocation = dest.navItem[0].urlLocation = urlLocation.url + `&limit=${urlLocation.limit}`;
let count = 0;
_.forEach(origin, (value) => {
if (count++ < 4) {
let forBreakingUrl = '';
if (saleType === '1') {
forBreakingUrl = urlLocation.localUrl;
} else {
forBreakingUrl = urlLocation.url;
}
let nav = {
urlLocation: forBreakingUrl + '&sort=' + value.relation_parameter.sort +
`&limit=${(urlLocation.limit + 1)}`,
title: value.category_name,
newDiscount: true
};
dest.navItem.push(nav);
}
});
return dest;
};
/**
* 处理断码区尺码数据,输出整合后的筛选条件
* @param origin
*/
exports.handleSaleBreakingSizeData = (origin) => {
let dest = {
breakSize: '',
breakSort: ''
};
_.forEach(origin, value => {
dest.breakSort += value.sort_id;
_.forEach(value.sub, subValue => {
dest.breakSize += subValue.size_id + ',';
});
});
return dest;
};