Authored by wangqing

增加购物车相关功能

... ... @@ -5,10 +5,14 @@
*/
var $ = require('jquery');
var $yohocookie = require('./common/yohocookie');
var $yohocart = require('./common/yohocart');
var yohodoT = require('../plugin/yohodoT');
var noticeSuccess = false;
/**
* 获取头部banner的回调函数
* @param {[type]} data [description]
*/
function JsonPCallBack(data) {
var topbanner;
... ... @@ -22,6 +26,10 @@ function JsonPCallBack(data) {
}
}
/**
* 获取头部服务器维护的回调
* @param {[type]} data [description]
*/
function NoticeCallBack(data) {
if (+data.code === 200) {
noticeSuccess = true;
... ... @@ -375,6 +383,62 @@ window.bannerMap = {
indexkids: '17f6d5d5d454d2c507bc5fcbc90f7756',
indexlifestyle: '735cd393e841762af8793c346abbbc36'
};
/**
* cookie 初始化
* @return {[type]} [description]
*/
function actionInitCookie() {
var $cookieGender = $.cookie('_Gender');
var $target = $('.cure');
if (typeof $cookieGender !== 'undefined' && $cookieGender !== '') {
$.setcookie('_Gender', 1, {
path: '/',
domain: '.yohobuy.com',
expires: 7
});
}
if ($target.find('.name-cn a').text() === '男生') {
$.setcookie('_Gender', '1,3', {
path: '/',
domain: '.yohobuy.com',
expires: 7
});
$.setcookie('_Channel', 'boys', {
path: '/',
domain: '.yohobuy.com',
expires: 7
});
}
if ($target.find('.name-cn a').text() === '女生') {
$.setcookie('_Gender', '2,3', {
path: '/',
domain: '.yohobuy.com',
expires: 7
});
$.setcookie('_Channel', 'girls', {
path: '/',
domain: '.yohobuy.com',
expires: 7
});
}
if ($target.find('.name-cn a').text() === '创意生活') {
$.setcookie('_Channel', 'lifestyle', {
path: '/',
domain: '.yohobuy.com',
expires: 7
});
}
if ($target.find('.name-cn a').text() === '潮童') {
$.setcookie('_Channel', 'kids', {
path: '/',
domain: '.yohobuy.com',
expires: 7
});
}
}
/**
* cookie集合
* @type {Object}
... ... @@ -394,9 +458,207 @@ function actionExeCookieMap() {
window.cookieMap[key] = temparr[1];
}
}
/**
* 一级菜单点击(会设置cookie)
* @return {[type]} [description]
*/
function actionFirstMenuClick() {
var $cookieGender;
$('.first-nav-item').click(function() {
$cookieGender = $.cookie('_Gender');
if (typeof $cookieGender !== 'undefined' && $cookieGender !== '') {
$.setcookie('_Gender', 1, {
path: '/',
domain: '.yohobuy.com',
expires: 7
});
}
if ($(this).find('.name-cn a').text() === '男生') {
$.setcookie('_Gender', '1,3', {
path: '/',
domain: '.yohobuy.com',
expires: 7
});
$.setcookie('_Channel', 'boys', {
path: '/',
domain: '.yohobuy.com',
expires: 7
});
}
if ($(this).find('.name-cn a').text() === '女生') {
$.setcookie('_Gender', '2,3', {
path: '/',
domain: '.yohobuy.com',
expires: 7
});
$.setcookie('_Channel', 'girls', {
path: '/',
domain: '.yohobuy.com',
expires: 7
});
}
if ($(this).find('.name-cn a').text() === '创意生活') {
$.setcookie('_Channel', 'lifestyle', {
path: '/',
domain: '.yohobuy.com',
expires: 7
});
}
if ($(this).find('.name-cn a').text() === '潮童') {
$.setcookie('_Channel', 'kids', {
path: '/',
domain: '.yohobuy.com',
expires: 7
});
}
});
$('#backToOld').click(function() {
$.setcookie('_New', 1, {
path: '/',
domain: '.yohobuy.com'
});
location.href = 'http://www.yohobuy.com';
});
}
/**
* 购物车商品数量
* @return {[type]} [description]
*/
function actionUpdateCartNum() {
$('#miniCartBox').miniCart({
cartNum: '.ic-infomation'
});
}
/**
* 加载购物车数据
* @return {[type]} [description]
*/
function loadCartData() {
var shoppingInfo,
shoppingData,
strK;
$.getData(window.apiDomain, {
method: 'open.Shoppingcart.getCartData',
shopping_key: $.getShoppingKey()
}, function(jsonData) {
if (window.cartTpl === '') {
window.cartTpl = yohodoT.template($('#mini-cart-tpl').html());
}
$('#miniCartBox').children('.gobuy-wrapper').html(window.cartTpl({
cartData: jsonData
}));
if (parseInt($('.ic-infomation').html()) === 0) {
$('#miniCartBox .gobuy-wrapper').html('<div class="gobuy-empty">' +
'<h3 class="information">您的购物车暂无商品</h3></div>');
}
if ($('#miniCartBox .goods-list li').length < 1) {
shoppingInfo = $.cookie('_g');
shoppingData = eval('(' + shoppingInfo + ')');
if (shoppingData !== null) {
strK = '{"_k":"' + shoppingData._k + '","_nac":0' + ',"_ac":0,"_r":0}';
$.setcookie('_g', strK, {
path: '/',
domain: '.yohobuy.com'
});
}
$('.ic-infomation').html(0);
$('#miniCartBox .gobuy-wrapper').html('<div class="gobuy-empty">' +
'<h3 class="information">您的购物车暂无商品</h3></div>');
}
});
}
/**
* 删除购物车数据
* @param {[type]} id [description]
* @param {Boolean} isreduce [description]
* @return {[type]} [description]
*/
function delCartGoods(id, isreduce) {
$.getData(window.apiDomain, {
method: 'open.Shoppingcart.delone',
shopping_key: $.getShoppingKey(),
id: id,
isreduce: isreduce
}, function(jsonData) {
var shoppingInfo = $.cookie('_g');
var shoppingData = eval('(' + shoppingInfo + ')');
var strK = '{"_k":"' + shoppingData._k + '","_nac":' + jsonData.total_goods_num + ',"_ac":0,"_r":0}';
$.setcookie('_g', strK, {
path: '/',
domain: '.yohobuy.com'
});
loadCartData();
actionUpdateCartNum();
});
}
window.apiDomain = 'http://api.open.yohobuy.com';
window.cartTpl = '';
/**
* 点击购物车
* @return {[type]} [description]
*/
function clickMiniCartBox() {
var $target;
var shopcarurl = 'http://www.yohobuy.com/shopping/cart';
$('#miniCartBox').on('click', function(e) {
$target = $(e.target);
if ($target.hasClass('gobuy')) {
window.location.href = shopcarurl;
}
});
}
/**
* 显示购物车效果
* @param {Boolean} isShow [description]
* @return {[type]} [description]
*/
function showMiniCart(isShow) {
if (isShow === 1) {
$('#miniCartBox').addClass('list-cur');
$('.gobuy-wrapper').show();
if (parseInt($('.ic-infomation').html()) !== 0) {
$('#miniCartBox .gobuy-wrapper').html('<div class="gobuy-loading">' +
'<h3 class="information">加载中,请稍后</h3></div>');
loadCartData();
} else {
$('#miniCartBox .gobuy-wrapper').html('<div class="gobuy-empty">' +
'<h3 class="information">您的购物车暂无商品</h3></div>');
}
} else {
$('#miniCartBox').removeClass('list-cur');
$('.gobuy-wrapper').hide();
}
}
/**
* 鼠标购物车的滑入滑出效果
* @return {[type]} [description]
*/
function actionGoodsCarMouseEffect() {
var $target;
$('#miniCartBox').mouseenter(function(e) {
$target = $(e.target);
if ($target.attr('id') === 'miniCartBox' || $target.attr('class') === 'ic-infomation') {
showMiniCart(1);
}
});
$('#miniCartBox').mouseleave(function(e) {
showMiniCart(0);
});
}
/**
* 获得banner & 异常通知
* @return {[type]} [description]
*/
... ... @@ -496,15 +758,47 @@ function actionBrandChange() {
}
}
/**
* 循环检测购物车数量
* @return {[type]} [description]
*/
function actionLoopUpdCartNum() {
setInterval(actionUpdateCartNum, 2000);
}
/**
* 处理模板
* @return {[type]} [description]
*/
function actionExeTemplate() {
var resulthtml = $('#goodcartempwarpper').html().replace(/\%L/g, '{{').replace(/\%R/g, '}}');
$('#goodcartempwarpper').html(resulthtml);
}
/**
* 执行搜索
* @return {[type]} [description]
*/
function actionSearch() {
var searchDomain = 'http://search.yohobuy.com/api/suggest';
$('#query_key').search(searchDomain);
}
/**
* 初始化函数
* @return {[type]} [description]
*/
exports.init = function() {
actionExeCookieMap();
actionYoHoGroup();
actionTopTagToggle();
actionTopLogoAnimate();
actionGetBannerAndNotice();
actionBrandChange();
actionAddKeyWords();
actionExeTemplate(); //处理模板
actionInitCookie(); //初始化cookie
actionExeCookieMap(); //格式化cookie
actionYoHoGroup(); // yoho集团鼠标效果
actionTopTagToggle(); // yoho上部分开关
actionTopLogoAnimate(); //yoho logo动画
actionGetBannerAndNotice(); // 获取banner和服务器维护提示
actionBrandChange(); //切换品牌
actionAddKeyWords(); //跳转后增加关键字
actionFirstMenuClick(); //一级菜单点击(会设置cookie)
actionGoodsCarMouseEffect(); //鼠标滑入滑出效果
actionUpdateCartNum(); //更新购物车数量
actionLoopUpdCartNum(); //循环检测购物车数量
actionSearch(); //搜索执行
};
\ No newline at end of file
... ...
// yohodoT.js
// 2011-2014, Laura Doktorova, https://github.com/olado/yohodoT
// Licensed under the MIT license.
"use strict";
var yohodoT = {
version: "1.0.3",
templateSettings: {
evaluate: /\{\{([\s\S]+?(\}?)+)\}\}/g,
interpolate: /\{\{=([\s\S]+?)\}\}/g,
encode: /\{\{!([\s\S]+?)\}\}/g,
use: /\{\{#([\s\S]+?)\}\}/g,
useParams: /(^|[^\w$])def(?:\.|\[[\'\"])([\w$\.]+)(?:[\'\"]\])?\s*\:\s*([\w$\.]+|\"[^\"]+\"|\'[^\']+\'|\{[^\}]+\})/g,
define: /\{\{##\s*([\w\.$]+)\s*(\:|=)([\s\S]+?)#\}\}/g,
defineParams: /^\s*([\w$]+):([\s\S]+)/,
conditional: /\{\{\?(\?)?\s*([\s\S]*?)\s*\}\}/g,
iterate: /\{\{~\s*(?:\}\}|([\s\S]+?)\s*\:\s*([\w$]+)\s*(?:\:\s*([\w$]+))?\s*\}\})/g,
varname: "it",
strip: true,
append: true,
selfcontained: false,
doNotSkipEncoded: false
},
template: undefined, //fn, compile template
compile: undefined //fn, for express
},
_globals;
yohodoT.encodeHTMLSource = function(doNotSkipEncoded) {
var encodeHTMLRules = {
"&": "&#38;",
"<": "&#60;",
">": "&#62;",
'"': "&#34;",
"'": "&#39;",
"/": "&#47;"
},
matchHTML = doNotSkipEncoded ? /[&<>"'\/]/g : /&(?!#?\w+;)|<|>|"|'|\//g;
return function(code) {
return code ? code.toString().replace(matchHTML, function(m) {
return encodeHTMLRules[m] || m;
}) : "";
};
};
_globals = (function() {
return this || (0, eval)("this");
}());
if (typeof module !== "undefined" && module.exports) {
module.exports = yohodoT;
} else if (typeof define === "function" && define.amd) {
define(function() {
return yohodoT;
});
} else {
_globals.yohodoT = yohodoT;
}
var startend = {
append: {
start: "'+(",
end: ")+'",
startencode: "'+encodeHTML("
},
split: {
start: "';out+=(",
end: ");out+='",
startencode: "';out+=encodeHTML("
}
},
skip = /$^/;
function resolveDefs(c, block, def) {
return ((typeof block === "string") ? block : block.toString())
.replace(c.define || skip, function(m, code, assign, value) {
if (code.indexOf("def.") === 0) {
code = code.substring(4);
}
if (!(code in def)) {
if (assign === ":") {
if (c.defineParams) value.replace(c.defineParams, function(m, param, v) {
def[code] = {
arg: param,
text: v
};
});
if (!(code in def)) def[code] = value;
} else {
new Function("def", "def['" + code + "']=" + value)(def);
}
}
return "";
})
.replace(c.use || skip, function(m, code) {
if (c.useParams) code = code.replace(c.useParams, function(m, s, d, param) {
if (def[d] && def[d].arg && param) {
var rw = (d + ":" + param).replace(/'|\\/g, "_");
def.__exp = def.__exp || {};
def.__exp[rw] = def[d].text.replace(new RegExp("(^|[^\\w$])" + def[d].arg + "([^\\w$])", "g"), "$1" + param + "$2");
return s + "def.__exp['" + rw + "']";
}
});
var v = new Function("def", "return " + code)(def);
return v ? resolveDefs(c, v, def) : v;
});
}
function unescape(code) {
return code.replace(/\\('|\\)/g, "$1").replace(/[\r\t\n]/g, " ");
}
yohodoT.template = function(tmpl, c, def) {
c = c || yohodoT.templateSettings;
var cse = c.append ? startend.append : startend.split,
needhtmlencode, sid = 0,
indv,
str = (c.use || c.define) ? resolveDefs(c, tmpl, def || {}) : tmpl;
str = ("var out='" + (c.strip ? str.replace(/(^|\r|\n)\t* +| +\t*(\r|\n|$)/g, " ")
.replace(/\r|\n|\t|\/\*[\s\S]*?\*\//g, "") : str)
.replace(/'|\\/g, "\\$&")
.replace(c.interpolate || skip, function(m, code) {
return cse.start + unescape(code) + cse.end;
})
.replace(c.encode || skip, function(m, code) {
needhtmlencode = true;
return cse.startencode + unescape(code) + cse.end;
})
.replace(c.conditional || skip, function(m, elsecase, code) {
return elsecase ?
(code ? "';}else if(" + unescape(code) + "){out+='" : "';}else{out+='") :
(code ? "';if(" + unescape(code) + "){out+='" : "';}out+='");
})
.replace(c.iterate || skip, function(m, iterate, vname, iname) {
if (!iterate) return "';} } out+='";
sid += 1;
indv = iname || "i" + sid;
iterate = unescape(iterate);
return "';var arr" + sid + "=" + iterate + ";if(arr" + sid + "){var " + vname + "," + indv + "=-1,l" + sid + "=arr" + sid + ".length-1;while(" + indv + "<l" + sid + "){" + vname + "=arr" + sid + "[" + indv + "+=1];out+='";
})
.replace(c.evaluate || skip, function(m, code) {
return "';" + unescape(code) + "out+='";
}) + "';return out;")
.replace(/\n/g, "\\n").replace(/\t/g, '\\t').replace(/\r/g, "\\r")
.replace(/(\s|;|\}|^|\{)out\+='';/g, '$1').replace(/\+''/g, "");
//.replace(/(\s|;|\}|^|\{)out\+=''\+/g,'$1out+=');
if (needhtmlencode) {
if (!c.selfcontained && _globals && !_globals._encodeHTML) _globals._encodeHTML = yohodoT.encodeHTMLSource(c.doNotSkipEncoded);
str = "var encodeHTML = typeof _encodeHTML !== 'undefined' ? _encodeHTML : (" + yohodoT.encodeHTMLSource.toString() + "(" + (c.doNotSkipEncoded || '') + "));" + str;
}
try {
return new Function(c.varname, str);
} catch (e) {
if (typeof console !== "undefined") console.log("Could not create a template function: " + str);
throw e;
}
};
yohodoT.compile = function(tmpl, def) {
return yohodoT.template(tmpl, null, def);
};
module.exports = yohodoT;
\ No newline at end of file
... ...
... ... @@ -761,11 +761,13 @@
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
font-size: 12px;
}
.valuenum {
position: absolute;
right: 10px;
font-size: 12px;
}
.header-tool {
padding-right: 23px;
... ...