seckill.js
3.77 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
/**
* 秒杀结算
*/
'use strict';
const co = require('bluebird').coroutine;
const seckillModel = require('../models/seckill');
const buyNowModel = require('../models/buynow');
const BAD_REQUEST = '非法请求';
exports.ensure = (req, res, next) => {
const sku = Number.parseInt(req.query.sku, 10);
const skn = Number.parseInt(req.query.skn, 10);
const uid = req.user.uid;
// require skn, sku;
if (!(sku && skn)) {
return next('error');
}
co(function*() {
let paymentOption = {
buy_number: 1,
yoho_coin_mode: 1,
product_sku: sku,
sku_type: 'S',
uid
};
let skillData = yield seckillModel.skillData(skn); // 根据skn查活动信息
skillData = skillData.data;
if (!(
skillData &&
skillData.status &&
skillData.secKillSku.some(obj => obj.productSku === sku) // skn has sku;
)) {
return Promise.reject('活动不存在');
}
paymentOption.activity_id = skillData.activityId;
// 获取结算 数据
let paymentInfo = yield buyNowModel.payment(paymentOption);
if (paymentInfo.code !== 200) {
return Promise.reject('结算请求失败');
}
// 渲染
let view = Object.assign({
seckill: skillData,
orderEnsure: true,
sku,
}, paymentInfo.data);
// console.log(view);
res.locals.title = '确认订单';
res.render('order-ensure', view);
})().catch(next);
};
exports.compute = (req, res, next) => {
const uid = req.user.uid,
sku = req.body.sku,
activityId = req.body.activityId;
if (!req.xhr) {
return next(400);
}
// 必填字段
if ([uid, sku, activityId].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: 1,
uid: req.user.uid,
product_sku: sku,
delivery_way: req.body.deliveryId || 1,
use_yoho_coin: req.body.use_yoho_coin || 0,
activity_id: activityId
};
return buyNowModel.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,
addressId = Number.parseInt(req.body.addressId, 10),
deliveryTime = Number.parseInt(req.body.deliveryTime, 10),
deliveryWay = Number.parseInt(req.body.deliveryWay, 10),
paymentId = Number.parseInt(req.body.paymentId, 10),
paymentType = Number.parseInt(req.body.paymentType, 10);
if (!req.xhr) {
return next(404);
}
if ([uid, sku, activityId, addressId, deliveryTime,
deliveryWay, paymentId, paymentType
].some(field => !field)) {
return res.status(400).json({
code: 400,
msg: BAD_REQUEST
});
}
const options = {
sku_type: 'S',
buy_number: 1,
invoices_title: req.body.invoicesTitle || '',
invoices_type_id: req.body.invoicesTypeId || '',
use_yoho_coin: Number.parseFloat(req.body.useYohoCoin) || 0,
remark: req.body.remark || '',
address_id: addressId,
delivery_time: deliveryTime,
delivery_way: deliveryWay,
payment_id: paymentId,
payment_type: paymentType
};
return buyNowModel.submit(options)
.then(result => {
res.clearCookie('order-info');
res.json(result);
})
.catch(next);
};