buy-now-controller.js
14 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
/*
* @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 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;
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 = {};
}
co(function * () {
let result = yield req.ctx(BuyNowModel).payment({
uid: req.user.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
});
let computeData = yield req.ctx(BuyNowModel).compute({
uid: req.user.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,
promotion_code: orderInfo.promotion_code
});
let validCouponCount = yield req.ctx(BuyNowModel).countUsableCoupon({
uid: req.user.uid,
product_sku: req.query.product_sku,
sku_type: req.query.sku_type,
buy_number: buy_number
});
let headerData = headerModel.setNav({
navTitle: '确认订单',
navBtn: 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;
let orderEnsure = _.assign(
paymentProcess.tranformPayment(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
}
);
return res.render('buynow/order-ensure', {
pageHeader: headerData,
module: 'cart',
page: 'buynow-order-ensure',
title: '确认订单',
width750: true,
localCss: true,
product_sku: product_sku,
orderEnsure: orderEnsure
});
})().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,
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 params = {
uid: 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,
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 * () {
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 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('select-address', {
module: 'cart',
page: 'buynow-select-address',
pageHeader: headerData,
pageFooter: true,
moreUrl,
address,
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 userModel.queryProfile(uid);
let mobile = _.get(userData, 'data.mobile', '');
let addresslist = yield userModel.addressTextData(uid);
let returnData = 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
});
}
/**
* 获取用户可用和不可用优惠券列表
* @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();