sale-handler.js
21.1 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
/*
* @Author: Targaryen
* @Date: 2016-05-25 18:16:59
* @Last Modified by: Targaryen
* @Last Modified time: 2016-06-01 18:58:00
*/
'use strict';
const library = '../../../library';
const helpers = require(`${library}/helpers`);
const _ = require('lodash');
// const aMinute = 60000;
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}秒'
};
/**
* 折扣专场专题列表过期时间处理
*/
const processTime = (time) => {
let data = {};
let type = '';
if (time < anHour) {
data.warnColor = true;
data.time = '即将结束';
} else {
if (time > aDay) {
type = 'dh';
} else {
type = 'h';
}
data.time = helpers.dateDiffFormat(timeFormat[type], time, 'ms');
}
return data.time;
};
/**
* 折扣专场专题详情过期时间处理
* @param {[string]} time
* @return {[boject]}
*/
// const processTimes = (time) => {
// let data = {};
// let type = '';
// if (time > aDay) {
// type = 'dhms';
// } else {
// if (time > anHour) {
// type = 'hms';
// } else {
// if (time > aMinute) {
// type = 'ms';
// } else {
// type = 's';
// }
// }
// }
// data.time = helpers.dateDiffFormat(timeFormat[type], time, 'ms');
// return data;
// };
/**
* 处理用于筛选的 URL
* @param {[type]} originParam [description]
* @param {[type]} newParam [description]
* @return {[type]} [description]
*/
const handleFilterUrl = (originParam, newParam) => {
let dest = '?';
let tempOriginParam = {};
// 创建 originParam、newParam 键的数组
let originParamArray = _.keys(originParam);
let newParamArray = _.keys(newParam);
// 判断参数是否已经存在
let isExist = !_.isEmpty(_.find(originParamArray, function(o) {
return o === newParamArray[0];
}));
if (isExist) {
tempOriginParam = Object.assign(tempOriginParam, originParam);
_.forEach(newParam, function(value, key) {
tempOriginParam[key] = value;
});
} else {
tempOriginParam = Object.assign(tempOriginParam, originParam, newParam);
}
_.forEach(tempOriginParam, function(value, key) {
dest = dest + key + '=' + value + '&';
});
return _.trim(dest, '&');
};
/**
* 处理最新降价折扣信息
* @return {[type]} [description]
*/
const handleSaleNewSaleSortData = (params) => {
var dest;
dest = [{
name: '1~3折',
href: handleFilterUrl(params, { p_d: '0.1,0.3' }),
checked: (params.p_d === '0.1,0.3') ? true : false
}, {
name: '4~6折',
href: handleFilterUrl(params, { p_d: '0.4,0.6' }),
checked: (params.p_d === '0.4,0.6') ? true : false
}, {
name: '7折以上',
href: handleFilterUrl(params, { p_d: '0.7,1' }),
checked: (params.p_d === '0.7,1') ? true : false
}];
return dest;
};
/**
* 处理选中数据
* @param {[type]} origin [description]
* @return {[type]} [description]
*/
const handleCheckedData = (params, origin, param) => {
var dest = [];
// 品牌选中数据
if (!_.isEmpty(origin)) {
_.forEach(origin, (value) => {
if (value.checked) {
let checked = {};
checked.name = value.name;
let tempParam = {};
tempParam[param] = '';
checked.href = handleFilterUrl(params, tempParam);
dest.push(checked);
}
});
}
return dest;
};
/**
* 处理断码区分类筛选数据
* @return {[type]} [description]
*/
const handleSalebreakingYardsSortData = (origin, params) => {
var dest = [];
if (!_.isEmpty(params.sort)) {
// 是否有分类筛选参数,如果有
_.forEach(params.sort.split(','), function(sortParam) {
_.forEach(origin, function(value) {
// 判断是否在这组数据里面
if (!_.isEmpty(_.find(value.sort_id.split(','), o => {
return o === sortParam;
}))) {
// 如果在某组数据里面,组合数据退出循环
_.forEach(value.sub, function(subValue) {
let size = {};
size.checked =
(parseInt(params.size, 10) === parseInt(subValue.size_id, 10)) ? true : false;
size.href = handleFilterUrl(params, { size: subValue.size_id });
size.name = subValue.size_name;
dest.push(size);
});
return false;
}
});
});
} else { // 如果没有筛选参数,返回所有分类
_.forEach(origin, function(value) {
_.forEach(value.sub, function(subValue) {
let size = {};
size.checked = (parseInt(params.size, 10) === parseInt(subValue.size_id, 10)) ? true : false;
size.href = handleFilterUrl(params, { size: subValue.size_id });
size.name = subValue.size_name;
dest.push(size);
});
});
}
return _.uniqWith(dest, _.isEqual);
};
/**
* 处理筛选数据
* @param {[type]} origin [description]
* @return {[type]} [description]
*/
exports.handleSaleFilterData = (origin, params) => {
var dest = {};
dest.brand = {};
dest.price = [];
dest.channel = [];
dest.brand.default = [];
dest.brand.brandsShow = [];
dest.brand.brandIndex = [];
dest.brand.showMore = true;
dest.brand.showMulti = true;
dest.checkedConditions = {};
dest.checkedConditions.conditions = [];
// 品牌索引数据处理
dest.brand.brandIndex = [];
dest.brand.brandIndex[0] = {
index: 'all',
name: '全部'
};
dest.brand.brandIndex[1] = {
index: '0-9',
name: '0~9'
};
for (let i = 65; i < 91; i++) {
dest.brand.brandIndex.push({
index: String.fromCharCode(i).toLowerCase(),
name: String.fromCharCode(i)
});
}
// 品牌数据处理
if (!_.isEmpty(origin.brand)) {
let count = 0;
_.forEach(origin.brand, function(value) {
let brand = {};
brand.checked = (parseInt(params.brand, 10) === parseInt(value.id, 10)) ? true : false;
brand.href = handleFilterUrl(params, { brand: value.id });
brand.name = value.brand_name;
brand.key = value.brand_domain;
if (!_.isEmpty(value.brand_alif)) {
if (!_.isNaN(parseInt(value.brand_alif, 10))) {
brand.index = '0-9';
} else {
brand.index = value.brand_alif.toLowerCase();
}
}
if (count < 10) {
dest.brand.default.push(brand);
}
dest.brand.brandsShow.push(brand);
count++;
});
}
// 来排排序
dest.brand.brandsShow = _.sortBy(dest.brand.brandsShow, function(o) {
return o.index;
});
// 处理 价格 筛选数据
if (!_.isEmpty(origin.priceRange)) {
_.forEach(origin.priceRange, function(value, key) {
let price = {
checked: (params.price === key) ? true : false,
href: handleFilterUrl(params, { price: key }),
name: value
};
dest.price.push(price);
});
}
// 处理频道数据
for (let i = 0; i < 2; i++) {
dest.channel[i] = {};
dest.channel[i].checked = false;
}
dest.channel[0].name = '男生';
dest.channel[0].href = handleFilterUrl(params, { gender: '1,3' });
dest.channel[0].checked = (params.gender === '1,3') ? true : false;
dest.channel[1].name = '女生';
dest.channel[1].href = handleFilterUrl(params, { gender: '2,3' });
dest.channel[1].checked = (params.gender === '2,3') ? true : false;
// 最新降价数据处理
if (parseInt(params.saleType, 10) === 3) {
dest.discount = handleSaleNewSaleSortData(params);
// 最新降价筛选数据处理
dest.checkedConditions.conditions = _.union(dest.checkedConditions.conditions,
handleCheckedData(params, dest.discount, 'p_d'));
}
// 断码区尺码数据处理
if (!_.isEmpty(origin.size)) {
dest.size = handleSalebreakingYardsSortData(origin.size, params);
// 最新降价筛选数据处理
dest.checkedConditions.conditions = _.union(dest.checkedConditions.conditions,
handleCheckedData(params, dest.size, 'size'));
}
// 清除所有选中数据
dest.checkedConditions.clearUrl = '?';
// 处理频道筛选数据
dest.checkedConditions.conditions = _.union(dest.checkedConditions.conditions,
handleCheckedData(params, dest.channel, 'gender'));
// 处理品牌筛选数据
dest.checkedConditions.conditions = _.union(dest.checkedConditions.conditions,
handleCheckedData(params, dest.brand.brandsShow, 'brand'));
// 处理价格筛选数据
dest.checkedConditions.conditions = _.union(dest.checkedConditions.conditions,
handleCheckedData(params, dest.price, 'price'));
return dest;
};
/**
* 处理 opts 排序数据 待处理 待确认
* @param {[type]} origin [description]
* @return {[type]} [description]
*/
exports.handleSaleOptsData = (params, total) => {
var dest = {};
dest.sortType = [];
for (let i = 0; i < 3; i++) {
let opt = {};
switch (i) {
case 0:
opt.href = handleFilterUrl(params, { order: 's_t_desc' });
opt.name = '最新';
break;
case 1:
opt.href = handleFilterUrl(params, { order: 's_p_asc' });
opt.name = '价格';
break;
case 2:
opt.href = handleFilterUrl(params, { order: 'p_d_asc' });
opt.name = '折扣';
break;
default:
break;
}
opt.hasSortOrient = {};
opt.hasSortOrient.active = {};
opt.hasSortOrient.active.desc = {};
dest.sortType.push(opt);
}
// 上下翻页数据处理
dest.pageCounts = [{
href: handleFilterUrl(params, { limit: 100 }),
count: 100
}, {
href: handleFilterUrl(params, { limit: 80 }),
count: 80
}, {
href: handleFilterUrl(params, { limit: 60 }),
count: 60
}];
dest.curPage = _.isEmpty(params.page) ? 1 : params.page; // 当前页码数
// 上一页下一页
let preHref = (!_.isEmpty(params.page) && params.page > 1) ? params.page - 1 : 1;
let nextHref = (!_.isEmpty(params.page)) ? params.page + 1 : 2;
dest.preHref = handleFilterUrl(params, { page: preHref });
dest.nextHref = handleFilterUrl(params, { page: nextHref });
// 每页商品数量
dest.countPerPage = _.isEmpty(params.limit) ? 60 : params.limit;
// 全部页码数量
dest.pageCount = parseInt(total / (_.isEmpty(params.limit) ? 60 : params.limit), 10) + 1;
// 每页多少商品
let paramsLimit = parseInt((_.isEmpty(params.limit) ? 60 : params.limit), 10);
// 商品开始结束数
dest.start = (!_.isEmpty(params.page) ? params.page - 1 : 0) * paramsLimit;
dest.end = parseInt(dest.start, 10) + paramsLimit;
return dest;
};
/**
* 处理页面左侧分类筛选数据
* @return {[type]} [description]
*/
exports.handleSaleSortData = (origin, params) => {
var leftContent = {};
leftContent.allSort = {};
leftContent.allSort.list = [];
_.forEach(origin, function(value) {
let category = {};
category.name = value.category_name;
category.num = value.node_count;
category.childList = [];
category.checked = (params.sort === value.relation_parameter.sort) ? true : false;
category.href = handleFilterUrl(params, { sort: value.relation_parameter.sort });
let gender = !_.isEmpty(params.gender) ? `&gender=${params.gender}` : '';
_.forEach(value.sub, function(subValue) {
category.childList.push({
name: subValue.category_name,
num: subValue.node_count,
href: '?sort=' + subValue.relation_parameter.sort + gender
});
});
leftContent.allSort.list.push(category);
});
return leftContent;
};
/**
* 处理折扣专区活动数据
* @param {[type]} origin [description]
* @return {[type]} [description]
*/
exports.handleSaleActivityData = (origin) => {
var dest = {};
dest.big = [];
dest.normal = [];
_.forEach(origin, function(value, key) {
let activity = {};
activity.link =
helpers.urlFormat('product/sale/discount/detail', { id: value.id });
activity.img = value.cover_url;
activity.time = processTime(value.left_time);
activity.brand = value.logo_url;
activity.discount = value.promotion_name;
activity.title = value.title;
if (key < 3) {
dest.big.push(activity);
} else {
dest.normal.push(activity);
}
});
return dest;
};
/**
* 处理折扣专区标题数据
* @param {[type]} origin [description]
* @return {[type]} [description]
*/
exports.handleDiscountTitleData = (origin) => {
var dest = {};
dest.title = origin.title;
dest.up = true;
dest.discount = origin.promotion_name;
dest.time = processTime(origin.left_time);
return dest;
};
/**
* 处理首页 banner 数据
* @param {[type]} origin [description]
* @return {[type]} [description]
*/
exports.handleSaleBannerData = (origin) => {
var dest = {};
dest.list = [];
_.forEach(origin, function(value) {
if (value.template_name === 'focus') {
_.forEach(value.data, function(subValue) {
let banner = {};
banner.bannerHeight = 450;
banner.href = subValue.url;
banner.img = subValue.src;
dest.list.push(banner);
});
}
});
return dest;
};
/**
* 处理首页 banner 小图 只取前三张
* @param {[type]} origin [description]
* @return {[type]} [description]
*/
exports.handleSaleBannerSmallData = (origin) => {
var dest = [];
let count = 0;
_.forEach(origin, function(value) {
if (value.template_name === 'small_pic') {
_.forEach(value.data, function(picList) {
if (count++ < 3) {
let smallPic = {};
smallPic.link = picList.url;
smallPic.icon = picList.src;
smallPic.title = picList.title;
smallPic.desc = picList.alt;
dest.push(smallPic);
}
});
}
});
return dest;
};
/**
* 处理首页楼层分类数据 数据写死
* @return {[type]} [description]
*/
exports.handleSaleCategoryData = () => {
return [{
title: 'VIP会员专享',
urlLocation: 'www.baidu.com',
navItem: [{
vip: true,
urlLocation: '?saleType=2&order=s_t_desc&limit=11',
title: '今日推荐'
}, {
incompleteSize: true,
urlLocation: '?saleType=2&order=s_t_desc&limit=11&' +
'sort=119,114,115,124,258,123,125,121,227,117,116,293,173,122,118,413',
title: '上装'
}, {
newDiscount: true,
urlLocation: '?saleType=2&order=s_t_desc&limit=11&' +
'sort=129,130,131,346,133,348',
title: '裤装'
}, {
vip: true,
urlLocation: '?saleType=2&order=s_t_desc&limit=11&' +
'sort=147,148,151,149',
title: '鞋履'
}, {
vip: true,
urlLocation: '?saleType=2&order=s_t_desc&limit=11&' +
'sort=152,153,186,313,162,311,322,75,235,424,142,238,211' +
',76,156,171,376,407,275,397,272,212,267,277,405,357,273,' +
'401,240,323,327,157,163,342,160,167,234,111,432,183,373,' +
'333,74,292,78,271,374,161,304,72,324,316,104,233,239,112,' +
'274,260,164,340,375,341,353,184,210,393,403,79,158,159,165,' +
'221,261,300,302,330,317,383,416,303,306,326,269,288,385,415,' +
'113,320,378,389,450,328,325,334,359,377,110,154,155,200,218,' +
'263,351,398,77,262,264,283,305,331,332,349,362,387,399,411',
title: '配饰'
}]
}, {
title: '断码区',
urlLocation: 'www',
navItem: [{
vip: true,
urlLocation: '?saleType=1&order=s_t_desc&limit=19',
title: '今日推荐'
}, {
incompleteSize: true,
urlLocation: '?saleType=1&order=s_t_desc&limit=19&' +
'sort=119,114,115,124,258,123,125,121,227,117,116,293,173,122,118,413',
title: '上装'
}, {
newDiscount: true,
urlLocation: '?saleType=1&order=s_t_desc&limit=19&' +
'sort=129,130,131,346,133,348',
title: '裤装'
}, {
vip: true,
urlLocation: '?saleType=1&order=s_t_desc&limit=19&' +
'sort=147,148,151,149',
title: '鞋履'
}, {
vip: true,
urlLocation: '?saleType=1&order=s_t_desc&limit=19&' +
'sort=152,153,186,313,162,311,322,75,235,424,142,238,211' +
',76,156,171,376,407,275,397,272,212,267,277,405,357,273,' +
'401,240,323,327,157,163,342,160,167,234,111,432,183,373,' +
'333,74,292,78,271,374,161,304,72,324,316,104,233,239,112,' +
'274,260,164,340,375,341,353,184,210,393,403,79,158,159,165,' +
'221,261,300,302,330,317,383,416,303,306,326,269,288,385,415,' +
'113,320,378,389,450,328,325,334,359,377,110,154,155,200,218,' +
'263,351,398,77,262,264,283,305,331,332,349,362,387,399,411',
title: '配饰'
}]
}];
};
/**
* 处理分页数据
* @param {[type]} total [description]
* @param {[type]} params [description]
* @return {[type]} [description]
*/
exports.handleSalePagerData = (total, params) => {
// 当前页
let currentPage = parseInt((_.isEmpty(params.page) ? 1 : params.page), 10);
let perPageCount = parseInt((_.isEmpty(params.limit) ? 60 : params.limit), 10);
let totalPage = parseInt(total / perPageCount, 10) + 1;
let dest = '<a href ="' + handleFilterUrl(params, { page: (currentPage) }) +
'" class="cur">' + currentPage + '</a>';
// 先处理大多数情况
if ((currentPage > 2) && (currentPage < (parseInt(total, 10) / perPageCount) - 1)) {
for (let i = 1; i < 3; i++) {
dest = '<a href ="' + handleFilterUrl(params, { page: (currentPage - i) }) + '">' +
(currentPage - i) + '</a> ' + dest + ' <a href ="' +
handleFilterUrl(params, { page: (currentPage + i) }) +
'">' + (currentPage + i) + '</a>';
}
// 处理页码小于2的情况
} else if (currentPage <= 2) {
for (let i = currentPage - 1; i > 0; i--) {
dest = '<a href ="' + handleFilterUrl(params, { page: i }) +
'">' + i + '</a>' + dest;
}
for (let i = currentPage + 1; i < (totalPage < 6 ? totalPage : 6); i++) {
dest += '<a href ="' + handleFilterUrl(params, { page: i }) +
'">' + i + '</a>';
}
// 处理页码大于最大页-2的情况
} else if (currentPage > totalPage - 2) {
for (let i = currentPage + 1; i < totalPage + 1; i++) {
dest += '<a href ="' + handleFilterUrl(params, { page: i }) +
'">' + i + '</a>';
}
for (let i = currentPage - 1; i > (currentPage - 5 > 1 ? currentPage - 5 : 1); i--) {
dest = '<a href ="' + handleFilterUrl(params, { page: i }) +
'">' + i + '</a>' + dest;
}
}
// 处理后省略号的情况
if ((totalPage > 5) && (currentPage < (totalPage - 2))) {
dest += ' ... <a href ="' + handleFilterUrl(params, { page: totalPage }) +
'">' + totalPage + '</a>';
}
// 处理前省略号的情况
if (currentPage > 3) {
dest = '<a href ="' + handleFilterUrl(params, { page: 1 }) +
'">1</a> ... ' + dest;
}
// 处理上一页下一页
if (currentPage !== 1) {
dest = '<a href="' + handleFilterUrl(params, { page: currentPage - 1 }) +
'"><span class="iconfont">上一页</span></a>' + dest;
}
if (currentPage !== totalPage) {
dest += '<a href="' + handleFilterUrl(params, { page: currentPage + 1 }) +
'"><span class="iconfont">下一页</span></a>';
}
return dest;
};