seckill.js
7.24 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
/**
* 秒杀页面
* @author: 陈峰<feng.chen@yoho.cn>
* @date: 2016/09/08
*/
'use strict';
const logger = global.yoho.logger;
const moment = require('moment');
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;
};
/**
* 秒杀商品 添加 秒杀按钮状态
*
* @param isApp app才有提醒功能
* @param productList 秒杀商品列表
*
* 默认: 去抢购
* 已抢光 over=true
* 在APP中未开抢 wait=true
*/
function _productAddFlag(productList) {
let now = Date.now();
productList.sort((a, b) => a.orderBy < b.orderBy);
productList.forEach(product => {
if (now < product.startTime * 1000) { // 未开抢
product.wait = true;
} else if (product.secKillStatus === 0 || (now > product.endTime * 1000)) { // 确实已抢光, 过了秒杀时间
product.over = true;
}
});
return productList;
}
let _helpers = {
statusClass: (nav) => {
if (nav.over) {
return 'over';
} else if (nav.now) {
return 'now';
} else if (nav.wait) {
return 'wait';
} else {
return '';
}
},
readaleTime: (unixStamp) => {
return moment.unix(unixStamp).format('MM月DD日 HH:mm');
}
};
/**
* [秒杀列表页面]
*/
const index = (req, res, next) => {
let headerData = headerModel.setNav({
navTitle: '秒杀活动',
navBtn: true,
}),
result = {},
hbsHelper = {
helpers: _helpers
};
let uid = req.yoho.isApp && req.query.uid;
return seckillModel.queryActivity().then((resultActivity) => {
// console.log(resultActivity)
if (resultActivity.code !== 200 || resultActivity.data.secKillProductVoList.length === 0) {
return next();
}
result.activitys = resultActivity.data.secKillProductVoList.sort((a, b) => a.startTime > b.startTime);
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) {
let i = result.activitys.length;
// 没有focus的活动,则focus 最后一个 over 的活动
while (i) {
let activity = result.activitys[i - 1];
if (activity.over) {
activity.focus = true;
break;
}
i = i - 1;
}
// 没有over,全部都是未开抢,则focus第一个
if (!i) {
result.activitys[0].focus = true;
}
}
let focusActivity = result.activitys.find(activity => activity.focus);
return seckillModel.queryProductList(focusActivity.activityId, uid).then((resultProducts) => {
result.products = _productAddFlag(resultProducts.data, req.yoho.isApp);
// console.log(result);
res.render('seckill', Object.assign({
title: '秒杀活动',
pageHeader: headerData,
pageFooter: true,
width750: true,
times: 12
}, result, hbsHelper));
});
});
};
/**
* [xhr根据活动id获取商品列表]
*/
const getProductList = (req, res, next) => {
if (!req.xhr) {
return next();
}
let activityId = Number(req.query.activityId);
let uid = req.yoho.isApp && req.query.uid;
if (!activityId) {
return next();
}
return seckillModel.queryProductList(activityId, uid).then((resultProducts) => {
let result = {
products: _productAddFlag(resultProducts.data)
};
res.render('seckill/product-list', Object.assign(result, {
layout: false
}, {helpers: _helpers}));
}).catch(error => {
logger.error(error);
});
};
// only app
const remind = (req, res, next) => {
if (!(req.yoho.isApp && req.xhr)) {
return res.status(404).json({
msg: '请求不合法'
});
}
return seckillModel.remind({
on_off: req.body.on_off,
activity_id: Number.parseInt(req.body.activity_id, 10),
product_skn: Number.parseInt(req.body.product_skn, 10),
uid: Number.parseInt(req.body.uid, 10),
sec_kill_id: Number.parseInt(req.body.sec_kill_id, 10),
app_type: 0
})
.then(result=> {
return res.json(result);
});
};
// Middleware:检测 app_version 是否正确
// @param okVersion string "5.1.0" [master_version, minor_version, patch_version]
// TODO: 可以移到 middleware;
const checkAppVer = function(okVersion) {
return (req, res, next) => {
/**
* [比较app版本]
* @param okVersion string '5.1.0'
* @param curVersion string
* @returns -1: 小于, 0 等于, 1大于
*/
let compareVersion = (okVersion, curVersion) => { //eslint-disable-line
let result = 0; // -1: 小于, 0 等于, 1 大于
if (okVersion === curVersion) {
return result;
}
okVersion = okVersion.split('.');
curVersion = curVersion.split('.');
for (var i = 0; i < 3; i++) {
let standard = Number.parseInt(okVersion[i], 10) || 0;
let cur = Number.parseInt(curVersion[i], 10) || 0;
if (cur < standard) {
result = -1;
break;
} else if (cur > standard) {
result = 1;
break;
}
}
return result;
};
if (req.yoho.isApp) {
let curVersion = req.query.app_version;
let isOld = compareVersion(okVersion, curVersion) === -1;
if (isOld) {
return res.redirect('/passport/update');
}
}
next();
};
};
module.exports = {
index,
getProductList,
remind,
checkAppVer
};