cart.js
4.03 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
'use strict';
const _ = require('lodash');
const helpers = global.yoho.helpers;
const paymentProcess = require(global.utils + '/payment-process');
const shoppingAPI = require('../../serverAPI/order/shopping');
/**
* 调用购物车结算接口返回的数据处理
*
*
*
* @param int $uid 用户ID
* @param string $cartType 购物车类型,ordinary表示普通购物车
* @param array $orderInfo cookie中记录的一些订单有关数据
* @param string $limitProductCode 限购商品码,用户限购商品购买
* @param string $sku 商品sku,用于限购商品购买
* @param stirng $skn 商品skn,用于限购商品购买
* @param int $buyNumber 购买商品数目,用户限购商品支付
* @param bool $isAjax 是否是异步请求
* @return array 接口返回的数据
*/
exports.cartPay = (uid, cartType, orderInfo, limitProductCode, sku, skn, buyNumber, isAjax) => {
isAjax = isAjax || false;
let result = {};
let skuList = [];
let isLimitGoods = skn && sku && buyNumber; // 存在sku, skn 和buyNumber时为限购商品
let orderComputeAPI = null;
if (isLimitGoods) {
skuList.push({
type: 'limitcode',
limitproductcode: limitProductCode,
buy_number: buyNumber,
skn,
sku
});
result.isLimit = true;
}
// cookie保存的数据
if (!_.isEmpty(orderInfo)) {
orderInfo.paymentType = orderInfo.paymentType ? orderInfo.paymentType : '';
orderComputeAPI = shoppingAPI.orderComputeAPI(
uid,
cartType,
orderInfo.deliveryId,
orderInfo.paymentType,
orderInfo.couponCode,
orderInfo.yohoCoin, skuList
);
}
return Promise.all([
shoppingAPI.cartPayAPI(uid, cartType, 0, skuList), // 0. 订单数据
orderComputeAPI,
shoppingAPI.getValidCouponCount(uid) // 2. 有效优惠券
]).then(res => {
let pay = res[0];
let orderCompute = _.get(res[1], 'data', {});
let validCouponCount = _.get(res[2], 'data.count', 0);
let goodsList = _.get(pay, 'data.goods_list', []);
if (_.isEmpty(goodsList)) {
if (isLimitGoods) {
result.error = true;
result.message = pay.message;
} else {
result.cartUrl = helpers.urlFormat('/cart/index/new');
}
return pay;
}
result = Object.assign(
result,
paymentProcess.tranformPayment(pay.data, orderInfo, cartType, skuList, orderCompute),
{
coupon: paymentProcess.coupon(validCouponCount, orderInfo, orderCompute)
}
);
return result;
});
};
/**
* 处理优惠券列表数据
*
* @param int $uid 用户ID
* @return array|mixed 处理之后的优惠券数据
*/
exports.getCouponList = uid => {
let result = {
notAvailableCoupons: [],
coupons: []
};
return shoppingAPI.listCoupon(uid)
.then(coupons => {
let unusableCoupons = _.get(coupons, 'data.unusable_coupons', []);
let usableCoupons = _.get(coupons, 'data.usable_coupons', []);
let procCouponsData = coupon => {
return {
couponCode: coupon.coupon_code,
couponDetailInfomation: coupon.coupon_name,
couponValue: coupon.coupon_value,
couponValidity: coupon.coupon_validity
};
};
result.notAvailableCoupons = unusableCoupons.map(procCouponsData);
result.coupons = usableCoupons.map(procCouponsData);
return result;
},
() => result
);
};
exports.searchCoupon = (uid, couponCode) => {
let result = {code: 400, message: '出错啦~'};
if (!couponCode) {
return Promise.resolve({
code: 401,
message: '优惠券代码为空'
});
}
return shoppingAPI.useCoupon(uid, couponCode)
.catch(() => result);
};