seckill.js
3.49 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
/**
* 秒杀页面
* @author: 陈峰<feng.chen@yoho.cn>
* @date: 2016/09/08
*/
'use strict';
const mRoot = '../models';
const headerModel = require('../../../doraemon/models/header');
const seckillModel = require(`${mRoot}/seckill`);
/**
* [时间缺0补0]
*/
const _timeFormat = (tick) => {
return tick < 10 ? `0${tick}` : tick;
}
/**
* [秒杀列表页面]
*/
const index = (req, res, next) => {
let headerData = headerModel.setNav({
navTitle: '秒杀活动',
navBtn: true,
}),
result = {},
hbsHelper = {
helpers: {
statusClass: (nav) => {
if (nav.over) {
return 'over';
} else if (nav.now) {
return 'now';
} else if (nav.wait) {
return 'wait';
} else {
return '';
}
}
}
};
return seckillModel.queryActivity().then((resultActivity) => {
if (resultActivity.code !== 200) {
return next();
}
result.activitys = resultActivity.data.secKillProductVoList;
let nowTime = Date.parse(new Date());
result.activitys.forEach((activity, i) => {
let date,
hour = 0,
minute = 0;
activity.startTime *= 1000;
date = new Date(activity.startTime);
hour = date.getHours();
minute = date.getMinutes();
activity.time = `${_timeFormat(hour)}:${_timeFormat(minute)}`;
if (nowTime > activity.startTime) { // 当前时间大于这个时间段,已经开始和即将开始两种情况
if (i < result.activitys.length - 1) {
let nextTime = result.activitys[i + 1].startTime * 1000;
if (nowTime < nextTime) { // 下一个时间段与当前时间来区别是否正在抢购
activity.now = true;
activity.focus = true;
} else {
activity.over = true;
}
} else { // 大于这个时间段但是后面没有秒抢时间端了,则依然显示抢购中
activity.now = true;
}
} else {
activity.wait = true;
}
});
if (result.activitys.length && result.activitys.findIndex(activity => activity.focus) < 0) {
result.activitys[0].focus = true;
}
let focusActivity = result.activitys.find(activity => activity.focus);
return seckillModel.queryProductList(focusActivity.activityId).then((resultProducts) => {
result.products = resultProducts.data;
res.render('seckill', Object.assign({
pageHeader: headerData,
pageFooter: true,
width750: true,
times: 12
}, result, hbsHelper));
})
})
};
/**
* [xhr根据活动id获取商品列表]
*/
const getProductList = (req, res, next) => {
if (!req.xhr) {
return next();
}
let activityId = req.query.activityId;
if (!activityId) {
return next();
}
return seckillModel.queryProductList(activityId).then((resultProducts) => {
let result = {
products: resultProducts.data
}
res.render('seckill/product-list', Object.assign(result, {
layout: false
}));
})
}
module.exports = {
index,
getProductList
};