order.js
5.19 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
'use strict';
const api = global.yoho.API;
const camelCase = global.yoho.camelCase;
const moment = require('moment');
const _ = require('lodash');
const pageSize = 10;
const typeActiveIndexMap = {
1: 0,
2: 1,
3: 2
};
const _convertUnixTime = (src) => {
return moment.unix(src).format('YYYY-MM-DD hh:mm:ss');
};
const deleteOrder = (uid, code) => {
return api.get('', {
method: 'app.SpaceOrders.delOrderByCode',
uid: uid,
order_code: code
}, {
cache: true
});
};
const cancelOrder = (uid, code) => {
return api.get('', {
method: 'app.SpaceOrders.close',
uid: uid,
order_code: code
}, {
cache: true
});
};
const getExpressInfo = (uid, code) => {
return api.get('', {
method: 'app.express.get',
uid: uid,
order_code: code
}, {
cache: true
});
};
const _getDetail = (uid, code) => {
return api.get('', {
method: 'app.SpaceOrders.detail',
uid: uid,
order_code: code
}, {
cache: true
});
};
const _getUserOrder = (uid, type, page) => {
return api.get('', {
method: 'app.SpaceOrders.get',
uid: uid,
type: type,
page: page,
limit: pageSize
}).then(result => {
let orderList = [];
let total = false;
let curPage = 1;
if (result && result.data) {
orderList = camelCase(result.data.order_list);
total = result.data.total;
curPage = result.data.page;
}
orderList.forEach(item => {
const ot = parseInt(item.orderType, 10);
const st = parseInt(item.status, 10);
// 转换订单创建时间
item.createTime = _convertUnixTime(item.createTime);
// 隐藏结束了的剩余时间
if (parseInt(item.payLefttime, 10) !== 0 && item.isCancel === 'N') {
item.showPayButton = true;
}
// 具体订单类型见 http://git.yoho.cn/yoho-documents/api-interfaces/blob/master/%E8%AE%A2%E5%8D%95/order.md
// 判断是否是手机订单,3, 4, 6对应 iOS, Android, H5
item.showMobile = ot === 3 || ot === 4 || ot === 6;
// 判断是否展示编辑选项
item.showEditOption = st === 6;
// 判断是否是待收货状态
item.showGetBtn = st === 4 || st === 5;
item.showBuyBtn = item.isCancel === 'Y';
});
return {
orderList: orderList,
total: total,
curPage: curPage
};
});
};
const getOrderData = (uid, type, page) => {
const navBar = {
tabs: [
{
text: '全部订单',
typeStr: 'all'
},
{
text: '待付款',
typeStr: 'paying'
},
{
text: '待发货',
typeStr: 'delivering'
}
]
};
type = parseInt(type, 10);
type = type < 4 ? type : 1;
page = page || 1;
return _getUserOrder(uid, type, page).then(result => {
const basicData = {
title: '我的订单',
showSearch: type === 1,
total: result.total,
type: type
};
navBar.tabs[typeActiveIndexMap[type]].isActive = true;
const order = Object.assign(basicData, {
orderList: result.orderList.length && result.orderList || false
}, navBar);
const paginationOpts = result.total > pageSize ? {
paginationOpts: {
total: result.total,
page: result.curPage,
limit: pageSize
}
} : false;
return {
order: Object.assign(order, paginationOpts)
};
});
};
const _convertAddress = addressList => {
let addrList = [];
addressList.forEach(addr => {
let address = {
time: '',
city: '',
action: ''
};
address.time = addr.acceptTime;
addr = addr.acceptAddress.split(' ');
if (addr.length > 1 && /市/.test(addr[0])) {
address.city = addr[0].replace('市', '');
addr.splice(0, 1);
address.action = addr.join('');
} else {
address.action = addr.join('');
}
addrList.push(address);
});
return _.reverse(addrList);
};
const getOrderDetail = (uid, code) => {
return api.all([_getDetail(uid, code), getExpressInfo(uid, code)]).then(result => {
let detail = result[0] && camelCase(result[0].data);
let express = result[1] && camelCase(result[1].data);
const basicData = {
title: '订单详情'
};
detail.createTime = _convertUnixTime(detail.createTime);
detail.expressInfo = express;
detail.expressInfo.addressList = _convertAddress(express.expressDetail);
return {
orderDetail: Object.assign(basicData, detail)
};
});
};
module.exports = {
getOrderData: getOrderData,
getOrderDetail: getOrderDetail,
cancelOrder: cancelOrder,
deleteOrder: deleteOrder,
getExpressInfo: getExpressInfo
};