buy-now-controller.js
18.5 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/*
* @Author: Targaryen
* @Date: 2017-06-21 10:15:38
* @Last Modified by: Targaryen
*/
const _ = require('lodash');
const co = require('bluebird').coroutine;
const headerModel = require('../../../doraemon/models/header');
const BuyNowModel = require('../models/buy-now-model');
const addressModel = require('../models/address');
const userModel = require('../models/user');
const orderModel = require('../models/order');
const shoppingModel = require('../models/shopping');
const utils = '../../../utils';
const paymentProcess = require(`${utils}/payment-process`);
const crypto = global.yoho.crypto;
const logger = global.yoho.logger;
const helpers = global.yoho.helpers;
// cookie 参数
const actCkOpthn = {
path: '/cart/index'
};
class BuyNowController {
/**
* 确认订单页面
* @param {*} req
* @param {*} res
* @param {*} next
*/
orderEnsure(req, res, next) {
let orderInfo;
let uid = req.user.uid;
try {
orderInfo = JSON.parse(req.cookies.buynow_info);
} catch (e) {
logger.info(`orderEnsure: get buynow-order-info from cookie error:${JSON.stringify(e)}`);
orderInfo = {};
res.clearCookie('buynow_info', actCkOpthn);
}
let product_sku = req.query.product_sku;
let buy_number = req.query.buy_number;
if (!product_sku || !buy_number) {
return next();
}
if (product_sku !== orderInfo.product_sku) {
orderInfo = {};
res.clearCookie('buynow_info', actCkOpthn);
}
co(function * () {
let [userProfile, address, result, computeData, validCouponCount, validGiftCardCount] =
yield Promise.all([
req.ctx(userModel).queryProfile(uid),
req.ctx(addressModel).addressData(uid),
req.ctx(BuyNowModel).payment({
uid: uid,
product_sku: product_sku,
sku_type: req.query.sku_type,
buy_number: buy_number,
yoho_coin_mode: parseInt(orderInfo.use_yoho_coin, 10) > 0 ? 1 : 0
}),
req.ctx(BuyNowModel).compute({
uid: uid,
product_sku: product_sku,
sku_type: req.query.sku_type,
buy_number: buy_number,
payment_type: orderInfo.payment_type,
delivery_way: orderInfo.delivery_way,
use_yoho_coin: parseInt(orderInfo.use_yoho_coin, 10),
coupon_code: orderInfo.coupon_code,
gift_card_code: orderInfo.gift_card_code,
promotion_code: orderInfo.promotion_code
}),
req.ctx(BuyNowModel).countUsableCoupon({
uid: uid,
product_sku: req.query.product_sku,
sku_type: req.query.sku_type,
buy_number: buy_number
}),
req.ctx(shoppingModel).countUsableGiftCard(uid) // 可用礼品卡数量
]);
// 获取用户完整手机号
let mobile = _.get(userProfile, 'data.mobile', '');
let orderAddress = _.get(result, 'address', []);
let addressList = _.get(address, 'data', []);
let orderEnsure = {};
if (result.code !== 200 && result.message) {
orderEnsure = {message: result.message};
} else {
orderAddress.length && _.forEach(addressList, address => { //eslint-disable-line
if (address.address_id === orderAddress.address_id) {
mobile = address.mobile;
return false;
}
});
// 兼容原有的数据格式
orderInfo.deliveryId = orderInfo.delivery_way;
orderInfo.deliveryTimeId = orderInfo.delivery_time;
orderInfo.couponCode = orderInfo.coupon_code;
orderInfo.yohoCoin = orderInfo.use_yoho_coin;
orderInfo.paymentType = orderInfo.payment_type;
orderEnsure = _.assign(
paymentProcess.tranformPayment(_.get(result, 'data', {}), orderInfo, null, null, computeData.data),
{
coupon: paymentProcess.coupon(
_.get(validCouponCount, 'data.count', 0),
orderInfo,
computeData.data
),
selectAddressUrl: helpers.urlFormat('/cart/index/buynow/selectAddress', {
product_sku: product_sku,
buy_number: buy_number
}),
selectCouponUrl: helpers.urlFormat('/cart/index/buynow/selectCoupon', {
product_sku: product_sku,
buy_number: buy_number
}),
isOrdinaryCart: true,
choseGiftCard: helpers.urlFormat('/cart/index/buynow/selectGiftcard'),
giftCards: paymentProcess.handleGiftCards({
validGiftCardCount: _.get(validGiftCardCount, 'data.count', 0),
orderCompute: computeData.data
})
}
);
}
return res.render('buynow/order-ensure', {
pageHeader: headerModel.setNav({
navTitle: '确认订单',
navBtn: false
}),
module: 'cart',
page: 'buynow-order-ensure',
title: '确认订单',
width750: true,
localCss: true,
product_sku: product_sku,
orderEnsure: orderEnsure,
userMobile: mobile
});
})().catch(next);
}
/**
* 参数更改,重新运算结算数据
* @param {*} req
* @param {*} res
* @param {*} next
*/
orderCompute(req, res, next) {
co(function * () {
let result = yield req.ctx(BuyNowModel).compute({
uid: req.user.uid,
cart_type: req.body.cart_type,
delivery_way: req.body.delivery_way,
payment_type: req.body.payment_type,
product_sku: req.body.product_sku,
buy_number: req.body.buy_number,
coupon_code: req.body.coupon_code,
gift_card_code: req.body.gift_card_code,
use_yoho_coin: req.body.use_yoho_coin
});
result.data.use_yoho_coin = paymentProcess.transPrice(result.data.use_yoho_coin);
result.data.yohoCoinCompute = paymentProcess.yohoCoinCompute(result.data);
return res.json(result.data);
})().catch(next);
}
/**
* 提交订单
* @param {*} req
* @param {*} res
* @param {*} next
*/
orderSub(req, res, next) {
let uid = req.user.uid;
let udid = req.sessionID || 'yoho';
let verifyCode = req.body.verifyCode || null;
let params = {
uid: uid,
udid: udid,
product_sku: req.body.product_sku,
sku_type: req.body.sku_type,
buy_number: req.body.buy_number,
coupon_code: req.body.coupon_code,
gift_card_code: req.body.gift_card_code,
address_id: parseInt(crypto.decrypt('', req.body.address_id), 10) || 0,
delivery_time: req.body.delivery_time,
delivery_way: req.body.delivery_way,
use_yoho_coin: req.body.use_yoho_coin,
use_red_envelopes: req.body.use_red_envelopes,
payment_id: req.body.payment_id,
payment_type: req.body.payment_type,
product_sku_list: req.body.product_sku_list,
is_print_price: req.body.is_print_price,
remark: req.body.remark,
activity_id: req.body.activity_id,
ip: req.yoho.clientIp
};
// 是否开发票
if (req.body.invoice && req.body.invoice === 'true') {
let invoicePayableType = _.parseInt(req.body.invoice_payable_type) || 1;
let invoicesTitlePersonal = req.body.invoices_title_personal ? req.body.invoices_title_personal : '个人';
params.invoice = true;
params.invoices_type = req.body.invoices_type; // 发票类型:纸质 1,电子 2
params.receiverMobile = req.body.receiverMobile; // 接收人电话
params.invoice_payable_type = invoicePayableType; // 区分个人或企业发票
if (invoicePayableType === 1) {
// 开个人发票
params.invoices_title = invoicesTitlePersonal;
} else {
// 开公司发票
params.invoices_title = req.body.invoices_title;
params.buyerTaxNumber = req.body.buyerTaxNumber;
}
// 购买方纳税人识别号
if (req.body.buyerTaxNumber) {
params.buyerTaxNumber = req.body.buyerTaxNumber;
}
}
if (req.cookies.mkt_code || req.cookies._QYH_UNION) {
let unionInfo = paymentProcess.unionInfoHandle(req.cookies, uid);
params.qhy_union = _.get(unionInfo, 'unionKey', false);
params.userAgent = _.get(unionInfo, 'userAgent', '');
}
co(function * () {
// 使用礼品卡,发送验证码
if (params.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(BuyNowModel).submit(params);
// 提交成功清除Cookie
res.clearCookie('buynow_info', actCkOpthn);
return res.json(result);
})().catch(next);
}
/**
* 选择地址
* @param {*} req
* @param {*} res
* @param {*} next
*/
selectAddress(req, res, next) {
let uid = req.user.uid;
let product_sku = req.query.product_sku;
let buy_number = req.query.buy_number;
co(function * () {
let address = yield req.ctx(addressModel).addressData(uid);
let moreUrl = helpers.urlFormat('/cart/index/buynow/orderensure', {
product_sku: product_sku,
buy_number: buy_number
});
address = address.data;
let headerData = headerModel.setNav({
navTitle: '选择地址',
navBtn: false,
backUrl: moreUrl
});
res.render('buynow/select-address', {
module: 'cart',
page: 'buynow-select-address',
pageHeader: headerData,
pageFooter: true,
moreUrl,
address,
product_sku: product_sku,
buy_number: buy_number,
localCss: true
});
})().catch(next);
}
/**
* 填写发票信息
* @param {*} req
* @param {*} res
* @param {*} next
*/
selectInvoice(req, res, next) {
let product_sku = req.query.product_sku;
let buy_number = req.query.buy_number;
let uid = req.user.uid;
let orderInfo;
try {
orderInfo = JSON.parse(req.cookies.buynow_info);
} catch (e) {
orderInfo = {};
}
co(function* () {
let userData = yield req.ctx(userModel).queryProfile(uid);
let mobile = _.get(userData, 'data.mobile', '');
let addresslist = yield req.ctx(userModel).addressTextData(uid);
let returnData = req.ctx(orderModel).processInvoiceData(orderInfo, mobile, addresslist);
let headerData = headerModel.setNav({
invoiceNotice: '发票须知',
navTitle: '发票信息',
navBtn: false
});
res.render('select-invoice', _.assign(returnData, {
pageHeader: headerData,
module: 'cart',
page: 'buynow-select-invoice',
localCss: true,
addressMore: helpers.urlFormat('/cart/index/buynow/orderensure', {
product_sku: product_sku,
buy_number: buy_number
})
}));
})().catch(next);
}
/**
* 选择优惠券页面
*/
selectCoupon(req, res) {
let headerData = headerModel.setNav({
navTitle: '选择优惠券',
navBtn: false
});
res.render('select-coupon', {
module: 'cart',
page: 'buynow-select-coupon',
title: '选择优惠券',
selectCouponPage: true,
pageHeader: headerData,
pageFooter: false,
localCss: true
});
}
/**
* 选择礼品卡页面
*/
selectGiftcard(req, res, next) {
let headerData = headerModel.setNav({
navTitle: '礼品卡',
navBtn: false
});
let uid = req.user.uid;
co(function* () {
let usable_giftCards = []; // 可用礼品卡列表
let sureActice = false; // 确认按钮状态
let giftCardsData = yield req.ctx(shoppingModel).listGiftCard(uid);
let usableGiftCardsOrigin = _.get(giftCardsData, 'data.usable_giftCards', false);
if (usableGiftCardsOrigin) {
let orderInfo;
try {
orderInfo = JSON.parse(req.cookies.buynow_info);
} catch (e) {
logger.info(`orderEnsure: get buynow_info from cookie error:${JSON.stringify(e)}`);
orderInfo = {};
res.clearCookie('buynow_info', actCkOpthn);
}
let handleResult = paymentProcess.handleGiftCardsRender(usableGiftCardsOrigin,
_.get(orderInfo, 'gift_card_code'));
usable_giftCards = handleResult.giftCards;
sureActice = handleResult.sureActice;
}
res.render('select-giftcard', {
module: 'cart',
page: 'buynow-select-giftcard',
title: '礼品卡',
pageHeader: _.assign(headerData, {
useGiftCard: true
}),
pageFooter: false,
localCss: true,
width750: true,
usable_giftCards: usable_giftCards,
sureActice: sureActice
});
})().catch(next);
}
/**
* 获取用户可用和不可用优惠券列表
* @param {*} req
* @param {*} res
* @param {*} next
*/
listCoupon(req, res, next) {
let orderInfo;
try {
orderInfo = JSON.parse(req.cookies.buynow_info);
} catch (e) {
logger.info(`orderEnsure: get buynow-order-info from cookie error:${JSON.stringify(e)}`);
orderInfo = {};
res.clearCookie('buynow_info', actCkOpthn);
}
let delivery_way = orderInfo.deliveryId || 1;
co(function* () {
let result = yield req.ctx(BuyNowModel).listCoupon({
uid: req.user.uid,
product_sku: req.query.product_sku,
sku_type: req.query.sku_type,
buy_number: req.query.buy_number,
is_group_frees: 'Y', // 运费券是否分组
delivery_way: delivery_way,
});
let finalResult = {
availableCoupons: [],
usableFreesCoupons: [],
notAvailableCoupons: [],
};
let usableCoupons = _.get(result, 'data.usable_coupons', []);
let freesCoupons = _.get(result, 'data.usable_frees_coupons', []);
let unusableCoupons = _.get(result, 'data.unusable_coupons', []);
let orderInfoUsableUsualCode = _.get(orderInfo, 'usable_usual_code', '');
let orderInfoUsableFreeCode = _.get(orderInfo, 'usable_free_code', '');
let procCouponsData = coupon => {
return {
couponCode: coupon.coupon_code,
couponDetailInfomation: coupon.coupon_name,
couponValue: coupon.coupon_value,
couponValidity: coupon.coupon_validity,
checked: coupon.coupon_code === orderInfoUsableUsualCode ||
coupon.coupon_code === orderInfoUsableFreeCode
};
};
finalResult.availableCoupons = usableCoupons.map(procCouponsData); // 可用优惠券
finalResult.usableFreesCoupons = freesCoupons.map(procCouponsData); // 运费券
finalResult.notAvailableCoupons = unusableCoupons.map(procCouponsData); // 不可用优惠券
res.json(finalResult);
})().catch(next);
}
/**
* 使用优惠券
* @param {*} req
* @param {*} res
* @param {*} next
*/
useCoupon(req, res, next) {
co(function* () {
let result = yield req.ctx(BuyNowModel).useCoupon({
uid: req.user.uid,
product_sku: req.body.product_sku,
sku_type: req.body.sku_type,
buy_number: req.body.buy_number,
coupon_code: req.body.coupon_code
});
res.json(result);
})().catch(next);
}
/**
* 输入优惠券码使用优惠券
*/
usePromotionCode(req, res, next) {
co(function* () {
let result = yield req.ctx(BuyNowModel).usePromotionCode({
uid: req.user.uid,
product_sku: req.body.product_sku,
sku_type: req.body.sku_type,
buy_number: req.body.buy_number,
promotion_code: req.body.promotion_code
});
res.json(result);
})().catch(next);
}
}
module.exports = new BuyNowController();