Authored by yyq

店铺收藏

@@ -6,6 +6,7 @@ @@ -6,6 +6,7 @@
6 const helpers = global.yoho.helpers; 6 const helpers = global.yoho.helpers;
7 const brandService = require('../models/favorite-brand-service'); 7 const brandService = require('../models/favorite-brand-service');
8 const productService = require('../models/favorite-product-service'); 8 const productService = require('../models/favorite-product-service');
  9 +const fav = require('../models/favorite');
9 10
10 /** 11 /**
11 * 收藏品牌ajax请求 12 * 收藏品牌ajax请求
@@ -85,7 +86,36 @@ const collectProduct = (req, res, next) => { @@ -85,7 +86,36 @@ const collectProduct = (req, res, next) => {
85 } 86 }
86 }; 87 };
87 88
  89 +const collectShop = (req, res, next) => {
  90 + let uid = req.user.uid || '';
  91 + let shopId = req.body.shopId;
  92 + let isadd = req.body.isFavorite;
  93 +
  94 + // needColloect 说明刚登录状态 是cookie传的值
  95 + if (req.body.needColloect * 1 === 1) {
  96 + isadd = true;
  97 + }
  98 +
  99 + if (!uid) {
  100 + res.json({
  101 + code: 401,
  102 + message: '用户没有登录',
  103 + data: {url: helpers.urlFormat('/signin')}
  104 + });
  105 + } else if (!shopId) {
  106 + res.json({
  107 + code: 400,
  108 + message: '收藏失败'
  109 + });
  110 + } else {
  111 + fav.toggleFavShop(shopId, uid, isadd).then(result => {
  112 + res.json(result);
  113 + }).catch(next);
  114 + }
  115 +};
  116 +
88 module.exports = { 117 module.exports = {
89 changeFavoriteBrand, 118 changeFavoriteBrand,
90 - collectProduct 119 + collectProduct,
  120 + collectShop
91 }; 121 };
@@ -24,6 +24,9 @@ const shop = (shopId, req, res, next) => { @@ -24,6 +24,9 @@ const shop = (shopId, req, res, next) => {
24 24
25 // 经典模板 25 // 经典模板
26 list.getShopData(shopId, req.user.id, req.query, shopInfo).then(result => { 26 list.getShopData(shopId, req.user.id, req.query, shopInfo).then(result => {
  27 + Object.assign(result, {
  28 + page: 'shop'
  29 + });
27 res.render('list/shop-index', result); 30 res.render('list/shop-index', result);
28 }).catch(next); 31 }).catch(next);
29 32
@@ -126,6 +129,9 @@ exports.shopList = (req, res, next) => { @@ -126,6 +129,9 @@ exports.shopList = (req, res, next) => {
126 } 129 }
127 130
128 list.getShopListData(req.query).then(result => { 131 list.getShopListData(req.query).then(result => {
  132 + Object.assign(result, {
  133 + page: 'shop'
  134 + });
129 res.render('list/shop-list', result); 135 res.render('list/shop-list', result);
130 }).catch(next); 136 }).catch(next);
131 }; 137 };
  1 +/**
  2 + * 收藏相关接口
  3 + * @author: yyq<yanqing.yang@yoho.cn>
  4 + * @date: 2016/7/17
  5 + */
  6 +
  7 +'use strict';
  8 +
  9 +const api = global.yoho.API;
  10 +
  11 +/**
  12 + * 收藏API
  13 + * @function addFavAsync
  14 + * @param { number } uid 用户uid
  15 + * @param { number } id 收藏id
  16 + * @param { string } type 收藏类型 product--商品 brand--品牌 shop--店铺
  17 + * @return { Object } 收藏结果
  18 + */
  19 +const addFavAsync = (uid, id, type) => {
  20 + return api.get('', {
  21 + method: 'app.favorite.add',
  22 + id: id,
  23 + uid: uid,
  24 + type: type
  25 + });
  26 +};
  27 +
  28 +/**
  29 + * 取消收藏API
  30 + * @function cancelFavAsync
  31 + * @param { number } uid 用户uid
  32 + * @param { number } id 收藏id
  33 + * @param { string } type 收藏类型 product--商品 brand--品牌 shop--店铺
  34 + * @return { Object } 取消收藏结果
  35 + */
  36 +const cancelFavAsync = (uid, id, type) => {
  37 + return api.get('', {
  38 + method: 'app.favorite.cancel',
  39 + fav_id: id,
  40 + uid: uid,
  41 + type: type
  42 + });
  43 +};
  44 +
  45 +module.exports = {
  46 + addFavAsync, // 收藏
  47 + cancelFavAsync // 取消收藏
  48 +};
