ensure.js
24.7 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
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
/**
* @description: 订单保存
* @time: 2015/12/21
*/
var $ = require('yoho.jquery');
var address = require('./address');
var dialog = require('../common/dialog');
var Order = {
/*业务数据(人民币)*/
Data: {
product: 0,//商品价格
activity: 0,//活动
carriage: 0,//邮费
juan: 0,//优惠卷
yohocoin: 0//YOHO币
},
/*view 交互*/
UI: {
e: {
$pan: $('.pan'),
$jc: $('#juancode'),
$bp: $('#biprice'),
$statistics: $('li', '.play-total'),
$tobi: $('p em', '.play-bi-pan .play-pan'),
$cancel: $('.cancel', '.btn-group'),
$total: $('em', '.to-play'),
$tbody: $('table tbody', '.pay-wapper'),
//请填写并核对一下信息
$orderSelectionSave: $('.address-list h2 span'),
$addAddress: $('.add-address'),
$addressManage: $('.address-manage'),
$spanPayTypeLegend: $('span.pay-type-legend'),
$supportType: $('.support-type'),
$existAddressListWrap: $('.exist-address-list'),
$existAddressList: $('.exist-address-list').find('li'),
$existAddress: $('.exist-address-list').find('input[name="exist-address"]'),
$payTimeModify: $('.pay-time-modify'),
$switchPayModify: $('.switch-pay-modify'),
$modityPayInfo: $('.modity-pay-info'),
$payTotal: $('.play-total'),
$toPay: $('.to-play em'),
$redEnvelopes: $('.red-envelopes'),
$useEnvelopes: $('.use-envelopes'),
$hasEnvelopes: $('.has-envelopes span'),
$useEnvelopesInput: $('.use-envelopes').find('input')
},
init: function() {
var e = this.e,
d = Order.Data,
c = Order.Common;
//初始化产品价格(表格中的价格)
e.$tbody.find('tr').each(function(index) {
var td = $(this).find('td');
var len = e.$tbody.find('tr').length;
var subtotal = parseFloat(td.eq(1).html()) * parseInt(td.eq(3).html());
td.eq(1).html('¥' + td.eq(1).html());
if (td.hasClass('xforone')) {
td.eq(4).find('del').html('¥' + c.strFixed(subtotal));
subtotal = 0;
} else {
td.eq(4).html('¥' + c.strFixed(subtotal));
}
d.product += subtotal;
if (!index) {
td.last().attr('rowspan', len);
} else {
td.last().remove();
}
if (index === len - 1) {
e.$tbody.find('tr').eq(0).find('td').last().html('<strong>¥' + c.strFixed(d.product) + '</strong>');
}
});
// 使用优惠券、使用yoho币点击展开
e.$pan.find('dt').click(function() {
var pan = $(this).parent('dl.pan');
if (pan.find('dd').is(':hidden')) {
c.winShow(pan);
$(this).addClass('active');
} else {
c.winHide(pan);
$(this).removeClass('active');
}
});
//请填写并核对一下信息
e.$orderSelectionSave.click(function() {
var i = 0,
$addressLi = $('.exist-address-list li'),
liLen = $addressLi.length,
notSelect = 0;
if (!$(this).attr('expand')) {
$(this).attr('expand', 'expand').text('[不保存并关闭]');
$('.exist-address-list li').removeClass('hide');
$('.save-btn').removeClass('hide');
e.$existAddressListWrap.addClass('address-expand');
if (liLen === 1) {
e.$addressManage.removeClass('hide');
}
} else {
for (i = 0; i < liLen; i++) {
if ($addressLi.eq(i).hasClass('address_select')) {
notSelect++;
}
if (!$addressLi.eq(i).hasClass('address_select')) {
$addressLi.eq(i).addClass('hide');
}
}
if (notSelect === 0) {
$('.exist-address-list li:not(".use-new-address")').removeClass('hide');
$('.use-new-address input').removeAttr('checked');
}
$('.address-list-inner li.address_select input').attr('checked', 'checked');
$('.address-list-inner li').removeClass('active');
$('.address-list-inner li.address_select').addClass('active');
$(this).removeAttr('expand').text('[修改]');
$('.save-btn').addClass('hide');
e.$existAddressListWrap.removeClass('address-expand');
e.$addressManage.addClass('hide');
}
});
//支付及送货时间
e.$switchPayModify.click(function() {
if (e.$payTimeModify.hasClass('hide')) {
e.$payTimeModify.removeClass('hide');
e.$modityPayInfo.addClass('hide');
$(this).text('[不保存并关闭]');
} else {
e.$payTimeModify.addClass('hide');
e.$modityPayInfo.removeClass('hide');
$(this).text('[修改]');
e.$payTimeModify.find('input[name="pay-type"]').filter('.checked').attr('checked', 'checked');
e.$payTimeModify.find('input[name="pay-time-radio"]').filter('.checked').attr('checked', 'checked');
e.$payTimeModify.find('input[name="call-me"]').filter('.checked').attr('checked', 'checked');
}
});
e.$spanPayTypeLegend.click(function() {
$(this).toggleClass('active');
e.$supportType.toggleClass('hide');
});
$(document).on('click', '.exist-address-list li', function() {
$('.exist-address-list li').removeClass('active');
$(this).addClass('active');
$(this).find('input').attr('checked', 'checked');
if ($(this).hasClass('use-new-address')) {
e.$addressManage.removeClass('hide');
$('.address-manage input').val('');
$('.save-btn').removeAttr('data-id');
// 0表示获取所有省的列表,'000000'表示无匹配的区域码
address.newAddress(0, '000000');
}
});
},
//渲染价格明细
render: function(data, tips) {
// 下面这段是在服务端算价格
var e = this.e,
i,
dArr = data.promotion_formula_list,
str = '',
isExpress = '';
var tipsStr;
var envelopesVal;
if (!!tips) {
tipsStr = tips;
} else {
tipsStr = '';
}
for (i = 0; i < dArr.length; i++) {
if (dArr[i].promotion === '优惠券:') {
dArr[i].promotion = (tipsStr ? tipsStr : '优惠券') + '<span class="juan-modify">[修改]</span>:';
}
if (dArr[i].promotion === 'YOHO币:') {
dArr[i].promotion = 'YOHO币<span class="bi-modify">[修改]</span>:';
}
if (dArr[i].isExpress) {
isExpress = 'total-express-w';
} else {
isExpress = '';
}
str += '<li class="' + isExpress + '">' +
'<label>' + dArr[i].promotion + '</label>' +
'<em>' + dArr[i].promotion_amount + '</em>' +
'</li>';
}
e.$payTotal.html(str);
e.$toPay.html((data.last_order_amount).toFixed(2));
//有货币清零状态更新
$('#biprice').val(100 * data.use_yoho_coin);
if (~~data.use_yoho_coin === 0) {
$('.errbitip').text('');
} else {
$('.errbitip').text('抵扣' + data.use_yoho_coin.toFixed(2) + '元');
}
envelopesVal = e.$useEnvelopesInput.data('all') - data.use_red_envelopes;
if (~~data.use_red_envelopes > 0) {
e.$useEnvelopes.find('span').html('-¥' + data.use_red_envelopes.toFixed(2));
e.$useEnvelopes.find('input').attr('data-use', data.use_red_envelopes);
} else {
e.$useEnvelopes.find('span').html('-¥0.00');
e.$hasEnvelopes.html('¥' + (e.$useEnvelopesInput.attr('data-all') * 1).toFixed(2));
e.$redEnvelopes.removeClass('active');
e.$useEnvelopesInput.prop('checked', false);
}
e.$hasEnvelopes.html('¥' + envelopesVal.toFixed(2));
}
},
/*公共方法*/
Common: {
enterNUM: function(str1, str2) {
str1 = str1.replace(/[^\d]/g, '');
if (str1 && str2) {
str1 = parseInt(str1);
str2 = parseInt(str2);
if (str1 > str2) {
str1 = str2;
}
}
return str1;
},
calucate: function() {
var p = Order.Data;
$.each(Order.Data, function(key, index) {
p[key] = parseFloat(Order.Data[key]);
});
return p.product - p.activity + p.carriage - p.juan - p.yohocoin;
},
winShow: function(pan) {
pan.find('dt').show();
pan.find('dd').show();
if (pan.attr('class').match(/(-bi-)|(-juan-)/g)) {
pan.find('dt').hide();
}
},
winHide: function(pan) {
pan.find('dt').show();
pan.find('dd').hide();
/*if (pan.hasClass('play-juan-pan') && $('.juan-modify').length > 0) {
pan.find('dt').hide();
}
if (pan.hasClass('play-bi-pan') && $('.bi-modify').length > 0) {
pan.find('dt').hide();
}*/
},
strFixed: function(str) {
return parseFloat(str).toFixed(2);
}
},
/*业务逻辑*/
Bll: {
e: {
$juanok: $('#juansubmit'),
$juantip: $('.errtip', '.play-juan-pan'),
$biok: $('#bisubmit'),
$carriageGroup: $('input[name="carriagegroup"]'),
$selectExpress: $('.express-list input'),
$bp: $('#biprice'),
$errorTip: $('.errbitip'),
$useEnvelopes: $('.use-envelopes'),
$hasEnvelopes: $('.has-envelopes span'),
$useEnvelopesInput: $('.use-envelopes').find('input'),
$redEnvelopes: $('.red-envelopes'),
$loading: $('.loading')
},
init: function() {
var e = this.e,
u = Order.UI,
options,
c = Order.Common,
envelopesVal;
function getSelectData(callback) {
var $juangroupInput = $('.play-juan.is-select input[name="juangroup"]:checked');
var expressVal = $('.express-list input[name="carriagegroup"]').val();
var biVal = $('#biprice.is-select').val();
var codeVal = $juangroupInput.val();
var cartType = $('.address-manage').attr('cart-type');
var redEnvelopes = $('.use-envelopes input:checked').attr('data-use');
var resultData;
codeVal = !!codeVal ? codeVal : $juangroupInput.closest('li').find('#juancode').val();
resultData = {
cartType: cartType,
deliveryWay: expressVal,
couponCode: codeVal,
yohoCoin: biVal
//redEnvelopes: e.$useEnvelopesInput.length > 0 ? ~~e.$hasEnvelopes.text().substring(1) : '0'
};
if (!!redEnvelopes) {
resultData.redEnvelopes = redEnvelopes;
}
callback(resultData);
}
// 选择快递
e.$selectExpress.click(function() {
var expressId = $(this).val();
e.$loading.show();
getSelectData(function(d) {
d.deliveryWay = expressId;
options = {
type: 'POST',
url: '/cart/index/orderCompute',
data: d,
success: function(data) {
e.$loading.hide();
if (data.code === 200) {
u.render(data.data);
if (expressId === '1') {
$('.express-list.sf').addClass('hide');
} else if (expressId === '2') {
$('.express-list.sf').removeClass('hide');
}
} else {
new dialog.Alert(data.message).show();
}
}
};
$.ajax(options);
});
});
//点击 优惠卷支付 确认按钮
e.$juanok.click(function() {
var pan = $(this).parents('dl.pan');
var juanText = '';
$('.play-juan').addClass('is-select');
getSelectData(function(d) {
var code = d.couponCode;
if (!code) {
e.$juantip.html('请选择或填写一个优惠券');
$('.play-juan').removeClass('is-select');
} else {
e.$loading.show();
options = {
type: 'POST',
url: '/cart/index/orderCompute',
data: d,
success: function(data) {
e.$loading.hide();
if (data.code === 200) {
juanText = $('.play-juan input[value="' + code + '"]').next().text();
u.render(data.data, juanText);
//$('#juancode').val(code);
c.winHide(pan);
e.$juantip.html('');
//$('.play-juan-pan dt').hide();
} else {
e.$juantip.html(data.message);
$('.play-juan').removeClass('is-select');
}
}
};
$.ajax(options);
}
});
});
// 优惠券修改
$(document).on('click', '.juan-modify', function() {
var $pan = $('.play-juan-pan');
c.winShow($pan);
});
e.$biok.click(function() {
var pan = $(this).parents('dl.pan');
var juanText;
$('#biprice').addClass('is-select');
getSelectData(function(d) {
if (!!d.couponCode) {
juanText = $('.play-juan input[value="' + d.couponCode + '"]').next().text();
}
//if (d.yohoCoin * 1 === 0) {
if (!/^\d+$/.test(d.yohoCoin + '') || d.yohoCoin * 1 === 0) {
$('.errbitip').html('请输入一个正整数');
$('#biprice').removeClass('is-select');
} else if (d.yohoCoin * 1 > $('.play-pan em').text() * 1) {
$('.errbitip').html('您的YOHO币不足');
$('#biprice').removeClass('is-select');
} else {
e.$loading.show();
options = {
type: 'POST',
url: '/cart/index/orderCompute',
data: d,
success: function(data) {
e.$loading.hide();
if (data.code === 200) {
u.render(data.data, juanText);
c.winHide(pan);
//$('.play-bi-pan dt').hide();
} else {
$('.errbitip').html(data.message);
$('#biprice').removeClass('is-select');
}
}
};
$.ajax(options);
}
});
});
// 修改yoho币
$(document).on('click', '.bi-modify', function() {
var $pan = $('.play-bi-pan');
c.winShow($pan);
});
e.$bp.keyup(function() {
var val = $(this).val();
//if (val * 1 < 0 || val.indexOf('.') > 0) {
if (!/^\d+$/.test(val + '')) {
e.$errorTip.html('请输入一个正整数');
} else if (val * 1 > $(this).data('bi') * 1) {
e.$errorTip.html('您的YOHO币不足');
} else {
e.$errorTip.html('抵扣' + (val / 100).toFixed(2) + '元');
}
});
// 取消yoho 币
$('.cancel-bi').click(function() {
var $pan = $('.play-bi-pan');
var juanText;
$('.errbitip').html('');
if ($('#biprice').hasClass('is-select')) {
e.$loading.show();
getSelectData(function(d) {
d.yohoCoin = 0;
if (!!d.couponCode) {
juanText = $('.play-juan input[value="' + d.couponCode + '"]').next().text();
}
options = {
type: 'POST',
url: '/cart/index/orderCompute',
data: d,
success: function(data) {
e.$loading.hide();
if (data.code === 200) {
u.render(data.data, juanText);
c.winHide($pan);
$('#biprice').val('0');
//$('.play-bi-pan dt').hide();
$('#biprice').removeClass('is-select');
$('.errbitip').html('');
// $('.use-envelopes input').attr('checked', 'checked');
// $('.red-envelopes').addClass('active');
} else {
$('.errbitip').html(data.message);
}
}
};
$.ajax(options);
});
} else {
c.winHide($pan);
$('#biprice').val('0');
}
});
// 取消优惠券
$('.cancel-code').click(function() {
var $pan = $('.play-juan-pan');
if ($('.play-juan').hasClass('is-select')) {
e.$loading.show();
getSelectData(function(d) {
d.couponCode = '';
options = {
type: 'POST',
url: '/cart/index/orderCompute',
data: d,
success: function(data) {
e.$loading.hide();
if (data.code === 200) {
u.render(data.data);
c.winHide($pan);
$('.play-juan').removeClass('is-select');
$('#juancode').val('')
.siblings('input[name="juangroup"]').attr('checked', 'checked');
// $('.use-envelopes input').attr('checked', 'checked');
// $('.red-envelopes').addClass('active');
} else {
e.$juantip.html(data.message);
}
}
};
$.ajax(options);
});
} else {
c.winHide($pan);
}
});
// 红包
envelopesVal = e.$useEnvelopesInput.data('all') - e.$useEnvelopesInput.data('use'); // 剩余的红包
e.$hasEnvelopes.text('¥' + envelopesVal.toFixed(2));
e.$useEnvelopes.find('input').click(function() {
var $this = $(this),
juanText;
e.$loading.show();
$this.attr('disabled', 'disabled');
getSelectData(function(d) {
if (!!$this.attr('checked')) {
//d.redEnvelopes = $this.val();
d.redEnvelopes = $this.data('use');
} else {
d.redEnvelopes = 0;
}
juanText = $('.play-juan input[value="' + d.couponCode + '"]').next().text();
options = {
type: 'POST',
url: '/cart/index/orderCompute',
data: d,
success: function(data) {
e.$loading.hide();
if (data.code === 200) {
u.render(data.data, juanText);
$this.removeAttr('disabled');
if (!!$this.attr('checked')) {
e.$redEnvelopes.addClass('active');
} else {
e.$useEnvelopes.find('span').html('-¥0.00');
e.$hasEnvelopes.html('¥' + ($this.attr('data-all') * 1).toFixed(2));
e.$redEnvelopes.removeClass('active');
}
} else {
new dialog.Alert(data.message).show();
}
}
};
$.ajax(options);
});
});
//发票抬头和备注字数限制
function limitStrLen($obj, len) {
var $this = $obj;
$this.keyup(function() {
if ($this.val().length > len) {
$this.val($this.val().substr(0, len));
}
});
}
limitStrLen($('#piaodesc'), 30);
limitStrLen($('#notedesc'), 100);
}
}
};
Order.Data.activity = 0;
Order.Data.carriage = 0;
Order.UI.init();
Order.Bll.init();
address.getUserAddressList();
// 0表示获取所有省的列表,'000000'表示无匹配的区域码
address.newAddress(0, '000000');
address.modifyAddress();
address.saveAddress();