/** * 页面公共逻辑 * @author: xuqi<qi.xu@yoho.cn> * @date: 2015/10/21 */ var $ = require('yoho-jquery'); var $footer = $('#yoho-footer'), $yohoPage = $('.yoho-page'), $header = $('.yoho-header'); // 为您优选-40位随机数指纹请求id var RECID = (new Date().getTime() + '_H5_YOHOBUY_' + Math.floor(Math.random() * 1000000 + 1000000) + '_' + Math.floor(Math.random() * 1000000 + 1000000)); var _ChannelVary = { boys: 1, girls: 2, kids: 3, lifestyle: 4 }; // 尝试打开 APP require('./common/open-app'); function cookie(name) { var cookies = document.cookie, cookieVal; if (cookies) { cookies = cookies.split(';'); cookies.forEach(function(c) { if (c.indexOf(name) > -1) { cookieVal = decodeURIComponent($.trim(c.replace(name + '=', ''))); return; } }); } return cookieVal; } function setCookie(name, value, options) { var expires = '', path, domain, secure, date; if (typeof value !== 'undefined') { options = options || {}; if (value === null) { value = ''; options.expires = -1; } if (options.expires && (typeof options.expires === 'number' || options.expires.toUTCString)) { if (typeof options.expires === 'number') { date = new Date(); date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000)); } else { date = options.expires; } expires = '; expires=' + date.toUTCString(); } path = options.path ? '; path=' + options.path : ''; domain = options.domain ? '; domain=' + options.domain : ''; secure = options.secure ? '; secure' : ''; document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join(''); } } function isCookiesEnabled() { var cookieEnabled = (navigator.cookieEnabled) ? true : false; if (typeof navigator.cookieEnabled === 'undefined' && !cookieEnabled) { document.cookie = 'testcookie'; cookieEnabled = (document.cookie.indexOf('testcookie') !== -1) ? true : false; } return (cookieEnabled); } function getUser() { var c = cookie('_UID'), user; if (typeof c === 'undefined') { return 0; } user = c.split('::'); if (typeof user === 'undefined' || user.length < 4) { return 0; } return user; } function getUid() { var user = getUser(); if (user === 0) { return 0; } return user[1]; } function getShoppingKey() { var c = cookie('_g'); if (typeof c === 'undefined') { return ''; } return JSON.parse(c).k; } // 根据页面内容重新设置通用底部的显示 function rePosFooter() { var winH; if ($footer.length === 0) { return; } $footer.addClass('bottom'); if($footer.css('position') === 'static') { $footer.css('position', ''); } winH = Math.min($(window).height(), window.screen.availHeight); if ($('body').height() >= winH - parseInt($footer.css('height'), 0)) { $footer.removeClass('bottom'); } $footer.show(); } /** * * add extra margin-bottom for footer to show yoho copyright when there is fixed bottom element on page * @param {String} The jquery selecor of the fixed bottom element * @return undefined */ function reMarginFooter(fixedElement) { var fixedElH = $(fixedElement).outerHeight(); if (fixedElement) { $footer.css('margin-bottom', fixedElH + 'px'); } } function queryString() { var vars = [], hash, i; var hashes = window.location.search.slice(1).split('&'); for (i = 0; i < hashes.length; i++) { hash = hashes[i].split('='); vars.push(hash[0]); vars[hash[0]] = hash[1]; } return vars; } // 给jQuery 扩展 queryString函数 $.extend({ queryString: queryString }); // 页面通用底部位置及status设置 (function() { var $op = $footer.children('.op-row'), $backToTop = $('.back-to-top'), $floatTop = $('.float-top'); var user = getUser(); var backToTopHammer; var floatTopHammer; setTimeout(function() { rePosFooter(); // 计算底部位置 }, 500); if (user === 0) { // 未登录 $op.prepend( '<a href="/signin.html?refer=' + location.href + '">登录</a>' + '<span class="sep-line">|</span>' + '<a href="/reg.html">注册</a>' ); } else { // 已登录 $op.prepend( '<span>Hi,</span>' + '<a class="user-name" href="/home?tmp=' + Math.random() + '">' + user[0] + '</a>' + '<a href="/passport/signout/index">退出</a>' ); } if ($backToTop.length > 0) { $backToTop.on('touchend touchcancel', function(e) { $(window).scrollTop(0); e.preventDefault(); return false; }); } if ($floatTop.length > 0) { $floatTop.on('click', function(e) { $(window).scrollTop(0); e.preventDefault(); return false; }); } $footer.removeClass('hide'); // 单击下载按钮 - 接受微信商城或者第三方来源的数据埋点信息 if (queryString().union_type) { setCookie('unionTypeYas', queryString().union_type, { path: '/' }); } }()); $header.on('touchstart', 'a', function() { $header.find('a').removeClass('highlight'); $(this).addClass('highlight'); }).on('touchend touchcancel', 'a', function() { $(this).removeClass('highlight'); }); $yohoPage.on('touchstart', '.tap-hightlight', function() { $(this).siblings('.tap-hightlight').removeClass('highlight'); $(this).addClass('highlight'); }).on('touchend touchcancel', '.tap-hightlight', function() { $(this).removeClass('highlight'); }); $('.nav-home').on('touchstart', function() { $('.homebuttom').toggleClass('hide'); }); (function() { var lastTime = 0, prefixes = 'webkit moz ms o'.split(' '), requestAnimationFrame = window.requestAnimationFrame, cancelAnimationFrame = window.cancelAnimationFrame, prefix, i; // 通过遍历各浏览器前缀,来得到requestAnimationFrame和cancelAnimationFrame在当前浏览器的实现形式 for (i = 0; i < prefixes.length; i++) { if (requestAnimationFrame && cancelAnimationFrame) { break; } prefix = prefixes[i]; requestAnimationFrame = requestAnimationFrame || window[prefix + 'RequestAnimationFrame']; cancelAnimationFrame = cancelAnimationFrame || window[prefix + 'CancelAnimationFrame'] || window[prefix + 'CancelRequestAnimationFrame']; } // 如果当前浏览器不支持requestAnimationFrame和cancelAnimationFrame,则会退到setTimeout if (!requestAnimationFrame || !cancelAnimationFrame) { requestAnimationFrame = function(callback) { var currTime = new Date().getTime(); // 为了使setTimteout的尽可能的接近每秒60帧的效果 var timeToCall = Math.max(0, 16 - (currTime - lastTime)); var id = window.setTimeout(function() { callback(currTime + timeToCall); }, timeToCall); lastTime = currTime + timeToCall; return id; }; cancelAnimationFrame = function(id) { window.clearTimeout(id); }; } window.requestAnimationFrame = requestAnimationFrame; window.cancelAnimationFrame = cancelAnimationFrame; }()); // 临时修改 footer function phoneHidden(phone) { return phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2'); } if ($footer.find('.user-name').text().length === 11) { $footer.find('.user-name').html(phoneHidden($footer.find('.user-name').text())); } // 浮层下载APP function downLoadApp(unionType) { var appUrl = 'http://union.yoho.cn/union/downapp.html'; var clickedAt = new Date(); setTimeout(function() { var mkt = unionType || queryString().union_type || cookie('unionTypeYas') || false; if ((new Date()) - clickedAt < 2000) { if (mkt) { appUrl += '?union_type=' + mkt; } if (window._yas && window._yas.sendCustomInfo) { window._yas.sendCustomInfo({ op: 'YB_DOWNLOAD_C', param: JSON.stringify({ C_ID: _ChannelVary[cookie('_Channel')] || 1, TO_URL: appUrl }) }, true); } window.location = appUrl; } }, 500); } // 为您优选埋点 http://redmine.yoho.cn/issues/10117 function givePoint(parameter) { if (!window._yas || !window._yas.sendCustomInfo) { return false; } if (!parameter.REC_POSE || !parameter.PRD_ID) { return true; } // 男:1,女:2,潮童:3,创意生活:4 var CID = _ChannelVary[cookie('_Channel')] || 1; var isAppOp = parameter.isAppOp; parameter = $.extend({ REC_ID: RECID, PRD_NUM: 0, C_ID: CID, ACTION_ID: 0, PAGE_NUM: 1 }, parameter); delete parameter.isAppOp; window._yas.sendCustomInfo({ op: 'YB_CHOOSE_FOR_YOU_Y', appop: isAppOp ? 'YB_CHOOSE_FOR_YOU' : '', // app内打开wap的埋点场合 param: JSON.stringify(parameter) }, true); } /** * 页面头颜色修正 */ (function() { var channel = cookie('_Channel'); var header = $('#yoho-header, .homebuttom'); var footer = $('.footer-tab .tab-item.current'); if (!/^\/home/.test(location.pathname)) { switch (channel) { case 'boys': if (!header.hasClass('boys')) { header.addClass('boys'); } break; case 'girls': if (!header.hasClass('girls')) { header.addClass('girls'); } break; case 'lifestyle': if (!header.hasClass('life-style')) { header.addClass('life-style'); } break; case 'kids': if (!header.hasClass('kids')) { header.addClass('kids'); } break; default: if (!header.hasClass('boys')) { header.addClass('boys'); } break; } } else { header.removeClass('kids'); header.removeClass('girls'); header.removeClass('life-style'); header.addClass('boys'); } switch (channel) { case 'boys': if (footer) { footer.addClass('boys'); } break; case 'girls': if (footer) { footer.addClass('girls'); } break; case 'lifestyle': if (footer) { footer.addClass('life-style'); } break; case 'kids': if (footer) { footer.addClass('kids'); } break; default: if (footer) { footer.addClass('boys'); } break; } }()); // 暴露公共接口 window.cookie = cookie; window.setCookie = setCookie; window.isCookiesEnabled = isCookiesEnabled; window.getUser = getUser; window.getUid = getUid; window.getShoppingKey = getShoppingKey; window.rePosFooter = rePosFooter; window.reMarginFooter = reMarginFooter; window.queryString = queryString(); window.givePoint = givePoint; window._ChannelVary = _ChannelVary; window.downLoadApp = downLoadApp;