seckill.js
9.25 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
/**
* 秒杀结算
*/
'use strict';
const _ = require('lodash');
const crypto = global.yoho.crypto;
const helpers = global.yoho.helpers;
const co = Promise.coroutine;
const userModel = require('../models/user');
const addressModel = require('../models/address');
const seckillModel = require('../models/seckill');
const headerModel = require('../../../doraemon/models/header'); // 头部model
const orderModel = require('../models/order');
const shoppingModel = require('../models/shopping');
const paymentProcess = require(global.utils + '/payment-process');
const BAD_REQUEST = '非法请求';
const SLAT = 'yohobuyseckill98';
exports.ensure = (req, res, next) => {
const sku = parseInt(req.query.sku, 10);
const skn = parseInt(req.query.skn, 10);
const uid = req.user.uid;
let activityId;
let orderInfo = req.cookies['order-info'];
try {
orderInfo = JSON.parse(req.cookies['order-info']);
} catch (e) {
orderInfo = {};
}
// require skn, sku;
if (!(sku && skn)) {
return next();
}
co(function*() {
let paymentOption = {
buy_number: 1,
yoho_coin_mode: 0, // 默认不使用有货币
product_sku: sku,
sku_type: 'S',
uid
};
let skillData = yield req.ctx(seckillModel).skillData(skn); // 根据skn查活动信息
let view = {
orderEnsure: false,
message: ''
};
skillData = skillData.data;
// 不在秒杀中
if (skillData && (skillData.status !== 2)) {
return res.redirect('/product/seckill');
}
if (!(
skillData &&
skillData.secKillSku.some(obj => obj.productSku === sku) // skn has sku;
)) {
view.message = '挤爆啦,系统繁忙';
return res.render('order-ensure', Object.assign({
module: 'cart',
page: 'seckill',
width750: true,
localCss: true,
pageHeader: headerModel.setNav({
navTitle: '确认订单',
backUrl: '/product/' + skn + '.html' // 商品url改版
}),
}, view));
}
paymentOption.activity_id = activityId = skillData.activityId;
let orderComputerData;
let limitMessage;
if (orderInfo) {
orderComputerData = yield req.ctx(seckillModel).compute(_.assign(paymentOption, {
delivery_way: orderInfo.deliveryId || 1,
payment_type: orderInfo.paymentType || 1,
use_yoho_coin: orderInfo.yohoCoin || 0,
gift_card_code: orderInfo.gift_card_code || null
}));
if (orderComputerData.code === 9999992) {
limitMessage = '人太多啦,请稍后再试!';
}
}
let [userProfile, address, paymentInfo, validGiftCardCountData] = yield Promise.all([
req.ctx(userModel).queryProfile(uid),
req.ctx(addressModel).addressData(uid),
req.ctx(seckillModel).payment(paymentOption, orderInfo, _.get(orderComputerData, 'data')),
req.ctx(shoppingModel).countUsableGiftCard(uid) // 可用礼品卡数量
]);
let validGiftCardCount = _.get(validGiftCardCountData, 'data.count', 0);
// 获取用户完整手机号
let mobile = _.get(userProfile, 'data.mobile', '');
let orderAddress = _.get(paymentInfo, 'address', []);
let addressList = _.get(address, 'data', []);
orderAddress.length && _.forEach(addressList, address => { //eslint-disable-line
if (address.address_id === orderAddress.address_id) {
mobile = address.mobile;
return false;
}
});
if (paymentInfo.code !== 200) {
view = {
orderEnsure: false,
message: paymentInfo.message
};
// hotfix: nginx 接口限流, code:9999991时没message 信息
if (!view.message) {
view.message = '挤爆啦,系统繁忙';
}
} else {
// 渲染
view = _.assign({
seckill: skillData,
orderEnsure: true,
sku,
}, paymentInfo.data, {
choseGiftCard: helpers.urlFormat('/cart/index/new/selectGiftcard'),
giftCards: paymentProcess.handleGiftCards({
validGiftCardCount: validGiftCardCount,
orderCompute: _.get(orderComputerData, 'data')
})
});
}
res.locals.title = '确认订单';
view.limitMessage = limitMessage;
res.render('order-ensure', Object.assign({
module: 'cart',
page: 'seckill',
pageHeader: headerModel.setNav({
navTitle: '确认订单',
backUrl: '/product/' + skn + '.html' // 商品url改版
}),
width750: true,
localCss: true,
userMobile: mobile,
cartToken: crypto.encryption(SLAT, [sku, activityId].join(''))
}, view));
})().catch(next);
};
exports.compute = (req, res, next) => {
const uid = req.user.uid,
sku = req.body.sku,
paymentType = parseInt(req.body.paymentType || 1, 10),
activityId = req.body.activityId;
if (!req.xhr) {
return next(400);
}
// 必填字段
if ([uid, sku, activityId, paymentType].some(field => !field)) {
return res.status(400).json({
code: 400,
msg: BAD_REQUEST
});
}
const options = {
sku_type: 'S', // 秒杀
buy_number: 1,
yoho_coin_mode: 1,
payment_type: paymentType,
uid: req.user.uid,
product_sku: sku,
delivery_way: req.body.deliveryId || 1,
use_yoho_coin: req.body.yohoCoin || 0,
gift_card_code: req.body.gift_card_code,
activity_id: activityId
};
return req.ctx(seckillModel).compute(options)
.then(result => {
res.json(result.data);
})
.catch(next);
};
exports.submit = (req, res, next) => {
const uid = req.user.uid,
sku = req.body.sku,
activityId = req.body.activityId,
deliveryTime = parseInt(req.body.deliveryTime, 10),
deliveryWay = parseInt(req.body.deliveryWay, 10),
paymentId = parseInt(req.body.paymentId, 10),
paymentType = parseInt(req.body.paymentType, 10),
token = req.body.cartToken;
let udid = req.cookies.udid || 'yoho';
let addressId = req.body.addressId || '';
let verifyCode = req.body.verifyCode || null;
if (!req.xhr) {
return next();
}
let compareToken = crypto.encryption(SLAT, [sku, activityId].join(''));
if (token !== compareToken) {
return next();
}
if (!addressId) {
return res.json({code: 400, message: '请选择地址'});
} else {
addressId = req.body.addressId;
}
if ([uid, sku, activityId, addressId, deliveryTime,
deliveryWay, paymentId, paymentType
].some(field => !field)) {
return res.status(400).json({
code: 400,
message: BAD_REQUEST
});
}
const options = {
sku_type: 'S',
buy_number: 1,
use_yoho_coin: parseFloat(req.body.useYohoCoin) || 0,
remark: req.body.remark || '',
gift_card_code: req.body.gift_card_code,
address_id: addressId,
delivery_time: deliveryTime,
delivery_way: deliveryWay,
payment_id: paymentId,
payment_type: paymentType,
product_sku: sku,
activity_id: activityId,
uid,
ip: req.ip || '',
udid: req.cookies.udid || 'yoho'
};
if (req.body.invoice === 'true') {
let invoicesTitlePersonal = req.body.invoices_title_personal ? req.body.invoices_title_personal : '个人';
let invoicePayableType = _.parseInt(req.body.invoice_payable_type);
_.assign(options, {
invoices_type: 2,
invoices_type_id: 12,
invoice_payable_type: invoicePayableType,
receiverMobile: req.body.receiverMobile || '',
});
if (invoicePayableType === 1) {
// 开个人发票
options.invoices_title = invoicesTitlePersonal;
} else {
// 开公司发票
options.invoices_title = req.body.invoices_title;
options.buyerTaxNumber = req.body.buyerTaxNumber;
}
}
co(function* () {
// 使用礼品卡,发送验证码
if (options.gift_card_code) {
if (!verifyCode) {
yield req.ctx(orderModel).giftCardSendSms(uid);
return res.json({
code: 411
});
} else {
let verifyResult = yield req.ctx(orderModel).validRegCode({
uid, verifyCode, udid
});
if (verifyResult.code !== 200) {
return res.json(verifyResult);
}
}
}
let result = yield req.ctx(seckillModel).submit(options);
if (result) {
res.clearCookie('order-info');
res.json(result);
}
})().catch(next);
};