order-ensure.js
2.98 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
/**
* 订单确认model
* @author: xuqi<qi.xu@yoho.cn>
* @date: 2016/8/26
*/
'use strict';
const _ = require('lodash');
// const api = global.yoho.API;
const helper = global.yoho.helpers;
const crypto = global.yoho.crypto;
// const config = global.yoho.config;
const ensureApi = require('./order-ensure-api');
const addressApi = require('./address-api');
const ensureHandle = require('./order-ensure-handle');
const index = (uid, cartType) => {
let resData = {};
return Promise.all([
ensureApi.getOrderPaymentAsync(uid, cartType, 0),
addressApi.getAddressListAsync(uid)
]).then(result => {
let data = _.get(result, '[0].data', false);
let address = _.get(result, '[1].data', []);
if (data) {
Object.assign(resData, ensureHandle.handlePaymentInfo(data, address));
}
return resData;
});
};
// 获取优惠券列表
// const getCoupons = (uid) => api.get('', {
// method: 'app.Shopping.listCoupon',
// uid: uid
// }).then(result => {
// if (result.code === 200) {
// let unuse = [];
// let use = [];
// _.forEach(result.data.unusable_coupons, i => {
// unuse.push({
// code: i.coupon_code,
// name: i.coupon_name,
// value: i.coupon_value,
// canuse: false
// });
// });
// _.forEach(result.data.usable_coupons, i => {
// use.push({
// code: i.coupon_code,
// name: i.coupon_name,
// value: i.coupon_value
// });
// });
// result.data = _.concat(use, unuse);
// }
// return result;
// });
// 订单计算
const compute = (uid, cartType, pa) => {
return ensureApi.getOrderComputeAsync(uid, cartType, pa.paymentType, pa.deliveryWay, pa).then(result => {
if (result.code === 200) {
if (_.has(result, 'data.last_order_amount')) {
result.data.last_order_amount = (result.data.last_order_amount).toFixed(2);
}
if (_.has(result, 'data.promotion_formula_list')) {
ensureHandle.handleViewPrice(result.data.promotion_formula_list);
}
result.data = Object.assign(result.data, ensureHandle.handleUseYohoCoin(result.data));
}
return result;
});
};
// 订单提交
const submit = (uid, cartType, p) => {
if (p.addressId) {
p.addressId = crypto.decrypt('', `${p.addressId}`);
}
return ensureApi.orderSubmitAsync(uid, cartType, p.addressId, p.deliveryTime,
p.deliveryWay, p.paymentType, p.paymentId, p.printPrice, p).then(result => {
if (result.code === 200) {
let d = result.data;
d.url = helper.urlFormat('/shopping/pay', {
ordercode: d.order_code
});
}
return result;
}
);
};
module.exports = {
index,
// getCoupons,
compute,
submit
};