Authored by wangqing

增加购物车相关功能

@@ -5,10 +5,14 @@ @@ -5,10 +5,14 @@
5 */ 5 */
6 6
7 var $ = require('jquery'); 7 var $ = require('jquery');
8 -  
9 - 8 +var $yohocookie = require('./common/yohocookie');
  9 +var $yohocart = require('./common/yohocart');
  10 +var yohodoT = require('../plugin/yohodoT');
10 var noticeSuccess = false; 11 var noticeSuccess = false;
11 - 12 +/**
  13 + * 获取头部banner的回调函数
  14 + * @param {[type]} data [description]
  15 + */
12 function JsonPCallBack(data) { 16 function JsonPCallBack(data) {
13 var topbanner; 17 var topbanner;
14 18
@@ -22,6 +26,10 @@ function JsonPCallBack(data) { @@ -22,6 +26,10 @@ function JsonPCallBack(data) {
22 } 26 }
23 } 27 }
24 28
  29 +/**
  30 + * 获取头部服务器维护的回调
  31 + * @param {[type]} data [description]
  32 + */
25 function NoticeCallBack(data) { 33 function NoticeCallBack(data) {
26 if (+data.code === 200) { 34 if (+data.code === 200) {
27 noticeSuccess = true; 35 noticeSuccess = true;
@@ -375,6 +383,62 @@ window.bannerMap = { @@ -375,6 +383,62 @@ window.bannerMap = {
375 indexkids: '17f6d5d5d454d2c507bc5fcbc90f7756', 383 indexkids: '17f6d5d5d454d2c507bc5fcbc90f7756',
376 indexlifestyle: '735cd393e841762af8793c346abbbc36' 384 indexlifestyle: '735cd393e841762af8793c346abbbc36'
377 }; 385 };
  386 +
  387 +/**
  388 + * cookie 初始化
  389 + * @return {[type]} [description]
  390 + */
  391 +function actionInitCookie() {
  392 + var $cookieGender = $.cookie('_Gender');
  393 + var $target = $('.cure');
  394 +
  395 + if (typeof $cookieGender !== 'undefined' && $cookieGender !== '') {
  396 + $.setcookie('_Gender', 1, {
  397 + path: '/',
  398 + domain: '.yohobuy.com',
  399 + expires: 7
  400 + });
  401 + }
  402 + if ($target.find('.name-cn a').text() === '男生') {
  403 + $.setcookie('_Gender', '1,3', {
  404 + path: '/',
  405 + domain: '.yohobuy.com',
  406 + expires: 7
  407 + });
  408 + $.setcookie('_Channel', 'boys', {
  409 + path: '/',
  410 + domain: '.yohobuy.com',
  411 + expires: 7
  412 + });
  413 + }
  414 + if ($target.find('.name-cn a').text() === '女生') {
  415 + $.setcookie('_Gender', '2,3', {
  416 + path: '/',
  417 + domain: '.yohobuy.com',
  418 + expires: 7
  419 + });
  420 + $.setcookie('_Channel', 'girls', {
  421 + path: '/',
  422 + domain: '.yohobuy.com',
  423 + expires: 7
  424 + });
  425 + }
  426 + if ($target.find('.name-cn a').text() === '创意生活') {
  427 + $.setcookie('_Channel', 'lifestyle', {
  428 + path: '/',
  429 + domain: '.yohobuy.com',
  430 + expires: 7
  431 + });
  432 + }
  433 + if ($target.find('.name-cn a').text() === '潮童') {
  434 + $.setcookie('_Channel', 'kids', {
  435 + path: '/',
  436 + domain: '.yohobuy.com',
  437 + expires: 7
  438 + });
  439 + }
  440 +}
  441 +
378 /** 442 /**
379 * cookie集合 443 * cookie集合
380 * @type {Object} 444 * @type {Object}
@@ -394,9 +458,207 @@ function actionExeCookieMap() { @@ -394,9 +458,207 @@ function actionExeCookieMap() {
394 window.cookieMap[key] = temparr[1]; 458 window.cookieMap[key] = temparr[1];
395 } 459 }
396 } 460 }
  461 +/**
  462 + * 一级菜单点击(会设置cookie)
  463 + * @return {[type]} [description]
  464 + */
  465 +function actionFirstMenuClick() {
  466 + var $cookieGender;
  467 +
  468 + $('.first-nav-item').click(function() {
  469 + $cookieGender = $.cookie('_Gender');
  470 + if (typeof $cookieGender !== 'undefined' && $cookieGender !== '') {
  471 + $.setcookie('_Gender', 1, {
  472 + path: '/',
  473 + domain: '.yohobuy.com',
  474 + expires: 7
  475 + });
  476 + }
  477 + if ($(this).find('.name-cn a').text() === '男生') {
  478 + $.setcookie('_Gender', '1,3', {
  479 + path: '/',
  480 + domain: '.yohobuy.com',
  481 + expires: 7
  482 + });
  483 + $.setcookie('_Channel', 'boys', {
  484 + path: '/',
  485 + domain: '.yohobuy.com',
  486 + expires: 7
  487 + });
  488 + }
  489 + if ($(this).find('.name-cn a').text() === '女生') {
  490 + $.setcookie('_Gender', '2,3', {
  491 + path: '/',
  492 + domain: '.yohobuy.com',
  493 + expires: 7
  494 + });
  495 + $.setcookie('_Channel', 'girls', {
  496 + path: '/',
  497 + domain: '.yohobuy.com',
  498 + expires: 7
  499 + });
  500 + }
  501 + if ($(this).find('.name-cn a').text() === '创意生活') {
  502 + $.setcookie('_Channel', 'lifestyle', {
  503 + path: '/',
  504 + domain: '.yohobuy.com',
  505 + expires: 7
  506 + });
  507 + }
  508 + if ($(this).find('.name-cn a').text() === '潮童') {
  509 + $.setcookie('_Channel', 'kids', {
  510 + path: '/',
  511 + domain: '.yohobuy.com',
  512 + expires: 7
  513 + });
  514 + }
  515 + });
  516 +
  517 + $('#backToOld').click(function() {
  518 + $.setcookie('_New', 1, {
  519 + path: '/',
  520 + domain: '.yohobuy.com'
  521 + });
  522 + location.href = 'http://www.yohobuy.com';
  523 + });
  524 +}
397 525
398 526
399 /** 527 /**
  528 + * 购物车商品数量
  529 + * @return {[type]} [description]
  530 + */
  531 +function actionUpdateCartNum() {
  532 + $('#miniCartBox').miniCart({
  533 + cartNum: '.ic-infomation'
  534 + });
  535 +}
  536 +/**
  537 + * 加载购物车数据
  538 + * @return {[type]} [description]
  539 + */
  540 +function loadCartData() {
  541 + var shoppingInfo,
  542 + shoppingData,
  543 + strK;
  544 +
  545 + $.getData(window.apiDomain, {
  546 + method: 'open.Shoppingcart.getCartData',
  547 + shopping_key: $.getShoppingKey()
  548 + }, function(jsonData) {
  549 + if (window.cartTpl === '') {
  550 + window.cartTpl = yohodoT.template($('#mini-cart-tpl').html());
  551 + }
  552 + $('#miniCartBox').children('.gobuy-wrapper').html(window.cartTpl({
  553 + cartData: jsonData
  554 + }));
  555 + if (parseInt($('.ic-infomation').html()) === 0) {
  556 + $('#miniCartBox .gobuy-wrapper').html('<div class="gobuy-empty">' +
  557 + '<h3 class="information">您的购物车暂无商品</h3></div>');
  558 + }
  559 + if ($('#miniCartBox .goods-list li').length < 1) {
  560 + shoppingInfo = $.cookie('_g');
  561 + shoppingData = eval('(' + shoppingInfo + ')');
  562 + if (shoppingData !== null) {
  563 + strK = '{"_k":"' + shoppingData._k + '","_nac":0' + ',"_ac":0,"_r":0}';
  564 + $.setcookie('_g', strK, {
  565 + path: '/',
  566 + domain: '.yohobuy.com'
  567 + });
  568 + }
  569 + $('.ic-infomation').html(0);
  570 + $('#miniCartBox .gobuy-wrapper').html('<div class="gobuy-empty">' +
  571 + '<h3 class="information">您的购物车暂无商品</h3></div>');
  572 + }
  573 + });
  574 +}
  575 +/**
  576 + * 删除购物车数据
  577 + * @param {[type]} id [description]
  578 + * @param {Boolean} isreduce [description]
  579 + * @return {[type]} [description]
  580 + */
  581 +function delCartGoods(id, isreduce) {
  582 + $.getData(window.apiDomain, {
  583 + method: 'open.Shoppingcart.delone',
  584 + shopping_key: $.getShoppingKey(),
  585 + id: id,
  586 + isreduce: isreduce
  587 + }, function(jsonData) {
  588 + var shoppingInfo = $.cookie('_g');
  589 + var shoppingData = eval('(' + shoppingInfo + ')');
  590 + var strK = '{"_k":"' + shoppingData._k + '","_nac":' + jsonData.total_goods_num + ',"_ac":0,"_r":0}';
  591 +
  592 + $.setcookie('_g', strK, {
  593 + path: '/',
  594 + domain: '.yohobuy.com'
  595 + });
  596 + loadCartData();
  597 + actionUpdateCartNum();
  598 + });
  599 +}
  600 +window.apiDomain = 'http://api.open.yohobuy.com';
  601 +window.cartTpl = '';
  602 +
  603 +
  604 +/**
  605 + * 点击购物车
  606 + * @return {[type]} [description]
  607 + */
  608 +function clickMiniCartBox() {
  609 + var $target;
  610 + var shopcarurl = 'http://www.yohobuy.com/shopping/cart';
  611 +
  612 + $('#miniCartBox').on('click', function(e) {
  613 + $target = $(e.target);
  614 + if ($target.hasClass('gobuy')) {
  615 + window.location.href = shopcarurl;
  616 + }
  617 + });
  618 +}
  619 +
  620 +/**
  621 + * 显示购物车效果
  622 + * @param {Boolean} isShow [description]
  623 + * @return {[type]} [description]
  624 + */
  625 +function showMiniCart(isShow) {
  626 + if (isShow === 1) {
  627 + $('#miniCartBox').addClass('list-cur');
  628 + $('.gobuy-wrapper').show();
  629 + if (parseInt($('.ic-infomation').html()) !== 0) {
  630 + $('#miniCartBox .gobuy-wrapper').html('<div class="gobuy-loading">' +
  631 + '<h3 class="information">加载中,请稍后</h3></div>');
  632 + loadCartData();
  633 + } else {
  634 + $('#miniCartBox .gobuy-wrapper').html('<div class="gobuy-empty">' +
  635 + '<h3 class="information">您的购物车暂无商品</h3></div>');
  636 + }
  637 + } else {
  638 + $('#miniCartBox').removeClass('list-cur');
  639 + $('.gobuy-wrapper').hide();
  640 + }
  641 +}
  642 +
  643 +
  644 +/**
  645 + * 鼠标购物车的滑入滑出效果
  646 + * @return {[type]} [description]
  647 + */
  648 +function actionGoodsCarMouseEffect() {
  649 + var $target;
  650 +
  651 + $('#miniCartBox').mouseenter(function(e) {
  652 + $target = $(e.target);
  653 + if ($target.attr('id') === 'miniCartBox' || $target.attr('class') === 'ic-infomation') {
  654 + showMiniCart(1);
  655 + }
  656 + });
  657 + $('#miniCartBox').mouseleave(function(e) {
  658 + showMiniCart(0);
  659 + });
  660 +}
  661 +/**
400 * 获得banner & 异常通知 662 * 获得banner & 异常通知
401 * @return {[type]} [description] 663 * @return {[type]} [description]
402 */ 664 */
@@ -496,15 +758,47 @@ function actionBrandChange() { @@ -496,15 +758,47 @@ function actionBrandChange() {
496 } 758 }
497 } 759 }
498 /** 760 /**
  761 + * 循环检测购物车数量
  762 + * @return {[type]} [description]
  763 + */
  764 +function actionLoopUpdCartNum() {
  765 + setInterval(actionUpdateCartNum, 2000);
  766 +}
  767 +/**
  768 + * 处理模板
  769 + * @return {[type]} [description]
  770 + */
  771 +function actionExeTemplate() {
  772 + var resulthtml = $('#goodcartempwarpper').html().replace(/\%L/g, '{{').replace(/\%R/g, '}}');
  773 +
  774 + $('#goodcartempwarpper').html(resulthtml);
  775 +}
  776 +/**
  777 + * 执行搜索
  778 + * @return {[type]} [description]
  779 + */
  780 +function actionSearch() {
  781 + var searchDomain = 'http://search.yohobuy.com/api/suggest';
  782 +
  783 + $('#query_key').search(searchDomain);
  784 +}
  785 +/**
499 * 初始化函数 786 * 初始化函数
500 * @return {[type]} [description] 787 * @return {[type]} [description]
501 */ 788 */
502 exports.init = function() { 789 exports.init = function() {
503 - actionExeCookieMap();  
504 - actionYoHoGroup();  
505 - actionTopTagToggle();  
506 - actionTopLogoAnimate();  
507 - actionGetBannerAndNotice();  
508 - actionBrandChange();  
509 - actionAddKeyWords(); 790 + actionExeTemplate(); //处理模板
  791 + actionInitCookie(); //初始化cookie
  792 + actionExeCookieMap(); //格式化cookie
  793 + actionYoHoGroup(); // yoho集团鼠标效果
  794 + actionTopTagToggle(); // yoho上部分开关
  795 + actionTopLogoAnimate(); //yoho logo动画
  796 + actionGetBannerAndNotice(); // 获取banner和服务器维护提示
  797 + actionBrandChange(); //切换品牌
  798 + actionAddKeyWords(); //跳转后增加关键字
  799 + actionFirstMenuClick(); //一级菜单点击(会设置cookie)
  800 + actionGoodsCarMouseEffect(); //鼠标滑入滑出效果
  801 + actionUpdateCartNum(); //更新购物车数量
  802 + actionLoopUpdCartNum(); //循环检测购物车数量
  803 + actionSearch(); //搜索执行
510 }; 804 };
  1 +// yohodoT.js
  2 +// 2011-2014, Laura Doktorova, https://github.com/olado/yohodoT
  3 +// Licensed under the MIT license.
  4 +
  5 +
  6 +"use strict";
  7 +
  8 +var yohodoT = {
  9 + version: "1.0.3",
  10 + templateSettings: {
  11 + evaluate: /\{\{([\s\S]+?(\}?)+)\}\}/g,
  12 + interpolate: /\{\{=([\s\S]+?)\}\}/g,
  13 + encode: /\{\{!([\s\S]+?)\}\}/g,
  14 + use: /\{\{#([\s\S]+?)\}\}/g,
  15 + useParams: /(^|[^\w$])def(?:\.|\[[\'\"])([\w$\.]+)(?:[\'\"]\])?\s*\:\s*([\w$\.]+|\"[^\"]+\"|\'[^\']+\'|\{[^\}]+\})/g,
  16 + define: /\{\{##\s*([\w\.$]+)\s*(\:|=)([\s\S]+?)#\}\}/g,
  17 + defineParams: /^\s*([\w$]+):([\s\S]+)/,
  18 + conditional: /\{\{\?(\?)?\s*([\s\S]*?)\s*\}\}/g,
  19 + iterate: /\{\{~\s*(?:\}\}|([\s\S]+?)\s*\:\s*([\w$]+)\s*(?:\:\s*([\w$]+))?\s*\}\})/g,
  20 + varname: "it",
  21 + strip: true,
  22 + append: true,
  23 + selfcontained: false,
  24 + doNotSkipEncoded: false
  25 + },
  26 + template: undefined, //fn, compile template
  27 + compile: undefined //fn, for express
  28 + },
  29 + _globals;
  30 +
  31 +yohodoT.encodeHTMLSource = function(doNotSkipEncoded) {
  32 + var encodeHTMLRules = {
  33 + "&": "&#38;",
  34 + "<": "&#60;",
  35 + ">": "&#62;",
  36 + '"': "&#34;",
  37 + "'": "&#39;",
  38 + "/": "&#47;"
  39 + },
  40 + matchHTML = doNotSkipEncoded ? /[&<>"'\/]/g : /&(?!#?\w+;)|<|>|"|'|\//g;
  41 + return function(code) {
  42 + return code ? code.toString().replace(matchHTML, function(m) {
  43 + return encodeHTMLRules[m] || m;
  44 + }) : "";
  45 + };
  46 +};
  47 +
  48 +_globals = (function() {
  49 + return this || (0, eval)("this");
  50 +}());
  51 +
  52 +if (typeof module !== "undefined" && module.exports) {
  53 + module.exports = yohodoT;
  54 +} else if (typeof define === "function" && define.amd) {
  55 + define(function() {
  56 + return yohodoT;
  57 + });
  58 +} else {
  59 + _globals.yohodoT = yohodoT;
  60 +}
  61 +
  62 +var startend = {
  63 + append: {
  64 + start: "'+(",
  65 + end: ")+'",
  66 + startencode: "'+encodeHTML("
  67 + },
  68 + split: {
  69 + start: "';out+=(",
  70 + end: ");out+='",
  71 + startencode: "';out+=encodeHTML("
  72 + }
  73 + },
  74 + skip = /$^/;
  75 +
  76 +function resolveDefs(c, block, def) {
  77 + return ((typeof block === "string") ? block : block.toString())
  78 + .replace(c.define || skip, function(m, code, assign, value) {
  79 + if (code.indexOf("def.") === 0) {
  80 + code = code.substring(4);
  81 + }
  82 + if (!(code in def)) {
  83 + if (assign === ":") {
  84 + if (c.defineParams) value.replace(c.defineParams, function(m, param, v) {
  85 + def[code] = {
  86 + arg: param,
  87 + text: v
  88 + };
  89 + });
  90 + if (!(code in def)) def[code] = value;
  91 + } else {
  92 + new Function("def", "def['" + code + "']=" + value)(def);
  93 + }
  94 + }
  95 + return "";
  96 + })
  97 + .replace(c.use || skip, function(m, code) {
  98 + if (c.useParams) code = code.replace(c.useParams, function(m, s, d, param) {
  99 + if (def[d] && def[d].arg && param) {
  100 + var rw = (d + ":" + param).replace(/'|\\/g, "_");
  101 + def.__exp = def.__exp || {};
  102 + def.__exp[rw] = def[d].text.replace(new RegExp("(^|[^\\w$])" + def[d].arg + "([^\\w$])", "g"), "$1" + param + "$2");
  103 + return s + "def.__exp['" + rw + "']";
  104 + }
  105 + });
  106 + var v = new Function("def", "return " + code)(def);
  107 + return v ? resolveDefs(c, v, def) : v;
  108 + });
  109 +}
  110 +
  111 +function unescape(code) {
  112 + return code.replace(/\\('|\\)/g, "$1").replace(/[\r\t\n]/g, " ");
  113 +}
  114 +
  115 +yohodoT.template = function(tmpl, c, def) {
  116 + c = c || yohodoT.templateSettings;
  117 + var cse = c.append ? startend.append : startend.split,
  118 + needhtmlencode, sid = 0,
  119 + indv,
  120 + str = (c.use || c.define) ? resolveDefs(c, tmpl, def || {}) : tmpl;
  121 +
  122 + str = ("var out='" + (c.strip ? str.replace(/(^|\r|\n)\t* +| +\t*(\r|\n|$)/g, " ")
  123 + .replace(/\r|\n|\t|\/\*[\s\S]*?\*\//g, "") : str)
  124 + .replace(/'|\\/g, "\\$&")
  125 + .replace(c.interpolate || skip, function(m, code) {
  126 + return cse.start + unescape(code) + cse.end;
  127 + })
  128 + .replace(c.encode || skip, function(m, code) {
  129 + needhtmlencode = true;
  130 + return cse.startencode + unescape(code) + cse.end;
  131 + })
  132 + .replace(c.conditional || skip, function(m, elsecase, code) {
  133 + return elsecase ?
  134 + (code ? "';}else if(" + unescape(code) + "){out+='" : "';}else{out+='") :
  135 + (code ? "';if(" + unescape(code) + "){out+='" : "';}out+='");
  136 + })
  137 + .replace(c.iterate || skip, function(m, iterate, vname, iname) {
  138 + if (!iterate) return "';} } out+='";
  139 + sid += 1;
  140 + indv = iname || "i" + sid;
  141 + iterate = unescape(iterate);
  142 + 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+='";
  143 + })
  144 + .replace(c.evaluate || skip, function(m, code) {
  145 + return "';" + unescape(code) + "out+='";
  146 + }) + "';return out;")
  147 + .replace(/\n/g, "\\n").replace(/\t/g, '\\t').replace(/\r/g, "\\r")
  148 + .replace(/(\s|;|\}|^|\{)out\+='';/g, '$1').replace(/\+''/g, "");
  149 + //.replace(/(\s|;|\}|^|\{)out\+=''\+/g,'$1out+=');
  150 +
  151 + if (needhtmlencode) {
  152 + if (!c.selfcontained && _globals && !_globals._encodeHTML) _globals._encodeHTML = yohodoT.encodeHTMLSource(c.doNotSkipEncoded);
  153 + str = "var encodeHTML = typeof _encodeHTML !== 'undefined' ? _encodeHTML : (" + yohodoT.encodeHTMLSource.toString() + "(" + (c.doNotSkipEncoded || '') + "));" + str;
  154 + }
  155 + try {
  156 + return new Function(c.varname, str);
  157 + } catch (e) {
  158 + if (typeof console !== "undefined") console.log("Could not create a template function: " + str);
  159 + throw e;
  160 + }
  161 +};
  162 +
  163 +yohodoT.compile = function(tmpl, def) {
  164 + return yohodoT.template(tmpl, null, def);
  165 +};
  166 +module.exports = yohodoT;
@@ -761,11 +761,13 @@ @@ -761,11 +761,13 @@
761 overflow: hidden; 761 overflow: hidden;
762 text-overflow: ellipsis; 762 text-overflow: ellipsis;
763 white-space: nowrap; 763 white-space: nowrap;
  764 + font-size: 12px;
764 } 765 }
765 766
766 .valuenum { 767 .valuenum {
767 position: absolute; 768 position: absolute;
768 right: 10px; 769 right: 10px;
  770 + font-size: 12px;
769 } 771 }
770 .header-tool { 772 .header-tool {
771 padding-right: 23px; 773 padding-right: 23px;