@@ -4,6 +4,6 @@ @@ -4,6 +4,6 @@
4 4
5 'use strict'; 5 'use strict';
6 6
7 -const favoriteProductAPI = require('./favorite-product-api'); 7 +const favoriteProductAPI = require('./favorite-api');
8 8
9 module.exports = favoriteProductAPI; 9 module.exports = favoriteProductAPI;
  1 +/**
  2 + * 收藏相关接口
  3 + * @author: yyq<yanqing.yang@yoho.cn>
  4 + * @date: 2016/7/17
  5 + */
  6 +
  7 +'use strict';
  8 +
  9 +const favAPI = require('./favorite-api');
  10 +
  11 +/**
  12 + * 收藏商品
  13 + * @function toggleFavProduct
  14 + * @param { number } productId 商品id
  15 + * @param { number } uid 用户uid
  16 + * @param { string } isadd 是否收藏 true--添加收藏 false--取消收藏
  17 + * @return { Object } 收藏结果
  18 + */
  19 +const toggleFavProduct = (productId, uid, isadd) => {
  20 + if (isadd) {
  21 + return favAPI.addFavAsync(uid, productId, 'product');
  22 + } else {
  23 + return favAPI.cancelFavAsync(uid, productId, 'product');
  24 + }
  25 +};
  26 +
  27 +/**
  28 + * 收藏品牌
  29 + * @function toggleFavBrand
  30 + * @param { number } brandId 品牌id
  31 + * @param { number } uid 用户uid
  32 + * @param { string } isadd 是否收藏 true--添加收藏 false--取消收藏
  33 + * @return { Object } 收藏结果
  34 + */
  35 +const toggleFavBrand = (brandId, uid, isadd) => {
  36 + if (isadd) {
  37 + return favAPI.addFavAsync(uid, brandId, 'brand');
  38 + } else {
  39 + return favAPI.cancelFavAsync(uid, brandId, 'brand');
  40 + }
  41 +};
  42 +
  43 +/**
  44 + * 收藏店铺
  45 + * @function toggleFavShop
  46 + * @param { number } shopId 店铺id
  47 + * @param { number } uid 用户uid
  48 + * @param { string } isadd 是否收藏 true--添加收藏 false--取消收藏
  49 + * @return { Object } 收藏结果
  50 + */
  51 +const toggleFavShop = (shopId, uid, isadd) => {
  52 + if (isadd) {
  53 + return favAPI.addFavAsync(uid, shopId, 'shop');
  54 + } else {
  55 + return favAPI.cancelFavAsync(uid, shopId, 'shop');
  56 + }
  57 +};
  58 +
  59 +module.exports = {
  60 + toggleFavProduct, // 收藏商品
  61 + toggleFavBrand, // 收藏品牌
  62 + toggleFavShop // 收藏店铺
  63 +};
@@ -13,6 +13,8 @@ const shopHandler = require('./shop-handler'); @@ -13,6 +13,8 @@ const shopHandler = require('./shop-handler');
13 const helpers = global.yoho.helpers; 13 const helpers = global.yoho.helpers;
14 const _ = require('lodash'); 14 const _ = require('lodash');
15 15
  16 +const limitNum = 60; // 商品每页显示数目
  17 +
