order-block.js
21.8 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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
/**
* 订单
* @author: xuqi<qi.xu@yoho.cn>
* @date: 2016/2/17
*/
var $ = require('yoho-jquery');
var ordersApi = require('./orders-api');
var dialog = require('../../common/dialog');
var stringHandle = require('../../common/stringHandle');
var Dialog = dialog.Dialog;
var Confirm = dialog.Confirm;
var $tpl = $('#cancel-dialog-tpl');
var html = $tpl.html();
var active;
var $dialogEdit = $('#edit-dialog-tpl');
var $addressManage, $province, $city,
$county, $street, $selectList, $name, $phone, $address, $telCode, $tel;
var isProvinceChecked = false;
var refundDialogTpl = require('hbs/home/orders/refund-dialog.hbs');
var yas = require('../../common/data-yas');
function cancelFactory(id) {
var options = {
mask: false,
btns: [
{
id: 'cancel-sure',
name: '确定并取消订单',
btnClass: ['cancel-sure'],
cb: function() {
var $checked = $('.cancel-dialog :checked');
var $tip = $('.cancel-dialog .cancel-tip');
if ($checked.length === 0) {
$tip.html('请选择要取消订单的原因');
return;
}
$.ajax({
type: 'POST',
url: '/home/orders/cancelorder',
data: {
orderCode: id,
reason: $checked.val()
}
}).then(function(data) {
if (data.code === 200) {
active.close();
history.go(0);
} else {
$tip.html(data.message);
}
});
}
},
{
id: 'cancel-no',
name: '取消',
btnClass: ['cancel-no'],
cb: function() {
active.close();
}
}
],
content: html,
className: 'cancel-dialog'
};
return new Dialog(options);
}
function showRefundDialog(orderId) {
return ordersApi.refundReason().then(function(result) {
var options = {
mask: false,
btns: [
{
id: 'cancel-sure',
name: '确定并取消订单',
btnClass: ['cancel-sure'],
cb: function() {
var $checked = $('.cancel-dialog :checked');
var $tip = $('.cancel-dialog .cancel-tip');
if ($checked.length === 0) {
$tip.html('请选择要取消订单的原因');
return;
}
ordersApi.refund(orderId, $checked.val()).then(function(data) {
if (data.code === 200) {
active.close();
history.go(0);
} else {
$tip.html(data.message);
}
});
}
},
{
id: 'cancel-no',
name: '取消',
btnClass: ['cancel-no'],
cb: function() {
active.close();
}
}
],
content: refundDialogTpl(result),
className: 'cancel-dialog'
};
active = new Dialog(options);
active.show();
});
}
function showMessageDialog(messageIcon, messageTitle, messageSummary, cb) {
var template = require('hbs/home/orders/dialog-message.hbs');
var messageHtml = template({
messageIcon: messageIcon,
messageTitle: messageTitle,
messageSummary: messageSummary}
);
var options = {
mask: true,
btns: [
{
id: 'message-sure',
name: '确定',
btnClass: ['message-sure'],
cb: function() {
active.close();
cb && cb();
}
}
],
content: messageHtml,
className: 'message-dialog'
};
return new Dialog(options);
}
// 再次购买
function buyAgain(id) {
$.ajax({
type: 'post',
url: '/home/orders/reAdd',
data: {orderCode: id}
}).then(function(d) {
if (d.code === 200) {
location.href = '/cart/cart';
} else {
new dialog.Alert(d.message).show();
}
});
}
function deleteOrder(id, obj) {
// 是否为彻底删除
var isFullyDelete = true;
var $order = $(obj).parents('.order');
var deleteMessage = isFullyDelete ? '您确定要永久删除订单吗?' : '您确定要删除订单吗?';
var deleteTip = isFullyDelete ? '永久删除后,订单将无法恢复,您将无法对该订单的商品申请售后服务,请谨慎操作。' : '删除后,您可以在订单回收站找回该订单,也可以做永久删除。';
var insert = require('hbs/home/orders/delete-order.hbs');
var deleteMessageHtml = insert({deleteMessage: deleteMessage, deleteTip: deleteTip});
var options = {
mask: false,
btns: [
{
id: 'delete-sure',
name: '确定',
btnClass: ['delete-sure'],
cb: function() {
$.ajax({
type: 'POST',
url: '/home/orders/delOrder',
data: {
orderCode: id,
isFullyDelete: isFullyDelete
}
}).then(function(data) {
if (data.code === 200) {
active.close();
$order.remove();
location.reload();
} else {
active.close();
new dialog.Alert(data.message).show();
}
});
}
},
{
id: 'delete-no',
name: '取消',
btnClass: ['delete-no'],
cb: function() {
active.close();
}
}
],
content: deleteMessageHtml,
className: 'delete-dialog'
};
return new Dialog(options);
}
// 构建select下拉选项
function structureOption($obj, data, selectId, countryId) {
var i,
optionHtml = '',
defaultOption,
isStar = '';
for (i = 0; i < data.length; i++) {
if (data[i].is_support_express === 'Y') {
isStar = '*';
} else {
isStar = '';
}
if (data[i].id === selectId) {
optionHtml += '<option selected value="' + data[i].id + '">' + isStar + data[i].caption + '</option>';
} else {
optionHtml += '<option value="' + data[i].id + '">' + isStar + data[i].caption + '</option>';
}
}
if ($obj.attr('name') === 'province') {
defaultOption = '<option value="0">请选择省份</option>';
} else if ($obj.attr('name') === 'city') {
defaultOption = '<option value="0">请选择城市</option>';
} else if ($obj.attr('name') === 'county') {
defaultOption = '<option value="0">请选择区县</option>';
} else if ($obj.attr('name') === 'street') {
defaultOption = '<option value="0">请选择乡镇/街道</option>';
if (countryId) {
defaultOption += '<option value="' + countryId + '">全部</option>';
}
}
$obj.html(defaultOption + optionHtml);
}
/**
* @description: 改函数会返回地址信息
* d {Object} type: 'getProvince'获取省 type: 'getCity'获取城市 type: 'getCounty'获取县
* id: 0 && type: 'getProvince' 获取所有省,默认没有选中项
* id !== 0 && type: 'getProvince' 获取所有省,默认选中用户所在的省
*/
function getAddress(d, callback) {
var $obj;
var selectId = d.selectId;
if (d.type === 'getProvince') {
// url = 'getProvince';
$obj = $province;
} else if (d.type === 'getCity') {
// url = 'getCity';
$obj = $city;
} else if (d.type === 'getCounty') {
// url = 'getCounty';
$obj = $county;
} else if (d.type === 'getStreet') {
$obj = $street;
}
$.ajax({
type: 'GET',
url: '/cart/address/area',
dataType: 'json',
data: {
id: d.id * 1
}
}).then(function(result) {
structureOption($obj, result.data, selectId, d.id);
if (typeof callback === 'function') {
return callback();
}
});
}
function validateForm() {
var $errName = $name.siblings('.error'),
$errProvince = $province.siblings('.error'),
$errAddress = $address.siblings('.error'),
$errPhone = $phone.siblings('.error'),
$errTel = $tel.siblings('.error');
var nameReg = /^([\u4e00-\u9fa5\w*]{1,20})$/;
var addressReg = /^[a-zA-Z0-9-#()()\u4e00-\u9fa5]+$/;
var phoneReg = /^(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/;
var telCodeReg = /^[0-9]{3,4}$/;
var telReg = /^[0-9]{8}$/;
var _rightHtml = '<i class="order-icon icon-right"></i>';
var _errorHtml = '<i class="order-icon icon-error"></i><b></b>';
return {
name: function(name) {
if (stringHandle.chinese2(name) > 20) {
$errName.html(_errorHtml);
$errName.find('b').html('收货人姓名最多支持20个字符');
$errName.show();
return false;
} else if (!nameReg.test(name)) {
$errName.html(_errorHtml);
$errName.find('b').html('收货人姓名不支持特殊符号');
$errName.show();
return false;
} else {
$errName.html(_rightHtml).show();
return true;
}
},
city: function(province, city, county) {
if (province === '0' || city === '0' || county === '0') {
$errProvince.html(_errorHtml);
$errProvince.find('b').html('请填写完整的省市区信息');
$errProvince.show();
return false;
} else {
$errProvince.html(_rightHtml).show();
return true;
}
},
address: function(address) {
var message = '';
if (!addressReg.test(address)) {
message = '详细地址不能为空';
if ($.trim(address) !== '') {
message = '只能包含数字、字母、汉字、#、-、()及其组合';
}
$errAddress.html(_errorHtml);
$errAddress.find('b').html(message);
$errAddress.show();
return false;
} else {
$errAddress.html(_rightHtml).show();
return true;
}
},
mobile: function(phone) {
var message = '';
var originMobile = $('.origin-user-mobile').val();
if (originMobile === phone) {
return true;
}
if (!phoneReg.test(phone)) {
message = '手机号码不能为空';
if ($.trim(phone) !== '') {
message = '你输入的联系电话格式不正确';
}
$errPhone.html(_errorHtml);
$errPhone.find('b').html(message);
$errPhone.show();
return false;
} else {
$errPhone.html(_rightHtml).show();
return true;
}
},
tel: function(telCode, tel) {
if (telCode === '' && tel === '') {
_rightHtml = '';
}
if ((!!telCode && !telCodeReg.test(telCode)) || (!!tel && !telReg.test(tel)) ||
(telCodeReg.test(telCode) && !telReg.test(tel)) ||
(!telCodeReg.test(telCode) && telReg.test(tel))) {
$errTel.html(_errorHtml);
$errTel.find('b').html('你输入的电话格式不正确');
$errTel.show();
return false;
} else {
$errTel.html(_rightHtml).show();
return true;
}
}
};
}
// 保存地址
function saveAddress(id) {
var name = $name.val(),
province = $province.val(),
city = $city.val(),
county = $county.val(),
street = $street.val(),
address = $address.val(),
phone = $phone.val(),
telCode = $telCode.val(),
tel = $tel.val();
var postData = {
orderCode: id,
userName: name,
areaCode: street,
address: address,
mobile: phone,
phoneCode: telCode,
phoneNum: tel
};
var validate = validateForm();
if (!validate.name(name) ||
!validate.city(province, city, county, street) ||
!validate.address(address) ||
!validate.mobile(phone) ||
!validate.tel(telCode, tel)) {
return;
}
if (postData.mobile === $('.origin-user-mobile').val()) {
delete postData.mobile;
}
$.ajax({
type: 'post',
url: '/home/orders/modifyAddress',
data: postData
}).then(function(d) {
if (d.code === 200) {
active.close();
active = showMessageDialog('icon-waiting', '订单修改', '您的订单正在尝试修改,请耐心等待。稍后可在订单详情页查看修改情况!', function() {
location.reload(true);
});
active.show();
} else {
new dialog.Alert(d.message).show();
}
});
}
// 编辑订单
function editOrder(id) {
var options = {
mask: true,
btns: [
{
id: 'edit-sure',
name: '确定',
btnClass: ['edit-sure'],
cb: function() {
saveAddress(id);
}
},
{
id: 'edit-no',
name: '取消',
btnClass: ['edit-no'],
cb: function() {
active.close();
}
}
],
content: $dialogEdit.html(),
className: 'edit-order-dialog'
};
return new Dialog(options);
}
// 地址操作
function newAddress(id) {
var code, codeId;
var pId = id || 0; // 如果没有传id则获取所有省列表
var addressCodeReg = /[0-9]{2}/gi;
$addressManage = $('.edit-order-dialog');
$province = $addressManage.find('select[name="province"]');
$city = $addressManage.find('select[name="city"]');
$county = $addressManage.find('select[name="county"]');
$street = $addressManage.find('select[name="street"]');
$selectList = $addressManage.find('select[name="province"],select[name="city"],select[name="county"]');
$name = $addressManage.find('.inp[name="name"]');
$phone = $addressManage.find('.inp[name="phone"]');
$address = $addressManage.find('.inp[name="address"]');
$telCode = $addressManage.find('.inp[name="tel-code"]');
$tel = $addressManage.find('.inp[name="tel"]');
var validate = validateForm(); // eslint-disable-line
code = $province.data('areacode') + '';
codeId = !!code && code.match(addressCodeReg);
// 获取省
getAddress({
id: pId,
type: 'getProvince',
selectId: codeId[0]
}, function() {
var provinceId = $province.val();
if (provinceId !== '0') {
isProvinceChecked = true;
// 如果获取的省有默认选中项则获取市
getAddress({
id: provinceId,
type: 'getCity',
selectId: '' + codeId[0] + codeId[1]
}, function() {
var cityId = $city.val();
// 如果获取的市有默认选中项则获取县
if (cityId !== '0') {
getAddress({
id: cityId,
type: 'getCounty',
selectId: '' + codeId[0] + codeId[1] + codeId[2]
}, function() {
var countyId = $county.val();
// 如果获取的县有默认选中项则获取乡镇/街道
if (countyId !== '0') {
getAddress({
id: countyId,
type: 'getStreet',
selectId: code
});
}
});
}
});
} else {
$addressManage.find('select[name="city"]').html('<option value="0">请选择城市</option>');
$addressManage.find('select[name="county"]').html('<option value="0">请选择区县</option>');
$street.html('<option value="0">请选择乡镇/街道</option>');
}
});
// 发生change事件时获取下一级地址
$selectList.change(function() {
var $this = $(this);
if ($this.attr('name') === 'province') {
if ($this.val() === '0') {
$addressManage.find('select[name="city"]').html('<option value="0">请选择城市</option>');
} else {
getAddress({
id: $this.val(),
type: 'getCity'
}, function() {
isProvinceChecked = true;
});
}
$addressManage.find('select[name="county"]').html('<option value="0">请选择区县</option>');
$street.html('<option value="0">请选择乡镇/街道</option>');
}
if ($this.attr('name') === 'city' && isProvinceChecked) {
if ($this.val() === '0') {
$addressManage.find('select[name="county"]').html('<option value="0">请选择区县</option>');
} else {
getAddress({
id: $this.val(),
type: 'getCounty'
});
}
$street.html('<option value="0">请选择乡镇/街道</option>');
}
if ($this.attr('name') === 'county' && isProvinceChecked) {
if ($this.val() === '0') {
$street.html('<option value="0">请选择乡镇/街道</option>');
} else {
getAddress({
id: $this.val(),
type: 'getStreet'
});
}
}
});
$name.on('blur', function() {
validate.name($.trim($name.val()));
});
$address.on('blur', function() {
validate.address($.trim($address.val()));
});
$phone.on('blur', function() {
validate.mobile($.trim($phone.val()));
});
$tel.on('blur', function() {
validate.tel($.trim($telCode.val()), $.trim($tel.val()));
});
$street.on('change', function() {
validate.city($province.val(), $city.val(), $county.val(), $(this).val());
});
}
// 关闭查看物流
$('.logistics').on('click', '.close-logistics', function() {
$(this).closest('.logistics').addClass('hide');
});
// 订单列表&订单详情【取消订单和确认收货】
$('.me-orders, .order-detail').on('click', '.cancel-order', function() {
// 取消订单
active = cancelFactory($(this).closest('.order, .order-detail').data('id'));
active.show();
}).on('click', '.confirm-received', function() {
var id = $(this).closest('.order, .order-detail').data('id');
// 确认收货
active = new Confirm({
cb: function() {
$.ajax({
type: 'POST',
url: '/home/orders/confirmorder',
data: {
orderCode: id
}
}).then(function(data) {
if (data.code === 200) {
active.close();
history.go(0);
}
});
},
content: '您确定要确认收货吗?'
});
active.show();
}).on('click', '.order-delete', function() {
var id = $(this).closest('.order, .order-detail').data('id');
active = deleteOrder(id, this);
active.show();
}).on('click', '.edit-order', function() {
var orderId = $(this).closest('.order, .order-detail').data('id');
$(this).addClass('edit-order-active');
active = editOrder(orderId);
active.show();
newAddress(0);
traceChangeAddress(orderId); // eslint-disable-line
}).on('click', '.rebuy', function() {
var id = $(this).closest('.order, .order-detail').data('id');
buyAgain(id);
});
$tpl.remove();
$dialogEdit.remove();
// 查看物流
$('.check-logistics').click(function() {
var $this = $(this);
var orderId = $this.data('id');
var time = $this.data('time');
var paytype = $this.data('paytype');
ordersApi.getExpress(orderId, paytype, time).then(function(result) {
var tpl = require('hbs/home/orders/express.hbs');
if (result.logistics.length === 0) {
return;
}
$this.siblings('.logistics').html(tpl(result)).removeClass('hide');
});
});
$('.refund-apply').on('click', function() {
var orderId = $(this).closest('.order, .order-detail').data('id');
showRefundDialog(orderId);
traceRefund(orderId); // eslint-disable-line
});
function traceRefund(orderId) {
yas.givePoint('YB_ORDER_REFUND_C', {
ORD_NUM: orderId
});
}
function traceChangeAddress(orderId) {
yas.givePoint('YB_ORDER_MODIFY_ADDRESS_C', {
ORD_NUM: orderId
});
}