order.js
2.82 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
'use strict';
const _ = require('lodash');
const helpers = global.yoho.helpers;
const cartModel = require('../models/cart');
const headerModel = require('../../../doraemon/models/header');
const userModel = require('../../serverAPI/user');
const addressModel = require('../../serverAPI/user/address');
exports.orderEnsure = (req, res, next) => {
req.locals.width750 = true;
req.locals.title = '确认订单';
let headerData = headerModel.setNav({
navTitle: '确认订单',
navBtn: false
});
let uid = req.user.uid;
let returnUrl = helpers.urlFormat('/cart/index/new');
let cartType = req.query.cartType || 'ordinary';
let orderInfo;
try {
orderInfo = JSON.parse(req.cookies['order-info']);
} catch (e) {
orderInfo = {};
}
let cookieCartType = _.get(orderInfo, 'cartType');
if (cookieCartType) {
cartType = cookieCartType;
}
// 如果传递了code, skn, sku, buy_number 就代表限购商品
let limitProductCode = req.query.limitproductcode || '';
let sku = req.query.sku || '';
let skn = req.query.skn || '';
let buyNumber = req.query.buy_number || 1;
if (limitProductCode) {
headerData.backUrl = req.get('Referer') || returnUrl;
}
let order = cartModel.cartPay(uid, cartType, orderInfo, sku, skn, buyNumber, req.xhr);
let userProfile = userModel.queryProfile(uid);
let address = addressModel.addressData(uid);
return Promise.all([order, userProfile, address])
.then(
result => {
order = result[0];
userProfile = result[1];
address = result[2];
if (order.cartUrl) { // TODO? 普通或者预售商品为空时, BUT WHEN AJAX?
return res.redirect(order.cartUrl);
}
if (req.xhr) {
return res.json(order);
}
// 获取用户完整手机号
let mobile = _.get(userProfile, 'data.mobile', '');
let orderAddress = _.get(order, 'address', []);
let addressList = _.get(address, 'data', []);
orderAddress.length && addressList.forEach(address => { //eslint-disable-line
if (address.address_id === orderAddress.address_id) {
mobile = address.mobile;
}
});
let viewData = {
orderEnsurePage: true,
isOrdinaryCart: cartType !== 'advance',
orderEnsure: order,
userMobile: mobile
};
viewData.pageHader = headerData;
viewData.pageFooter = true;
res.render('order-ensure', viewData);
}
)
.catch(next);
};