ticket-ensure.js
12 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
/**
* @description: 订单保存
* @time: 2015/12/21
*/
var $ = require('yoho.jquery');
var dialog = require('../common/dialog');
var accMul = function(arg1, arg2) {
var m = 0,
s1 = arg1.toString(),
s2 = arg2.toString();
try {
m += s1.split('.')[1].length;
} catch (e) {
}
try {
m += s2.split('.')[1].length;
} catch (e) {
}
return Number(s1.replace('.', '')) * Number(s2.replace('.', '')) / Math.pow(10, m);
};
//立即购买-门票
var TicketCat = {
el: {
$pan: $('.pan'),
$biok: $('#bisubmit'),
$loading: $('.loading'),
$payTotal: $('.play-total'),
$toPay: $('.ticket-play em'),
$errorTip: $('.errbitip'),
$tbody: $('table tbody', '.pay-wapper'),
$yohoCoinHelp: $('.yoho-coin-help'),
//门票js
$showTicketMobile: $('.show-ticket-mobile'),
$ticketMobile: $('.ticket-mobile'),
$ticketModifyBtn: $('.ticket-modify-btn'),
$setTicketMobile: $('.set-ticket-mobile'),
$ticketMobileBtn: $('.ticket-mobile-btn'),
$ticketMobileInput: $('.ticket-mobile-input'),
//变量
phoneReg: /^(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/,
numberReg: /^\d{11,30}$/
},
Data: {
buyNumber: 0,//购买总数量
productSku: 0
},
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;
},
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();
},
strFixed: function(str) {
return parseFloat(str).toFixed(2);
}
},
init: function() {
var _this = this,
el = this.el,
c = this.Common,
productSubtotal = 0;
//初始化产品价格(表格中的价格)
el.$tbody.find('tr').each(function(index) {
var td = $(this).find('td');
var len = el.$tbody.find('tr').length;
var subtotal = parseFloat(td.eq(1).html()) * parseInt(td.eq(3).html());
_this.Data.productSku = $(this).data('sku');
_this.Data.buyNumber += 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));
}
productSubtotal += subtotal;
if (!index) {
td.last().attr('rowspan', len);
} else {
td.last().remove();
}
if (index === len - 1) {
el.$tbody.find('tr').eq(0).find('td').last().html('<strong>¥' +
c.strFixed(productSubtotal) + '</strong>');
}
});
//修改手机号按钮
el.$ticketModifyBtn.click(function() {
el.$showTicketMobile.addClass('hide');
el.$setTicketMobile.removeClass('hide');
});
//保存手机号按钮
el.$ticketMobileBtn.click(function() {
var val = el.$ticketMobileInput.val();
if (!el.numberReg.test(val)) {
new dialog.Alert('请输入正确的手机号!').show();
return;
}
el.$showTicketMobile.removeClass('hide');
el.$setTicketMobile.addClass('hide');
el.$ticketMobile.text(val);
window.setCookie('ticketMobileInput', val, {
domain: '.yohobuy.com',
path: '/'
});
});
//手机号,保存cookie中,防止用户刷新
if (el.numberReg.test(window.cookie('ticketMobileInput'))) {
el.$showTicketMobile.removeClass('hide');
el.$setTicketMobile.addClass('hide');
el.$ticketMobile.text(window.cookie('ticketMobileInput'));
}
// 使用优惠券、使用有货币点击展开
el.$pan.find('dt').click(function() {
var pan = $(this).parent('dl.pan');
if ($(this).hasClass('not-btn')) {
return false;
}
if (pan.find('dd').is(':hidden')) {
c.winShow(pan);
$(this).addClass('active');
} else {
c.winHide(pan);
$(this).removeClass('active');
}
});
//使用有货币确认按钮
el.$biok.click(function() {
var pan = $(this).parents('dl.pan');
var selectData = {};
$('#biprice').addClass('is-select');
selectData = _this.getSelectData();
el.$loading.show();
$.ajax({
type: 'POST',
url: '/cart/index/setTicket',
data: selectData,
success: function(data) {
var result = data.data;
$('.errbitip').html('');
el.$loading.hide();
if (data.code === 200) {
if (result.yohoCoinCompute && !result.yohoCoinCompute.yohoCoinClick) {
$('.errbitip').html(result.yohoCoinCompute.yohoCoinMsg);
$('#biprice').removeClass('is-select');
$('.yohoCoinTip').addClass('coin-gray');
return false;
}
_this.render(result);
c.winHide(pan);
} else {
$('.errbitip').html(data.message);
$('#biprice').removeClass('is-select');
}
}
});
});
// 修改有货币
$(document).on('click', '.bi-modify', function() {
var $pan = $('.play-bi-pan');
c.winShow($pan);
});
//键盘事件,计算有币货抵多少钱
$('#biprice').keyup(function() {
var val = $(this).val();
if (!/^\d+$/.test(val + '')) {
el.$errorTip.html('请输入一个正整数');
} else if (val * 1 > $(this).data('bi') * 1) {
el.$errorTip.html('您的有货币不足');
} else {
el.$errorTip.html('抵扣' + (val / 100).toFixed(2) + '元');
}
});
// 取消yoho 币
$('.cancel-bi').click(function() {
var $pan = $('.play-bi-pan'),
selectData = {};
$('.errbitip').html('');
el.$loading.show();
selectData = _this.getSelectData();
selectData.yohoCoin = 0;
$.ajax({
type: 'POST',
url: '/cart/index/setTicket',
data: selectData,
success: function(data) {
el.$loading.hide();
if (data.code === 200) {
_this.render(data.data);
c.winHide($pan);
$('#biprice').removeClass('is-select');
$('.errbitip').html('');
} else {
$('.errbitip').html(data.message);
}
}
});
});
// 去支付
$('.ticket-play input.submit').click(function() {
var $this = $(this);
var selectData = _this.getSelectData();
selectData.mobile = el.$ticketMobile.text();
if (!el.numberReg.test(selectData.mobile)) {
new dialog.Alert('手机号格式不正确!').show();
return;
}
el.$loading.show();
if (!!$this.attr('disabled')) {
return;
}
$this.attr('disabled', 'disabled');
$.ajax({
type: 'POST',
dataType: 'json',
url: '/cart/index/ticketOrderSub',
data: selectData
}).then(function(data) {
el.$loading.hide();
if (data.code === 200) {
//域名不一样,导直连接错误
window.location.href =
'//www.yohobuy.com/shopping/pay?ordercode=' + data.data.order_code;
} else {
new dialog.Alert(data.message).show();
}
}).always(function() {
el.$loading.hide();
$this.removeAttr('disabled');
});
});
//有货币帮助弹框
el.$yohoCoinHelp.hover(function() {
$(this).find('.yoho-coin-help-dialog').removeClass('hide');
}, function() {
$(this).find('.yoho-coin-help-dialog').addClass('hide');
});
},
//渲染价格明细
render: function(data) {
var e = this.el,
i,
dArr = data.shopping_cart_data.promotion_formula_list,
str = '',
isJuan = false,
isExpress = '';
for (i = 0; i < dArr.length; i++) {
if (dArr[i].promotion === '有货币') {
dArr[i].promotion = '有货币<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.shopping_cart_data.last_order_amount).toFixed(2));
//有货币清零状态更新 start
$('dl.play-bi-pan').find('dt').removeClass('not-btn');
$('.not-btn-tip').addClass('hide');
$('.yohoCoinTip').removeClass('coin-gray');
if (data.yohoCoinCompute.yohoCoinNum > 0) {
$('.errbitip').html('');
}
if (!data.yohoCoinCompute.yohoCoinClick) {
if (isJuan) {
//不可以使用状态
$('dl.play-bi-pan').find('dt').addClass('not-btn');
$('.not-btn-tip').removeClass('hide');
//如果有货币按钮为打开状态,则关闭
_this.Common.winHide($('.play-bi-pan'));
}
$('.yohoCoinTip').addClass('coin-gray');
$('.errbitip').html(data.yohoCoinCompute.yohoCoinMsg);
}
$('#biprice').data({
'yoho-coin-num': data.yohoCoinCompute.yohoCoinNum,
'yoho-coin-click': data.yohoCoinCompute.yohoCoinClick
});
$('#biprice').find('.num-limit-coin').html(data.yohoCoinCompute.yoho_coin_pay_rule.num_limit);
$('#biprice').find('.totalYohoCoinNum').html(data.yohoCoinCompute.totalYohoCoinNum);
$('#biprice').find('.yohoCoinNum').html(data.yohoCoinCompute.yohoCoinNum);
$('#biprice').find('.yohoCoin').html(data.yohoCoinCompute.yohoCoin);
//有货币清零状态更新 end
},
getSelectData: function() {
var expressVal = $('.express-list input[name="carriagegroup"]:checked').val();
var biVal = $('#biprice.is-select').data('yoho-coin-num') * 1 || 0;
var cartType = $('.address-manage').attr('cart-type');
var resultData;
resultData = {
cartType: cartType,
deliveryWay: expressVal,
yohoCoin: biVal,
productSku: this.Data.productSku,
buyNumber: this.Data.buyNumber
};
return resultData;
}
};
TicketCat.init();