save.js
8.88 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
/**
* @description: 订单保存
* @time: 2015/12/21
*/
var $ = require('yoho.jquery');
var address = require('./address');
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'),
$existAddressList: $('.exist-address-list').find('li'),
$payTimeModify: $('.pay-time-modify'),
$switchPayModify: $('.switch-pay-modify')
},
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(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>');
}
});
/*初始化价格*/
this.render();
/*点击pan dt 打开dd*/
e.$pan.find('dt').click(function() {
var pan = $(this).parent('dl.pan');
if (pan.find('dd').is(':hidden')) {
c.winShow(pan);
} else {
c.winHide(pan);
}
});
/*使用优惠卷支付 下面的文本框*/
e.$jc.on('focus change', function(event) {
var radio = $(this).prev('label').prev(':radio');
if (event.type === 'focus') {
radio.attr('checked', true);
} else {
radio.val($(this).val());
}
});
/*使用YOHO币支付*/
e.$bp.data('tobi', e.$tobi.html());
e.$bp.on('keyup change', function() {
var bi = c.enterNUM($(this).val(), $(this).data('tobi'));
$(this).val(bi);
});
/*取消关闭小窗口*/
e.$cancel.click(function() {
var pan = $(this).parents('.pan');
c.winHide(pan);
});
e.$statistics.slice(3, 5).find('a').click(function() {
var pan = $('.' + $(this).attr('to'));
c.winShow(pan);
});
//请填写并核对一下信息
e.$orderSelectionSave.click(function() {
if (!$(this).data('expand')) {
$('.address-list-inner').addClass('hide');
$(this).data('expand', 'expand').text('[修改]');
} else {
$('.address-list-inner').removeClass('hide');
$(this).removeData('expand').text('[不保存并关闭]');
}
});
//展开新地址填写表单
e.$addAddress.click(function() {
if (e.$addressManage.hasClass('hide')) {
e.$addressManage.removeClass('hide');
} else {
return;
}
});
e.$spanPayTypeLegend.click(function() {
$(this).toggleClass('active');
e.$supportType.toggleClass('hide');
});
e.$existAddressList.click(function(event) {
e.$existAddressList.removeClass('active');
$(this).addClass('active');
if ($(this).hasClass('use-new-address')) {
e.$addAddress.attr('checked', true);// 必须在trigger前
e.$addAddress.trigger('click');
}
event.stopPropagation();
});
e.$switchPayModify.click(function() {
e.$payTimeModify.toggleClass('hide');
});
},
//渲染价格明细
render: function() {
var e = this.e,
d = Order.Data,
c = Order.Common;
var i = 0;
e.$total.html('0.00');
e.$statistics.find('em').html('0.00');
e.$statistics.slice(3, 5).hide();
$.each(d, function(key) {
if (d.hasOwnProperty(key)) {
if (d[key]) {
e.$statistics.eq(i).show().find('em').html(c.strFixed(d[key]));
}
}
i++;
});
e.$total.html(c.strFixed(c.calucate()));
}
},
/*公共方法*/
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();
},
strFixed: function(str) {
return parseFloat(str).toFixed(2);
}
},
/*业务逻辑*/
Bll: {
e: {
$juanok: $('#juansubmit'),
$juantip: $('.errtip', '.play-juan-pan'),
$biok: $('#bisubmit'),
$carriageGroup: $('input[name="carriagegroup"]')
},
init: function() {
var e = this.e,
d = Order.Data,
c = Order.Common,
u = Order.UI;
var options;
e.$carriageGroup.change(function(){
//d.carriage=parseFloat(/\d+(\.\d+)?/i.exec($(this).next("label").html()));
d.carriage=parseFloat($(this).val());
u.render();
});
/*点击 优惠卷支付 确认按钮*/
e.$juanok.click(function() {
var code = $('input[name="juangroup"]:checked').val();
var pan = $(this).parents('.pan');
if (code) {
options = {
url: '/Order/save/priceByCode',
type: 'post',
data: {
code: code
},
success: function(data) {
if (data.code === 200) {
if (data.price) {
d.juan = data.price;
Order.UI.render();
c.winHide(pan);
return;
}
u.render();
}
e.$juantip.html(data.msg);
},
error: function() {
e.$juantip.html('网络出现错误~');
}
};
$.ajax(options);
}
});
/*点击 YOHO币支付 确认按钮*/
e.$biok.click(function() {
var pan = $(this).parents('.pan');
d.yohocoin = parseFloat(u.e.$bp.val()) / 100;
u.render();
c.winHide(pan);
return;
});
}
}
};
Order.Data.activity = 0;
Order.Data.carriage = 0;
Order.UI.init();
Order.Bll.init();
address.newAddress();
address.modifyAddress();
address.saveAddress();