Authored by 周少峰

Merge branch 'feature/shopFavAsync'

@@ -124,9 +124,26 @@ const collectShop = (req, res, next) => { @@ -124,9 +124,26 @@ const collectShop = (req, res, next) => {
124 } 124 }
125 }; 125 };
126 126
  127 +const isFavShop = (req, res, next) => {
  128 + let uid = req.user.uid;
  129 + let shopId = req.body.shopId;
  130 +
  131 + if (!uid || !shopId) {
  132 + res.json({
  133 + code: 400,
  134 + message: '未收藏'
  135 + });
  136 + }
  137 +
  138 + fav.getFavStatus(uid, shopId, 'shop').then(result => {
  139 + res.json(result);
  140 + }).catch(next);
  141 +};
  142 +
127 module.exports = { 143 module.exports = {
128 changeFavoriteBrand, 144 changeFavoriteBrand,
129 collectProduct, 145 collectProduct,
130 collectShop, 146 collectShop,
  147 + isFavShop,
131 isFavoriteBrand 148 isFavoriteBrand
132 }; 149 };
@@ -9,6 +9,23 @@ @@ -9,6 +9,23 @@
9 const api = global.yoho.API; 9 const api = global.yoho.API;
10 10
11 /** 11 /**
  12 + * 是否收藏API
  13 + * @function cancelFavAsync
  14 + * @param { number } uid 用户uid
  15 + * @param { number } id 收藏id
  16 + * @param { string } type 类型 product--商品 brand--品牌 shop--店铺
  17 + * @return { Object } 收藏状态
  18 + */
  19 +const isFavAsync = (uid, id, type) => {
  20 + return api.get('', {
  21 + method: 'app.favorite.isFavorite',
  22 + id: id,
  23 + uid: uid,
  24 + type: type
  25 + });
  26 +};
  27 +
  28 +/**
12 * 收藏API 29 * 收藏API
13 * @function addFavAsync 30 * @function addFavAsync
14 * @param { number } uid 用户uid 31 * @param { number } uid 用户uid
@@ -43,6 +60,7 @@ const cancelFavAsync = (uid, id, type) => { @@ -43,6 +60,7 @@ const cancelFavAsync = (uid, id, type) => {
43 }; 60 };
44 61
45 module.exports = { 62 module.exports = {
  63 + isFavAsync, // 是否收藏
46 addFavAsync, // 收藏 64 addFavAsync, // 收藏
47 cancelFavAsync // 取消收藏 65 cancelFavAsync // 取消收藏
48 }; 66 };
@@ -56,7 +56,12 @@ const toggleFavShop = (shopId, uid, isadd) => { @@ -56,7 +56,12 @@ const toggleFavShop = (shopId, uid, isadd) => {
56 } 56 }
57 }; 57 };
58 58
  59 +const getFavStatus = (uid, id, type) => {
  60 + return favAPI.isFavAsync(uid, id, type);
  61 +};
  62 +
59 module.exports = { 63 module.exports = {
  64 + getFavStatus, // 收藏状态
60 toggleFavProduct, // 收藏商品 65 toggleFavProduct, // 收藏商品
61 toggleFavBrand, // 收藏品牌 66 toggleFavBrand, // 收藏品牌
62 toggleFavShop // 收藏店铺 67 toggleFavShop // 收藏店铺
@@ -85,6 +85,7 @@ router.get('/index/about', list.brandAbout); @@ -85,6 +85,7 @@ router.get('/index/about', list.brandAbout);
85 85
86 router.get('/shoplist', list.shopList); // 店铺列表页 86 router.get('/shoplist', list.shopList); // 店铺列表页
87 router.post('/shop/togglecollect', favorite.collectShop); // 店铺收藏 87 router.post('/shop/togglecollect', favorite.collectShop); // 店铺收藏
  88 +router.post('/index/isFavoriteShop', favorite.isFavShop); // 判断用户是否收藏品牌
88 89
89 // 品牌页水牌 90 // 品牌页水牌
90 router.post('/index/getNodeContent', list.getNodeContent); 91 router.post('/index/getNodeContent', list.getNodeContent);
@@ -72,3 +72,22 @@ if ($brandFavor && $brandFavor.length) { @@ -72,3 +72,22 @@ if ($brandFavor && $brandFavor.length) {
72 } 72 }
73 }); 73 });
74 } 74 }
  75 +
  76 +// 页面进入更新收藏状态
  77 +if ($shopFavor && $shopFavor.length) {
  78 + $.ajax({
  79 + type: 'POST',
  80 + url: '/product/index/isFavoriteShop',
  81 + data: {
  82 + shopId: shopId
  83 + }
  84 + }).then(function(data) {
  85 + if (data.code === 200 && data.data) {
  86 + // 已收藏
  87 + $shopFavor.find('i').addClass('coled');
  88 + return;
  89 + }
  90 +
  91 + $shopFavor.find('i').removeClass('coled');
  92 + });
  93 +}
@@ -11,12 +11,16 @@ var product = require('./index/product'); @@ -11,12 +11,16 @@ var product = require('./index/product');
11 11
12 var $shopIntro = $('.shop-intro'), 12 var $shopIntro = $('.shop-intro'),
13 $shopCollect = $('.shop-collect'), 13 $shopCollect = $('.shop-collect'),
  14 + $colloectIcon = $shopCollect.find('.shop-collect-ico'),
  15 + $colloectText = $shopCollect.find('.shop-collect-text'),
14 $searchForm = $('#shop-search-form'), 16 $searchForm = $('#shop-search-form'),
15 $sliderLeft = $('.slider-left'), 17 $sliderLeft = $('.slider-left'),
16 $allGoods = $('.all-goods'), 18 $allGoods = $('.all-goods'),
17 $fixedArea = $allGoods.find('.fixed-area'), 19 $fixedArea = $allGoods.find('.fixed-area'),
18 fixedAreaTop = $fixedArea.offset() ? $fixedArea.offset().top : 0; 20 fixedAreaTop = $fixedArea.offset() ? $fixedArea.offset().top : 0;
19 21
  22 +var shopId = $shopCollect.data('id');
  23 +
20 // Pjax 24 // Pjax
21 require('yoho-jquery-pjax'); 25 require('yoho-jquery-pjax');
22 26
@@ -36,6 +40,26 @@ if ($sliderLeft.length) { @@ -36,6 +40,26 @@ if ($sliderLeft.length) {
36 $sliderLeft.slider(); 40 $sliderLeft.slider();
37 } 41 }
38 42
  43 +if ($shopCollect && $shopCollect.length) {
  44 + $.ajax({
  45 + type: 'POST',
  46 + url: '/product/index/isFavoriteShop',
  47 + data: {
  48 + shopId: shopId
  49 + }
  50 + }).then(function(data) {
  51 + if (data.code === 200 && data.data) {
  52 + // 已收藏
  53 + $colloectIcon.addClass('on');
  54 + $colloectText.html('已收藏');
  55 + return;
  56 + }
  57 +
  58 + $colloectIcon.removeClass('on');
  59 + $colloectText.html('收藏');
  60 + });
  61 +}
  62 +
39 $shopIntro.on('click', function() { 63 $shopIntro.on('click', function() {
40 $('.pop-shop-intro').show(); 64 $('.pop-shop-intro').show();
41 $('.mask').show(); 65 $('.mask').show();
@@ -53,9 +77,7 @@ $('.shop-query-submit').on('click', function() { @@ -53,9 +77,7 @@ $('.shop-query-submit').on('click', function() {
53 77
54 // 收藏店铺 78 // 收藏店铺
55 function colloectAction() { 79 function colloectAction() {
56 - var $colloectIcon = $shopCollect.find('.shop-collect-ico'),  
57 - $colloectText = $shopCollect.find('.shop-collect-text'),  
58 - isFavorite = $colloectIcon.hasClass('on'), 80 + var isFavorite = $colloectIcon.hasClass('on'),
59 needColloect = window.cookie('needColloect'); 81 needColloect = window.cookie('needColloect');
60 82
61 $.ajax({ 83 $.ajax({
@@ -64,7 +86,7 @@ function colloectAction() { @@ -64,7 +86,7 @@ function colloectAction() {
64 data: { 86 data: {
65 isFavorite: isFavorite ? 0 : 1, 87 isFavorite: isFavorite ? 0 : 1,
66 needColloect: needColloect, 88 needColloect: needColloect,
67 - shopId: $shopCollect.data('id') 89 + shopId: shopId
68 }, 90 },
69 success: function(res) { 91 success: function(res) {
70 if (res.code === 200) { 92 if (res.code === 200) {