header.js
22.1 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
740
741
742
743
744
745
746
747
748
/**
* 公共头部
* @author: yyq<yanqing.yang@yoho.cn>
* @date: 2016/5/9
*/
var $ = require('yoho-jquery'),
handlebars = require('yoho-handlebars');
var $tool = $('.tool-wrapper'),
$yohoGroup = $tool.find('.yoho-group'),
$loginBox = $('#loginBox');
var $head = $('.head-wrapper'),
$searchForm = $('#search-form'),
$logotrans = $head.find('.main-logo'),
$searchSug = $head.find('.search-suggest'),
$goCart = $head.find('.go-cart'),
$goodsNum = $goCart.find('.goods-num-tip'),
$miniCart = $head.find('.mini-cart-wrapper');
var $subNav = $('.sub-nav-list .contain-third');
var thirdLineNum = 9,
delayer,
centerFn,
loginFn,
cartFn,
requestFrame;
var logoAngle = 0,
loopTime = 500,
keyword = '',
link = '',
reg = /^[\^\!\+\-\(\)\:\[\]\\\{\}\~\*\?\|\&\;\/]{0,}$/g;
var dataLayer = [];
// banner和地址的映射
var bannerMap = {
listboys: '4f78b0f418fc42314d8b6e791cfb7fa8',
listgirls: '00c1f025a51b6b597dc37925951ea27d',
listkids: 'b02df11184727701ade1b6de9737d08c',
listlifestyle: 'fd35c52dced0c880976ba858346d1fc5',
searchboys: '9fb8986ea700cc27a8057361c3924394',
searchgirls: 'e3e207a1443ca60c8037fe52a5560c18',
searchkids: '620fc77f479da8feaeb06f2324e5d0bb',
searchlifestyle: 'a3c93301c6ffaf3ed0f36a4a451be36d',
uniquebrandboys: '2ebb0810c0d1a67e5229149c9c3aba7d',
uniquebrandgirls: '99e23385f4ba4b65f406b7e2968ac821',
uniquebrandkids: 'a74ebc9b17840c91b9ea46568111fe6b',
uniquebrandlifestyle: '0e8c81ead53f56302baa4d0ad967f527',
brandsboys: '77b352db07129c76a9d532acad149f9f',
brandsgirls: 'bf047f16e52ebc38be5ce9c7623831e6',
brandskids: 'e3ae1ce9b5e13c6d271ef3eccb831652',
brandslifestyle: 'e4ac8029c30f65d7f1af030980d140fe',
newboys: '869d3c5f3b450fb52101d00a61ce87cb',
newgirls: 'd953b6dfdac02483d1dcce8d96055954',
newkids: '0874cb6d75df8e0e78f2d475e53ecc08',
newlifestyle: '43e8fc8e178115c262bbce2bd0012db7',
saleboys: 'c846e3165c994769b4201d8c32f3ae9b',
salegirls: '52b1d389edcbc62d65de71b80c4d6ad0',
salekids: 'ad8b1703c761ba00973868ab5199cc27',
salelifestyle: '7acc64905c70ac91846f43fb2cec4bbd',
homeboys: 'b0856a771ef1b59ab1234c74688fa42d',
homegirls: '2bd61fa12e4933211518f70fe5ce3c48',
homekids: '895c59e8c64b40399c9533509507320c',
homelifestyle: '2e037d4e25d2767352ca3e0a4627f7bd',
indexboys: '0c911d3000f52e8ca7cffb74f5864c29',
indexgirls: 'b645b8980c423ab30485e0a9d08c2ef7',
indexkids: '17f6d5d5d454d2c507bc5fcbc90f7756',
indexlifestyle: '735cd393e841762af8793c346abbbc36'
},
cookieMap = {};
// handlebars模板
centerFn = handlebars.compile($('#simple-account-info-tpl').html() || '');
loginFn = handlebars.compile($('#header-login-info-tpl').html() || '');
cartFn = handlebars.compile($('#mini-cart-tpl').html() || '');
// handlebars helper
handlebars.registerHelper('notzero', function(v1, options) {
if (v1 !== '0') {
return options.fn(this);
} else {
return options.inverse(this);
}
});
function getSource(column, postition, event) {
dataLayer.push({
louceng: column,
weizhi: postition,
event: event
});
}
function closeCover() {
var $cover = $('#cover');
$cover.remove();
}
// 设置头部banner
function setTopBanner(data) {
var topbanner = '';
if (data && data.url !== '') {
topbanner = '<a target="_blank" href="' + data.url + '" class="page-top-banner"' +
'style="height:36px;border:none;background-image:url(' + window.unescape(data.src) + ');' +
'background-position: center;display:block;"> </a>';
} else {
topbanner = '<div class="yoho-notice">' +
'<div class="notice-container center-content">' +
'<h1 class="notice-title">关于系统升级的公告</h1>' +
'<div class="notice-content">' +
'<p class="tips">尊敬的顾客:</p>' +
'<p class="detail">您好!为了向您提供更优质的服务,目前系统正在升级,请耐心等待。</p>' +
'<p class="detail">系统升级期间,部分地区用户体验会有暂时中断,如遇紧急事宜,欢迎垂询客服热线:' +
'400-889-9646 09:00-22:30(周一至周日)。稍后系统将恢复正常' +
'使用,欢迎您继续光顾YOHO!BUY有货!带来不便之处深表歉意,请您谅解!</p>' +
'</div>' +
'</div>' +
'</div>';
}
$('body').prepend(topbanner);
}
// cookie集合
function exeCookieMap() {
var cookies = document.cookie;
var cookiearr = cookies.split(';');
var i;
var temparr;
var key;
for (i = 0; i < cookiearr.length; i++) {
temparr = cookiearr[i].split('=');
if (typeof temparr[0] !== 'undefined') {
key = temparr[0].replace(/\s/g, '');
cookieMap[key] = temparr[1];
}
}
}
/**
* 获得banner & 异常通知
* @return {[type]} [description]
*/
function getBannerAndNotice() {
var INDEXKIDS = 'indexkids',
INDEXLIFESTYLE = 'indexlifestyle',
INDEXBOYS = 'indexboys',
INDEXWOMAN = 'indexgirls',
UNIQUEBRAND = 'uniquebrand';
var url = window.location.href;
var host = window.location.host;
var code = '';
var firstarea;
exeCookieMap();
if (url.indexOf('search') !== -1) {
code = bannerMap['search' + cookieMap._Channel];
}
if (url.indexOf('list') !== -1) {
code = bannerMap['list' + cookieMap._Channel];
}
if (url.indexOf('brands') !== -1) {
code = bannerMap['brands' + cookieMap._Channel];
}
if (url.indexOf('new') !== -1) {
code = bannerMap['new' + cookieMap._Channel];
}
if (url.indexOf('sale') !== -1) {
code = bannerMap['sale' + cookieMap._Channel];
}
if (url.indexOf('home') !== -1) {
code = bannerMap['home' + cookieMap._Channel];
}
if (url.indexOf('kids') !== -1) {
code = bannerMap[INDEXKIDS];
}
if (url.indexOf('woman') !== -1 || url.indexOf('girls') !== -1) {
code = bannerMap[INDEXWOMAN];
}
if (url.indexOf('lifestyle') !== -1) {
code = bannerMap[INDEXLIFESTYLE];
}
if (url.indexOf('www.yohobuy.com') !== -1 && window.location.pathname === '/') {
code = bannerMap[INDEXBOYS];
}
firstarea = host.split('.')[0];
if (firstarea !== 'list' && firstarea !== 'search' && firstarea !== 'www' &&
firstarea !== 'new' && firstarea !== 'item' && firstarea !== 'guang') {
code = bannerMap[UNIQUEBRAND + cookieMap._Channel];
}
$.getJSON('//new.yohobuy.com/common/getbanner?callback=?', {
client_type: 'web',
content_code: code
}, function(JsonData) {
if (+JsonData.code === 200) {
if (typeof JsonData.data === 'object') {
if (JsonData.data.url !== '') {
setTopBanner(JsonData.data);
} else {
setTopBanner();
}
}
}
});
}
// 格式化三级菜单
function formatThirdMenu() {
$subNav.each(function() {
var $thirdList = $(this).find('.hide-list'),
list = [],
i = 0;
if ($thirdList.length) {
$thirdList.children().each(function() {
if (i % thirdLineNum === 0) {
list.push('');
}
list[list.length - 1] += this.outerHTML + '';
i++;
});
for (i = 0; i < 3; i++) {
if (!list[i]) {
return;
}
$thirdList.before('<dl class="category-list">' + list[i] + '</dl>');
}
$thirdList.remove();
}
});
}
// 更新头部登陆信息
function updateLoginInfo(data) {
var info = {
usercenter: '//www.yohobuy.com/home?t=' + new Date().getTime(),
nickname: data.profileName,
signout: '//www.yohobuy.com/logout.html'
};
if (data.vip.curVipInfo.curLevel * 1 === 3) {
data.vip3 = true;
}
$tool.find('.simple-user-center').html(centerFn(data));
$loginBox.html(loginFn(info));
}
// 同步sso登录状态
function syncLoginInfo() {
var param = {
return_type: 'jsonp',
method: 'open.passport.get'
};
$.getJSON('//www.yohobuy.com/common/passport/?callback=?', param, function(jsonData) {
if (jsonData && jsonData.data && jsonData.data.result !== -1) {
updateLoginInfo(jsonData.data);
} else {
window.setCookie('_UID', '', {
domain: '.yohobuy.com',
expires: -1
});
}
$loginBox.show();
});
}
// 关键词搜索联想
function searchSuggest(key) {
var param = {
return_type: 'jsonp',
query: key
};
$.getJSON('//search.yohobuy.com/api/suggest?callback=?', param, function(jsonData) {
if (jsonData.code === 200) {
if (jsonData.data && jsonData.data.length) {
$searchSug.html(jsonData.data).show();
} else {
$searchSug.hide();
}
}
});
}
// 同步mini购物车数据
function syncCratInfo(strG) {
var info, total;
if (strG) {
window.setCookie('_g', strG, {
path: '/',
domain: '.yohobuy.com'
});
}
if (window.cookie('_g')) {
info = $.parseJSON(window.cookie('_g'));
total = parseInt(info._nac, 10) + parseInt(info._ac, 10);
total = total > 0 ? total : 0;
$goCart.data({
key: info._k,
num: total
});
$goodsNum.text(total);
}
}
function loadCartDetail(key) {
var param = {
return_type: 'jsonp',
method: 'open.Shoppingcart.getCartData',
shopping_key: key
};
$.getJSON('//www.yohobuy.com/common/shoppingCart/?callback=?', param, function(jsonData) {
var totalGoods = [],
infoArr = [
'main_goods',
'advance_goods',
'outlet_goods',
'gift_goods',
'need_pay_gifts'
],
data;
if (jsonData.code === 200) {
data = jsonData.data;
$.each(infoArr, function(k, v) {
if (data[v]) {
totalGoods = $.merge(totalGoods, data[v]);
}
});
if (totalGoods && totalGoods.length) {
data.totalGoods = totalGoods;
$miniCart.html(cartFn({
carData: data
}));
} else {
syncCratInfo('{"_k":"' + $goCart.data().key + '","_nac":0,"_ac":0,"_r":0}');
$miniCart.html('<div class="empty-cart"><h3>您的购物车暂无商品</h3></div>');
}
}
});
}
function delCartGoods(data, callback) {
var param = {
return_type: 'jsonp',
product_sku: data.sku,
promotion_id: data.proid,
product_num: data.num
};
$.getJSON('//www.yohobuy.com/common/delCartGoods/?callback=?', param, function(jsonData) {
var strG = '';
if (jsonData.code === 200) {
if (jsonData.data &&
typeof jsonData.data.total_goods_num !== 'undefined') {
strG = '{"_k":"' + data.key + '","_nac":' +
jsonData.data.total_goods_num + ',"_ac":0,"_r":0}';
syncCratInfo(strG);
if (jsonData.data.total_goods_num * 1 === 0) {
$miniCart.html('<div class="empty-cart"><h3>您的购物车暂无商品</h3></div>');
}
}
return callback();
}
});
}
/**
* css3动画
* @return {[type]} [description]
*/
function requestFrameAct() {
var prefixList = ['webkit', 'moz', 'ms'];
var func1 = prefixList[0] + 'RequestAnimationFrame';
var func2 = prefixList[1] + 'RequestAnimationFrame';
var func3 = prefixList[2] + 'RequestAnimationFrame';
if (window[func1]) {
return function(callback) {
requestAnimationFrame(callback);
};
}
if (window[func2]) {
return function(callback) {
window[func2](callback);
};
}
if (window[func3]) {
return function(callback) {
window[func3](callback);
};
}
return function(callback) {
window.setTimeout(callback, 67);
};
}
/**
* css3动画
* @return {[type]} [description]
*/
function tsAnimate() {
logoAngle += 10;
$logotrans.css({
transform: 'rotateX(' + logoAngle + 'deg)',
'-webkit-transform': 'rotateX(' + logoAngle + 'deg)',
'-moz-transform': 'rotateX(' + logoAngle + 'deg)'
});
if (logoAngle / 90 % 2 === 1) {
$logotrans.toggleClass('logo-cn');
}
if (logoAngle / 90 % 2 === 0 && logoAngle % 360 !== 0) {
window.setTimeout(tsAnimate, 3000);
} else {
if (logoAngle % 360 === 0) {
window.setTimeout(tsAnimate, 1 * 60 * 1000);
} else {
requestFrame(function() {
tsAnimate();
});
}
}
}
/**
* 淡出
* @return {[type]} [description]
*/
function fadeAnimate() {
var cycle = 3000;
if ($logotrans.hasClass('logo-cn-ie')) {
cycle = 1 * 60 * 1000;
}
$logotrans.fadeOut(loopTime, function() {
$logotrans.toggleClass('logo-cn-ie');
$logotrans.fadeIn(loopTime, function() {
window.setTimeout(function() {
fadeAnimate();
}, cycle);
});
});
}
/**
* 检测是否支持css3的动画
* @return {Boolean} [description]
*/
function isSupportCss3Animation() {
var thisFunc,
prefixList = ['webkit', 'moz', 'ms'],
i;
for (i = 0; i < prefixList.length; i++) {
thisFunc = prefixList[i] + 'RequestAnimationFrame';
if (window[thisFunc]) {
return true;
} else {
return false;
}
}
}
if (isSupportCss3Animation()) {
requestFrame = requestFrameAct();
window.setTimeout(tsAnimate, 3000);
} else {
window.setTimeout(fadeAnimate, 3000);
}
getBannerAndNotice(); // 获取头部banner
syncLoginInfo(); // 同步登陆信息
formatThirdMenu(); // 格式化三级菜单
setInterval(syncCratInfo, 2000); // 定时同步购物车数量
$yohoGroup.hover(function() {
var data = $(this).data();
$(this).text(data.cn);
}, function() {
var data = $(this).data();
$(this).text(data.en);
});
$searchForm.on('keyup', '.search-key', function(e) {
var val = $.trim($(this).val()),
$child = $searchSug.find('li'),
$act = $searchSug.find('.action'),
$focus;
if (e.which > 36 && e.which < 41) {
if (e.which === 38) {
$focus = $act.prev();
if (!$act.length || !$focus.length) {
$focus = $child.eq($child.length - 1);
}
} else if (e.which === 40) {
$focus = $act.next();
if (!$act.length || !$focus.length) {
$focus = $child.eq(0);
}
} else {
return;
}
$child.removeClass('action');
$focus.addClass('action');
$(this).val($focus.find('.searchvalue').text());
} else if (e.which === 13) {
if (val !== '') {
$searchForm.submit();
}
} else {
val = val.replace(new RegExp('\'', 'gm'), ''); // 去掉特殊字符
$(this).val(val);
searchSuggest(val);
}
}).on('blur', '.search-key', function() {
setTimeout(function() {
$searchSug.hide();
}, 200);
});
$goCart.hover(function() {
var data, _html = '';
if ($goCart.hasClass('on-hover')) {
return;
}
data = $goCart.data();
if (data && data.num * 1) {
_html = '<div class="loading-cart"><h3>加载中,请稍后</h3></div>';
loadCartDetail(data.key);
} else {
_html = '<div class="empty-cart"><h3>您的购物车暂无商品</h3></div>';
}
$miniCart.html(_html);
$goCart.addClass('on-hover');
}, function() {
$goCart.removeClass('on-hover');
});
$goCart.on('click', '.cart-goods-del', function() {
var $dom = $(this),
data = $dom.data(),
callback;
if (data) {
callback = function() {
$dom.closest('.goods-item').remove();
};
data.key = $goCart.data().key;
delCartGoods(data, callback);
}
});
$subNav.on({
mouseenter: function() {
var $thirdNav = $(this).children('.third-nav-wrapper'),
$show = $thirdNav.find('.show-detail'),
param = {};
delayer = setTimeout(function() {
$thirdNav.show();
}, 200);
if (!$show.length || $show.hasClass('show')) {
return;
}
param.content_code = $show.data().code;
param.client_type = 'web';
param.width = 337;
param.height = 250;
param._ = new Date();
$.getJSON('//new.yohobuy.com/common/getbanner?callback=?', param, function(JsonData) {
if (JsonData.code === 200) {
$show.addClass('show');
$show.find('img').attr('src', JsonData.data.src);
$show.find('a').attr('href', JsonData.data.url);
$show.find('.title').text(JsonData.data.title);
}
});
},
mouseleave: function() {
var $thirdNav = $(this).children('.third-nav-wrapper');
if (delayer) {
clearTimeout(delayer);
}
$thirdNav.hide();
}
});
/**
* 首次进入有弹窗
* @return {[type]} [description]
*/
function actionCover() {
var gender = window.cookie('_Gender');
var newMask = '';
var windowheight = '';
var selfheight = '';
var containertop;
var length = '';
if (window.location.href === 'http://www.yohobuy.com/' &&
(typeof gender === 'undefined' || gender === '' || gender === null)) {
$.get('/guide', function(data) {
newMask = document.createElement('div');
newMask.id = 'cover';
newMask.innerHTML = data;
document.body.appendChild(newMask);
windowheight = $(window).height();
selfheight = $('.guide-box').height();
containertop = windowheight / 2 - selfheight / 2;
length = $('.guide-box .clear').find('li').length;
$('.guide-box').css({
width: (200 * length) + 'px',
top: containertop + 'px'
});
$('#cover').bind('click', function() {
window.setCookie('_Gender', '1,3', {
path: '/',
domain: '.yohobuy.com',
expires: 90
});
window.setCookie('_Channel', 'boys', {
path: '/',
domain: '.yohobuy.com',
expires: 7
});
closeCover();
});
$('#cover .guide-box .close').bind('click', function() {
getSource('弹窗', 'CLOSE', 'homepage_man');
window.setCookie('_Gender', '1,3', {
path: '/',
domain: '.yohobuy.com',
expires: 90
});
window.setCookie('_Channel', 'boys', {
path: '/',
domain: '.yohobuy.com',
expires: 7
});
closeCover();
});
$('.boys img , .boys .go').bind('click', function() {
getSource('弹窗', 'BOYS', 'homepage_man');
window.setCookie('_Gender', '1,3', {
path: '/',
domain: '.yohobuy.com',
expires: 90
});
window.setCookie('_Channel', 'boys', {
path: '/',
domain: '.yohobuy.com',
expires: 7
});
closeCover();
});
$('.girls img, .girls .go').bind('click', function() {
getSource('弹窗', 'GIRLS', 'homepage_woman');
window.setCookie('_Gender', '2,3', {
path: '/',
domain: '.yohobuy.com',
expires: 90
});
window.setCookie('_Channel', 'girls', {
path: '/',
domain: '.yohobuy.com',
expires: 7
});
});
$('.lifestyle img, .lifestyle .go').bind('click', function() {
window.setCookie('_Channel', 'lifestyle', {
path: '/',
domain: '.yohobuy.com',
expires: 7
});
getSource('弹窗', 'LIEFSTYLE', 'homepage_lifestyle');
});
});
$('.lifestyle img, .lifestyle .go').bind('click', function() {
window.setCookie('_Channel', 'lifestyle', {
path: '/',
domain: '.yohobuy.com',
expires: 7
});
});
$('#cover .guide-box').bind('click', function(e) {
e.stopPropagation();
});
}
}
window.submitSearch = function() {
var $keywordinput = $('#query-key').val();
var $defaultsearch = $('#defaultsearch').val();
var column = 'Search';
var postition = 'Head Search';
var event = 'Search';
var $formatkeyword,
$formatkeywordinput;
$formatkeyword = $.trim(keyword.toLowerCase());
$formatkeywordinput = $.trim($keywordinput.toLowerCase());
getSource(column, postition, event);
if (link !== '' && keyword !== '' && $formatkeyword === $formatkeywordinput) {
location.href = link.replace('\'', '');
} else {
if (reg.test($keywordinput) && $keywordinput !== '') {
location.href = 'http://search.yohobuy.com/error?query=' + $keywordinput + '&result=error';
} else {
if ($keywordinput === '') {
$('#query-key').val($defaultsearch);
}
$searchForm.submit();
return false;
}
}
};
actionCover();