magazine-confirm.page.js
3.09 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
require('scss/cart/magazine-confirm.page.scss');
const $ = require('yoho-jquery');
const toast = require('js/plugin/tip');
const dialog = require('js/plugin/dialog');
let $activeItem = $('.choose-list .active');
let $totalPrice = $('.total-price .total-num');
let $customNumInput = $('#custom-num-input');
let buyNumber = 1;
let maxBuyNumber = 2000;
let productSku = $totalPrice.data('sku');
let submiting = false;
$('.choose-list').on('click', '.choose-item', function(e) {
let $elm = $(e.currentTarget);
if ($elm.hasClass('custom-choose')) {
$customNumInput.change();
} else {
let data = $elm.data();
$totalPrice.text(data.price);
buyNumber = data.num;
}
$activeItem.length && $activeItem.removeClass('active');
$activeItem = $elm.addClass('active');
});
$customNumInput.on('input', function() {
let val = $(this).val();
let handelVal = Math.abs(parseInt(`0${val}`, 10));
handelVal = Math.min(handelVal, maxBuyNumber);
handelVal = handelVal > 0 ? handelVal : '';
if (val !== handelVal) {
$(this).val(handelVal);
}
}).on('change', function() {
let val = $(this).val();
if (val > 0) {
$.ajax({
method: 'POST',
url: '/cart/magazine/order/compute',
data: {
sku: productSku,
buyNumber: val
},
success: function(ret) {
if (ret.code === 200) {
$totalPrice.text(`¥ ${ret.data.lastOrderAmount}`);
} else {
toast.show(ret.message || '网络出了点问题~');
}
}
});
}
});
$('.buy-btn-wrap').on('click', 'button', function() {
if (submiting) {
return toast.show('点太快了,休息一下~');
}
submiting = true;
setTimeout(() => {
submiting = false;
}, 3000);
$.ajax({
method: 'POST',
url: '/cart/magazine/order/submit',
data: {
sku: productSku,
buyNumber: buyNumber
}
}).then(ret => {
if (ret.code === 200) {
location.href = '/home/orders/paynew?order_code=' + ret.data.order_code;
} else {
toast.show(ret.message || '网络出了点问题~');
}
});
});
$('.view-buy-tip').on('click', function() {
dialog.showDialog({
hasClass: 'magazine-buytip-dialog',
hasHeader: '购买须知',
dialogText: '<ul><li>购买成功后,需要兑换阅读码阅读;</li>' +
'<li>电子刊为虚拟商品,若无系统问题,购买成功后不支持退换;</li>' +
'<li>购买多个电子刊阅读码,可以在【我的阅读码-阅读码】中赠送给好友兑换阅读;</li>' +
'<li>若出现无法阅读的情况,可以联系客服反馈问题,情况核对属实,将为您补发;</li></ul>',
hasFooter: {
centerBtnText: '知道了'
}
});
});
if ($activeItem.length) {
buyNumber = $activeItem.data().number || 1;
}
if ($customNumInput) {
maxBuyNumber = $customNumInput.data('max');
}