ticketsConfirm.js
3.96 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
'use strict';
const _ = require('lodash');
const co = Promise.coroutine;
const helpers = global.yoho.helpers;
const headerModel = require('../../../doraemon/models/header'); // 头部model
const ticketsConfirmModel = require('../models/ticketsConfirm');
const shoppingModel = require('../models/shopping');
const orderModel = require('../models/order');
const paymentProcess = require(global.utils + '/payment-process');
// cookie 参数
const actCkOpthn = {
path: '/cart/index'
};
const ticketsConfirm = (req, res, next) => {
let headerData = headerModel.setNav({
navTitle: '确认订单',
navBtn: false
});
let responseData = {
pageHeader: headerData,
module: 'cart',
page: 'tickets-confirm',
title: '确认订单',
localCss: true,
navBtn: false,
width750: true
};
let orderInfo;
try {
orderInfo = JSON.parse(req.cookies['order-info']);
} catch (e) {
orderInfo = {};
res.cookie('order-info', null, actCkOpthn);
}
let params = {
uid: req.user.uid,
productSku: req.query.productSku,
buyNumber: req.query.buyNumber,
useYohoCoin: orderInfo ? orderInfo.yohoCoin : 0,
gift_card_code: orderInfo.gift_card_code || null,
yohoCoinMode: true
};
co(function* () {
let [result, validGiftCardCountData] = yield Promise.all([
req.ctx(ticketsConfirmModel).ticketsConfirm(params),
req.ctx(shoppingModel).countUsableGiftCard(req.user.uid) // 可用礼品卡数量
]);
let validGiftCardCount = _.get(validGiftCardCountData, 'data.count', 0);
res.render('ticketsConfirm', _.assign(responseData, result, {
choseGiftCard: helpers.urlFormat('/cart/index/new/selectGiftcard'),
giftCards: paymentProcess.handleGiftCards({
validGiftCardCount: validGiftCardCount,
orderCompute: result.orderCompute
})
}));
})().catch(next);
};
const submitTicket = (req, res, next) => {
let uid = req.user.uid;
let udid = req.cookies.udid || 'yoho';
let gift_card_code = req.body.gift_card_code;
let verifyCode = req.body.verifyCode;
let params = {
uid: uid,
udid: udid,
productSku: req.body.productSku,
buyNumber: req.body.buyNumber,
mobile: req.body.mobile,
useYohoCoin: req.body.useYohoCoin
};
if (gift_card_code) {
_.assign(params, {
gift_card_code: gift_card_code
});
}
co(function* () {
// 使用礼品卡,发送验证码
if (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(ticketsConfirmModel).submitTicket(params);
if (result === {}) {
result.message = '人太多啦,稍后再试!';
}
// 提交成功清除Cookie
res.cookie('order-info', null, actCkOpthn);
res.json(result);
})().catch(next);
};
const checkTickets = (req, res) => {
let params = {
uid: req.user.uid,
productSku: req.body.productSku,
buyNumber: req.body.buyNumber,
useYohoCoin: req.body.useYohoCoin
};
// 未登录
if (!req.user.uid) {
return res.json({
code: 401,
redirect: '/signin.html'
});
} else {
req.ctx(ticketsConfirmModel).checkTickets(params).then(result => {
res.json(result);
});
}
};
module.exports = {
ticketsConfirm,
submitTicket,
checkTickets
};