16 // 品牌页folder名称 18 // 品牌页folder名称
17 const brandFolderSeries = '经典系列'; 19 const brandFolderSeries = '经典系列';
18 20
@@ -419,12 +421,10 @@ exports.getShopListData = (params) => { @@ -419,12 +421,10 @@ exports.getShopListData = (params) => {
419 return Promise.all(apiMethod).then(result => { 421 return Promise.all(apiMethod).then(result => {
420 let finalResult = { 422 let finalResult = {
421 headerData: Object.assign(result[0].headerData, {header: true}) 423 headerData: Object.assign(result[0].headerData, {header: true})
422 -  
423 - // pathNav: searchHandler.handlePathNavData(shopInfo, params, 'shop')  
424 }; 424 };
425 425
426 // 面包屑导航 426 // 面包屑导航
427 - // Object.assign(finalResult, searchHandler.handlePathNavData(shopInfo, params, 'shop')); 427 + Object.assign(finalResult, searchHandler.handlePathNavData({}, params, 'shop'));
428 428
429 // 店铺介绍 429 // 店铺介绍
430 430
@@ -439,17 +439,24 @@ exports.getShopListData = (params) => { @@ -439,17 +439,24 @@ exports.getShopListData = (params) => {
439 } 439 }
440 440
441 // // 获取商品数据和顶部筛选条件 441 // // 获取商品数据和顶部筛选条件
442 - // if (result[2].code === 200) {  
443 - // let allGoods = {  
444 - // name: '全部商品 ALL',  
445 - // sort: searchHandler.handleOptsData(params, _.get(result[2], 'data.total', 0)),  
446 - // list: productProcess.processProductList(_.get(result[2], 'data.product_list', []))  
447 - // }; 442 + if (result[2].code === 200) {
  443 + let tip = {
  444 + start: (_.get(result[2], 'data.page', 1) - 1) * limitNum + 1,
  445 + total: _.get(result[2], 'data.total', 0)
  446 + };
448 447
449 - // _.set(allGoods, 'sort.newPage', true); // 启用新的分页导航 448 + tip.end = tip.start + limitNum - 1;
  449 + if (tip.end > tip.total) {
  450 + tip.end = tip.total;
  451 + }
450 452
451 - // finalResult.allGoods = allGoods;  
452 - // } 453 + Object.assign(finalResult, {
  454 + filters: searchHandler.handleFilterData(_.get(result[2], 'data.filter', {}), params),
  455 + opts: searchHandler.handleOptsData(params, tip.total),
  456 + goods: productProcess.processProductList(_.get(result[2], 'data.product_list', [])),
  457 + footPager: {tip: tip}
  458 + });
  459 + }
453 460
454 if (result[3].code === 200) { 461 if (result[3].code === 200) {
455 finalResult.leftContent = searchHandler.handleSortData(_.get(result[3], 462 finalResult.leftContent = searchHandler.handleSortData(_.get(result[3],
@@ -30,9 +30,9 @@ const relateArticleUrl = 'guang/service/v2/article/getArticleByBrand'; @@ -30,9 +30,9 @@ const relateArticleUrl = 'guang/service/v2/article/getArticleByBrand';
30 */ 30 */
31 const getProductList = (params) => { 31 const getProductList = (params) => {
32 let finalParams = { 32 let finalParams = {
33 - limit: 60, 33 + method: 'app.search.li',
34 order: 's_n_desc', 34 order: 's_n_desc',
35 - method: 'app.search.li' 35 + limit: 60
36 }; 36 };
37 37
38 Object.assign(finalParams, params); 38 Object.assign(finalParams, params);
@@ -13,7 +13,7 @@ const helpers = global.yoho.helpers; @@ -13,7 +13,7 @@ const helpers = global.yoho.helpers;
13 const newProductsName = '新品上架 NEW'; 13 const newProductsName = '新品上架 NEW';
14 const hotProductsName = '人气单品 HOT'; 14 const hotProductsName = '人气单品 HOT';
15 const shopRecommentName = '经典推荐'; 15 const shopRecommentName = '经典推荐';
16 -const shopListUrl = '/shoplist'; 16 +const shopListUrl = '/product/shoplist';
17 17
18 /** 18 /**
19 * 新品上架 19 * 新品上架
@@ -134,22 +134,21 @@ const navigationBar = (data, shopId) => { @@ -134,22 +134,21 @@ const navigationBar = (data, shopId) => {
134 }, 134 },
135 { 135 {
136 name: '全部商品', 136 name: '全部商品',
137 - url: `{shopListUrl}/?navBar=1&shopId=${shopId}` 137 + url: `${shopListUrl}/?navBar=1&shopId=${shopId}`
138 }, 138 },
139 { 139 {
140 name: '人气单品', 140 name: '人气单品',
141 - url: `{shopListUrl}/?navBar=2&shopId=${shopId}` 141 + url: `${shopListUrl}/?navBar=2&shopId=${shopId}`
142 }, 142 },
143 { 143 {
144 name: '新品上架', 144 name: '新品上架',
145 - url: `{shopListUrl}/?navBar=3&shopId=${shopId}` 145 + url: `${shopListUrl}/?navBar=3&shopId=${shopId}`
146 } 146 }
147 ]; 147 ];
148 148
149 return {navigationBar: _.union(shopNav, _.filter(data, (value) => { 149 return {navigationBar: _.union(shopNav, _.filter(data, (value) => {
150 return value.url; 150 return value.url;
151 - }))  
152 - }; 151 + }))};
153 }; 152 };
154 153
155 /** 154 /**
@@ -71,8 +71,8 @@ router.get('/list/new', list.new); @@ -71,8 +71,8 @@ router.get('/list/new', list.new);
71 // 品牌页 71 // 品牌页
72 router.get('/list/brand', list.brand); 72 router.get('/list/brand', list.brand);
73 73
74 -// 品牌页  
75 -router.get('/shoplist', list.shopList); 74 +router.get('/shoplist', list.shopList); // 店铺列表页
  75 +router.post('/shop/togglecollect', favorite.collectShop); // 店铺收藏
76 76
77 // 品牌页水牌 77 // 品牌页水牌
78 router.post('/index/getNodeContent', list.getNodeContent); 78 router.post('/index/getNodeContent', list.getNodeContent);
  1 +/**
  2 + * 首页
  3 + * @author: bikai<kai.bi@yoho.cn>
  4 + * @date: 2016/4/26
  5 + */
  6 +
  7 +var $ = require('yoho-jquery'),
  8 + lazyLoad = require('yoho-jquery-lazyload');
  9 +
  10 +var product = require('./index/product');
  11 +
  12 +var $shopIntro = $('.shop-intro'),
  13 + $shopCollect = $('.shop-collect'),
  14 +
  15 + // $sliderLeft = $('.slider-left'),
  16 + $allGoods = $('.all-goods'),
  17 + $fixedArea = $allGoods.find('.fixed-area'),
  18 + fixedAreaTop = $fixedArea.offset() ? $fixedArea.offset().top : 0;
  19 +
  20 +// Pjax
  21 +// require('yoho-pjax');
  22 +
  23 +require('../common');
  24 +require('../plugins/filter');
  25 +require('../plugins/sort-pager');
  26 +
  27 +// require('../common/slider');
  28 +// require('../product/list');
  29 +
  30 +product.init(4);
  31 +
  32 +lazyLoad($('img.lazy'));
  33 +
  34 +// if ($sliderLeft.length) {
  35 +// $sliderLeft.slider();
  36 +// }
  37 +
  38 +$shopIntro.on('click', function() {
  39 + $('.pop-shop-intro').show();
  40 + $('.mask').show();
  41 +});
  42 +
  43 +$('.close-btn, .mask').on('click', function() {
  44 + $('.pop-shop-intro').hide();
  45 + $('.mask').hide();
  46 +});
  47 +
  48 +
  49 +// 收藏店铺
  50 +function colloectAction() {
  51 + var $colloectIcon = $shopCollect.find('.shop-collect-ico'),
  52 + $colloectText = $shopCollect.find('.shop-collect-text'),
  53 + isFavorite = $colloectIcon.hasClass('on'),
  54 + needColloect = window.cookie('needColloect');
  55 +
  56 + $.ajax({
  57 + type: 'post',
  58 + url: '/product/shop/togglecollect',
  59 + data: {
  60 + isFavorite: isFavorite ? 0 : 1,
  61 + needColloect: needColloect,
  62 + shopId: $shopCollect.data('id')
  63 + },
  64 + success: function(res) {
  65 + if (res.code === 200) {
  66 + if (isFavorite) {
  67 + $colloectIcon.removeClass('on');
  68 + $colloectText.html('收藏');
  69 + } else {
  70 + $colloectIcon.addClass('on');
  71 + $colloectText.html('已收藏');
  72 + }
  73 +
  74 + if (needColloect) {
  75 + $colloectIcon.addClass('on');
  76 + $colloectText.html('已收藏');
  77 + }
  78 + } else if (res.code === 401) {
  79 + window.setCookie('needColloect', '1', {
  80 + path: '/',
  81 + domain: '.yohobuy.com',
  82 + expires: 90
  83 + });
  84 + location.href = res.data.url;
  85 + }
  86 + }
  87 + });
  88 +}
  89 +
  90 +$shopCollect.on('click', function() {
  91 + colloectAction();
  92 +});
  93 +
  94 +if (window.cookie('needColloect') * 1 === 1 && window.getUid()) {
  95 + colloectAction();
  96 + window.setCookie('needColloect', '', {
  97 + path: '/',
  98 + domain: '.yohobuy.com',
  99 + expires: 1
  100 + });
  101 +}
  102 +
  103 +// 全部商品使用 pjax 翻页
  104 +// $allGoods.pjax('a.pjax', '.goods-wrap', {
  105 +// timeout: 5000,
  106 +// scrollTo: false // 默认滚动没有动画,禁止掉
  107 +// }).on('pjax:end', function() {
  108 +// product.init(4);
  109 +// lazyLoad($('img.lazy'));
  110 +
  111 +// $fixedArea = $allGoods.find('.fixed-area'); // 翻页后 fixed-area 区域有变化,需要更新选择器
  112 +
  113 +// // 分页后移动到全部商品
  114 +// $('html, body').animate({
  115 +// scrollTop: $allGoods.offset().top - 30
  116 +// });
  117 +// }).on('click', '.menu-list li', function() {
  118 +// var $this = $(this);
  119 +
  120 +// $this.siblings().removeClass('on');
  121 +// $this.addClass('on');
  122 +// });
  123 +
  124 +$(window).on('scroll', function() {
  125 + var scrollTop = $(this).scrollTop();
  126 +
  127 + if (scrollTop > fixedAreaTop) {
  128 + $fixedArea.css({
  129 + position: 'fixed',
  130 + top: 0
  131 + });
  132 + } else {
  133 + $fixedArea.css({
  134 + position: 'static',
  135 + top: 0
  136 + });
  137 + }
  138 +});