order.page.js
7.43 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
/**
* [个人中心]首页/我的订单
* @author: xuqi<qi.xu@yoho.cn>
* @date: 2016/07/04
*/
var dialog = require('../plugins/dialog');
var _dialog = dialog.Dialog;
var _alert = dialog.Alert;
var expressTpl = require('../../tpl/me/express.hbs');
var cancelOrder = require('./order/cancel-order');
var reOrder = require('./order/readd-order');
var confirmReceive = require('./order/confirm-receive');
// 订单剩余时间显示及倒计时
var countDown = require('./order/countdown');
// 更新表格
var tableOperation = {
$header: $('.table.column-category'),
$body: $('.table.table-body'),
removeBody: function() {
this.$body = $('.table.table-body');
},
appendBody: function(htmlStr) {
this.$body.remove();
$(htmlStr).appendTo(this.$header);
}
};
// 订单类型
var typeMap = {
all: 1,
paying: 2,
delivering: 3
};
// 个人中心共用代码加载
require('./me');
// 获取订单列表
function getOrderList(type, page) {
tableOperation.removeBody();
$.ajax({
url: '/me/getOrderList',
data: {
type: type,
page: page
}
}).done(function(res) {
tableOperation.appendBody(res);
bindEvent(); // eslint-disable-line
countDown.start();
}).fail(function(err) {
console.log(err);
});
}
// 获取url中的参数
function getQueryString() {
var queryArr = location.search.substr(1).split('&');
var query = {};
queryArr.forEach(function(pair) {
var arr = pair.split('=');
query[arr[0]] = arr[1];
});
return query;
}
// 获取订单总数
function getOrderTotal(type, key) {
$.ajax({
url: '/me/getOrderTotal',
data: {
type: type
}
}).done(function(result) {
var $navItme = $('*[data-type="' + key + '"]');
if (!$navItme.hasClass('active') && result.total) {
$navItme.find('.badge').text(result.total);
} else if (!$navItme.hasClass('active')) {
$navItme.find('.badge').text('');
}
});
}
// 设置订单总数
function setOrderTotal() {
var key;
for (key in typeMap) {
if (key !== 'all') {
getOrderTotal(typeMap[key], key);
}
}
}
function getCurrentTabType() {
return $('.order-nav .active').data('type');
}
function getCurrentPage() {
var $pageEl = $('.blk-pagination .active a');
var page;
if ($pageEl.length) {
page = $pageEl.text();
}
return page;
}
function bindBuyEvent() {
$('.buy-operation .buy').off('click').on('click', function() {
var code = $(this).closest('.order').data('code');
reOrder(code);
});
}
function updateTableContent($el) {
var type = getCurrentTabType();
var page = getCurrentPage();
if (type === 'all') {
$el.find('.status .status-str').text('已取消');
$el.find('.pay-operation').remove();
$el.find('.buy-operation').removeClass('hide');
setOrderTotal();
bindBuyEvent();
} else {
type = typeMap[type];
getOrderList(type, page);
}
}
// 绑定分页点击事件
function bindPaginationClick() {
$('.blk-pagination li').off('click').on('click', function(e) {
var $this = $(this);
var page = $this.find('a').attr('href').split('=')[1];
var type = getQueryString().type;
e.preventDefault();
if (!$this.hasClass('active')) {
$('.blk-pagination li.active').removeClass('active');
$this.addClass('active');
$(window).scrollTop(0);
getOrderList(type, page);
}
});
}
// 删除订单
function deleteOrder(code) {
$.ajax({
url: '/me/deleteOrder',
data: {
orderCode: code
}
}).done(function(result) {
var type = getCurrentTabType();
var page = getCurrentPage();
if (result && result.code === 200) {
type = typeMap[type];
getOrderList(type, page);
} else {
new _alert('<h1>出错了!请重试!</h1>').show();
}
}).fail(function(err) {
console.log(err);
new _alert('<h1>网络异常!</h1>').show();
});
}
// 取消订单
function getExpressInfo(code, $el) {
$.ajax({
url: '/me/getExpressInfo',
data: {
orderCode: code
}
}).done(function(result) {
var tpl;
var data = {};
if (result.code === 200) {
data = result.data;
data.sub_express_deatil = data.express_detail.slice(0, 2);
tpl = expressTpl(data);
$(tpl).appendTo($el);
}
}).fail(function(err) {
console.log(err);
});
}
// tab切换处理
// searchBar相关处理暂时不上线
$('.tabs li').on('click', function() {
var $this = $(this);
// var $searchBar = $('.search-bar');
var type = typeMap[$this.data('type')];
var page = getQueryString().page;
if (!$this.hasClass('active')) {
$('.tabs li.active').removeClass('active');
$this.addClass('active');
$this.find('.badge').text('');
// 搜索栏只在全部订单tab下才显示
// if (type !== 1) {
// $searchBar.addClass('vhide');
// } else {
// $searchBar.removeClass('vhide');
// }
getOrderList(type, page);
}
});
function bindDeleteEvent() {
$('.delete').on('click', function() {
var $this = $(this);
var code = $this.closest('.order').data('code');
var tip = new _dialog({
className: 'order-dialog delete-dialog',
content: '<h1>您确定要永久删除该订单么</h1>' +
'<p>永久删除后,订单将无法恢复,</p>' +
'<p>您将无法对该订单的商品申请售后服务,请谨慎操作。</p>',
btns: [
{
id: 'delete-confirm',
btnClass: ['confirm'],
name: '确认',
cb: function() {
deleteOrder(code);
tip.close();
}
},
{
id: 'delete-cancel',
btnClass: ['cancel-btn'],
name: '取消',
cb: function() {
tip.close();
}
}
]
}).show();
});
}
function bindCancelEvent() {
$('.cancel').on('click', function() {
var $this = $(this);
var code = $this.closest('.order').data('code');
cancelOrder.start(code, updateTableContent.bind(null, $this.closest('.order')));
});
}
function bindExpressEvent() {
$('.express').on('click', function() {
var $this = $(this);
var code = $this.closest('.order').data('code');
var $info = $this.find('.express-info');
if ($info.length) {
$info.toggleClass('hide');
} else {
getExpressInfo(code, $this);
}
});
}
function bindConfirmReceiveEvent() {
$('.confirm-receive').on('click', function() {
var code = $(this).closest('.order').data('code');
confirmReceive.done(code);
});
}
function bindEvent() {
bindPaginationClick();
bindDeleteEvent();
bindCancelEvent();
bindExpressEvent();
bindBuyEvent();
bindConfirmReceiveEvent();
}
bindEvent();
countDown.start();
setOrderTotal();