exchange.page.js
12.2 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
var colorTpl = require('../../tpl/me/color-list.hbs');
var sizeTpl = require('../../tpl/me/size-list.hbs');
var lazyLoad = require('yoho-jquery-lazyload');
var dialog = require('../plugins/dialog');
var _alert = dialog.Alert;
var addrSelector = require('../plugins/cascading-address');
var addr;
var addressReg = /^[\s\S]{2,100}$/;
var phoneReg = /^(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])(\*{4}[0-9]{4}|[0-9]{8})$/;
var validate = require('./order/validation');
var imgBoxTpl = require('../../tpl/me/thumbnail.hbs');
var changeTypeTpl = require('../../tpl/me/change-type.hbs');
var validateMap = {
user: {
sl: '#user',
v: {
maxLength: 12,
minLength: 2,
necessary: true,
errMsg: '真实姓名至少2个字符,最多12个字符'
}
},
addr: {
sl: '#addr',
v: {
reg: addressReg,
errMsg: '详细地址不能为空'
}
},
mob: {
sl: '#mob',
v: {
reg: phoneReg,
errMsg: '您输入的联系电话格式不正确'
}
}
};
lazyLoad($('.banner-img'));
// 添加.check方法
require('../plugins/check');
// 个人中心共用代码加载
require('./me');
function setActive($item) {
var color = $item.find('.color-text').data('color');
var size = $item.find('.size-text').data('size');
var $colorList = $item.find('.color-list');
var $sizeList = $item.find('.size-list');
$colorList.find('.img-box').each(function(i, box) {
var $box = $(box);
if ($box.find('img').data('color') === color) {
$colorList.find('.img-box').eq(i).addClass('active');
return false;
}
});
$sizeList.find('span').each(function(i, s) {
var $size = $(s);
if ($size.data('size') === size) {
$sizeList.find('span').eq(i).addClass('active');
}
});
}
function renderList(data) {
var cTpl;
var sTpl;
var $el = $('.goods-container');
var resultId;
var resultSkn;
if (data) {
resultId = data.productId;
resultSkn = data.productSkn;
$el.each(function(index, item) {
var $item = $(item);
var id = $item.data('id');
var skn = $item.data('skn');
var $form;
if (id === resultId && skn === resultSkn) {
$form = $item.closest('.table-body').next('.form');
if (!$form.find('.color-list').length) {
cTpl = colorTpl(data);
sTpl = sizeTpl(data);
$form.find('.group.color').append(cTpl);
$form.find('.group.size').append(sTpl);
setActive($form);
}
}
});
}
}
function bindColorEvent() {
$('.color-list img').off('clice').on('click', function() {
var $this = $(this);
var $sizeList = $this.closest('.group').next('.group').find('.size-list');
var index = $this.data('index');
var colorId = $this.data('color');
var colorText = $this.attr('alt');
var $c = $this.closest('.group.color').find('.color-text');
$c.text(colorText);
$c.attr('data-color', colorId);
$this.closest('.color-list').find('.active').removeClass('active');
$this.parent().addClass('active');
$sizeList.removeClass('hide');
$sizeList.addClass('hide');
$sizeList.eq(index).removeClass('hide');
});
}
function initSizeId() {
var s = $('.size-list:not("hide")').find('span').eq(0).data('size');
$('.group.size .size-text').eq(0).attr('data-size', s);
}
function bindSizeEvent() {
$('.size-list span').off('click').on('click', function() {
var $this = $(this);
var s = $this.text();
var id = $this.data('size');
var $s = $this.closest('.group.size').find('.size-text');
$this.parent().find('.active').removeClass('active');
$this.addClass('active');
$s.text(s);
$s.attr('data-size', id);
});
}
function getProductInfo() {
var $el = $('.goods-container');
$el.each(function(index, item) {
var id = $(item).data('id');
var skn = $(item).data('skn');
if (id && skn) {
$.ajax({
url: '/me/return/getProductInfo',
data: {
productId: id,
productSkn: skn
}
}).done(function(result) {
if (result.code === 200) {
renderList(result.data);
initSizeId();
bindColorEvent();
bindSizeEvent();
}
});
}
});
}
function bindSelectEvent() {
$('.exchange-reasons').on('change', function() {
var $op = $(this).find('option:selected');
$(this).attr('data-reason', $op.val());
if ($op.hasClass('is-special')) {
$op.closest('.table-body').siblings('.special-reason').show();
} else {
$op.closest('.table-body').siblings('.special-reason').hide();
}
});
}
function bindTypeEvent() {
$('.change-type .type').on('click', function() {
$('.change-type .type').removeClass('active');
$(this).addClass('active');
});
}
function bindCheckboxEvent() {
$('.checkbox-box').check({
type: 'checkbox',
group: 'exchange-good',
onChange: function(ele, checked) {
var $box = $(ele).closest('.change-info-box');
if (checked) {
$box.addClass('will-change');
$box.find('.form').removeClass('hide');
} else {
$box.removeClass('will-change');
$box.find('.form').addClass('hide');
}
if ($('.will-change').length) {
$('.btn.disable').removeClass('disable');
} else {
$('.btn.confirm').addClass('disable');
}
}
});
}
function submitChange(d) {
$.ajax({
url: '/me/return/submitExchange',
data: d
}).done(function(result) {
if (result.code === 200) {
location.href = '/me/return';
} else {
new _alert('<h1>' + result.message + '</h1>').show();
}
}).fail(function(err) {
console.log(err);
new _alert('<h1>出错了!</h1>').show();
});
}
function validateData() {
var $inputs = $('.express-info-box .require input');
var pass = true;
$inputs.each(function(idx, input) {
var k = input.id;
if (k && validateMap[k]) {
if (!validate.start(validateMap[k].sl, validateMap[k].v)) {
pass = false;
}
}
});
// 检查地址
if (!validate.addr(addr.getAreaIds(), {
el: '#city',
errMsg: '请填写完整的省市区信息'
})) {
pass = false;
}
return pass;
}
function bindConfirmEvent() {
$('.confirm').on('click', function() {
var $changeGood = $('.change-info-box.will-change');
var changeGoodsList = [];
var changeData = {};
var $specialReason = $changeGood.find('.special-reason');
var $imgs;
var imgs = [];
var mobile = $('#mob').val();
if ($(this).hasClass('disable')) {
return false;
}
if (!validateData()) {
return false;
}
$changeGood.each(function(index, good) {
var goodObj = {};
var $good = $(good);
var $c = $good.find('.goods-container');
var $f = $good.find('.form');
goodObj.product_skn = $c.data('skn');
goodObj.product_skc = $c.data('skc');
goodObj.product_sku = $c.data('sku');
goodObj.last_price = $c.data('price');
goodObj.product_id = $c.data('id');
goodObj.goods_type = $c.data('goodsType');
goodObj.exchange_reason = $c.find('.exchange-reasons').data('reason');
goodObj.new_goods_id = $f.find('.color-list .img-box.active img').data('goodsId');
goodObj.new_product_sku = $f.find('.size-list span.active').data('sku');
if ($specialReason.length && $specialReason.get(0).style.display === 'block') {
goodObj.remark = $specialReason.find('.right-content').find('.mark-text').val();
$imgs = $specialReason.find('.right-content').find('.thumb-box');
$imgs.each(function(idx, img) {
imgs.push($(img).data('img'));
});
goodObj.evidence_images = imgs;
}
changeGoodsList.push(goodObj);
});
changeData.app_type = 1; // BLK的类型规定为1
changeData.goods = changeGoodsList;
changeData.area_code = addr.getAreaIds().split(',')[2];
changeData.order_code = $('.returns-wrap>.order').data('code');
changeData.consignee_name = $('#user').val();
changeData.address = $('#addr').val();
changeData.delivery_tpye = $('.change-type .type.active').data('type');
if (!/\*/.test(mobile)) {
changeData.mobile = $('#mob').val();
}
submitChange(changeData);
});
}
function bindBlurEvent() {
$('.express-info-box .require input').on('blur', function() {
var $this = $(this);
var v = validateMap[this.id];
if ($this.val().length > 0) {
validate.start(v.sl, v.v);
}
});
}
function initAddr(areaCode) {
addr = addrSelector({
el: '#city'
});
if (areaCode) {
addr.setAddress(areaCode.toString());
}
}
function bindImgDeleteEvent() {
$('.operation-box .delete').off('click').on('click', function() {
var $thumb = $(this).closest('.thumb-box');
var $t = $thumb.siblings('.img-up-tip');
var number = $(this).closest('.left.right-content').find('.thumb-box').length - 1;
$t.text(number + '/4');
$thumb.siblings('.img-upload').show();
$thumb.remove();
});
}
function bindUploadEvent() {
var $tip = $('.img-up-tip');
$tip.text('0/4');
$('.img-upload').on('click', function() {
var upload = require('../plugins/upload');
var that = this;
var n = parseInt($(that).siblings('.thumb-box').length, 10);
if (n < 4) {
upload.up({
callback: function(result) {
var img;
var $t;
var o;
if (result.code === 200) {
n = parseInt($(that).siblings('.thumb-box').length, 10);
o = {src: result.data};
if (result.imgs && result.imgs.length) {
o.url = result.imgs[0];
}
img = imgBoxTpl(o);
$t = $(that).next('.img-up-tip');
$(that).before(img);
n += 1;
$t.text(n + '/4');
bindImgDeleteEvent();
if (n >= 4) {
$(that).hide();
}
} else {
new _alert(result.message).show();
}
}
});
}
});
}
function bindKeyUpEvent() {
$('.mark-text').on('keyup', function() {
var $this = $(this),
str = $.trim($this.val());
if (str.length > 100) {
str = str.substring(0, 100);
}
$this.val(str);
});
}
function getChangeType(areaCode) {
$.ajax({
url: '/me/return/getChangeType',
data: {
areaCode: areaCode
}
}).done(function(result) {
var changeTypeHtml = changeTypeTpl({
type: result.data
});
if (changeTypeHtml.length) {
$('.change-type .box-title').after(changeTypeHtml);
bindTypeEvent();
}
});
}
$(document).on('ready', function() {
var areaCode = $('#city').data('code');
getProductInfo();
getChangeType(areaCode);
initAddr(areaCode);
bindSelectEvent();
bindCheckboxEvent();
bindConfirmEvent();
bindBlurEvent();
bindUploadEvent();
bindKeyUpEvent();
});