Authored by htoooth

add fav

... ... @@ -6,46 +6,72 @@
'use strict';
const Promise = require('bluebird');
const co = Promise.coroutine;
const requestIp = require('request-ip');
const md5 = require('md5');
const helpers = global.yoho.helpers;
const favoriteService = require('../models/favorite-service');
const TABS = {
product: 'product',
brand: 'brand',
article: 'article'
};
const udidRequired = (req, res, next) => {
let udid = req.cookies.udid;
if (!udid) {
udid = md5(req.ip || requestIp.getClientIp(req));
if (res && res.cookie) {
res.cookie('udid', udid);
}
req.user.udid = udid;
}
next();
};
const index = (req, res, next)=> {
let uid = req.user.uid;
let udid = '8041246';
let udid = req.user.udid;
let page = +req.query.page || 1;
let type = req.query.type || TABS.product;
let selectedSort = +req.query.sort_id || 0;
let reduction = req.query.is_reduction || 'N';
let promotion = req.query.is_promotion || 'N';
let limit = +req.query.limit || 10;
co(function*() {
let page = req.query.page || 1;
let type = req.query.type || 'article';
let sort = req.query.sort_id || 0;
let reduction = req.query.is_reduction || 'N';
let promotion = req.query.is_promotion || 'N';
let limit = 10;
let data = {};
data.tabs = favoriteService.getFavoriteTabs('product');
data.tabs = favoriteService.getFavoriteTabs(type);
switch (type) {
case 'brand':
data.favBrands = yield favoriteService.favoriteBrandList(uid, page, limit, type);
case TABS.brand:
data.favBrands = yield favoriteService.favoriteBrandList(uid, page, limit);
break;
case 'article':
case TABS.article:
data.favArticles = yield favoriteService.favoriteArticleListAsync(uid, udid, page, limit);
break;
default:
data.favProducts = yield favoriteService.favoriteProductList(uid, page, limit, type, sort, 'N', reduction, promotion);
data.favProducts = yield favoriteService.favoriteProductList(
uid, page, limit, selectedSort, 'N', reduction, promotion, req.query
);
break;
}
res.render('favorite', {
return data;
})().then((result) => {
return res.render('favorite', {
meFavoritePage: true,
meFavorite: data
meFavorite: result
});
})().catch(next);
}).catch(next);
};
module.exports = {
index
index,
udidRequired
};
... ...
... ... @@ -38,28 +38,31 @@ const favoriteArticleData = (uid, udid, page, limit)=> {
return service.get(URL_ARTICLE_FAVORITE + 'getUserFavArticleList', options);
};
const getFavoriteProductList = (uid, limit)=> {
limit = limit || 500;
const getFavoriteProductList = (uid, page, limit)=> {
let options = {
method: 'web.favorite.product',
uid: uid,
limit: limit
page: 0,
limit: limit || 10
};
return api.get('', options);
};
const redutionCount = (uid)=> {
let options = {
return api.get('', {
method: 'web.redution.count',
uid: uid
};
return api.get('', options);
});
};
const favoriteBrandData = () => {
return Promise.resolve();
const favoriteBrandData = (uid, page, limit) => {
return api.get('', {
method: 'app.favorite.brand',
uid: uid,
page: page || 1,
limit: limit || 10
});
};
module.exports = {
... ...
... ... @@ -2,11 +2,11 @@
const Promise = require('bluebird');
const co = Promise.coroutine;
const _ = require('lodash');
const helpers = global.yoho.helpers;
const path = require('path');
const pagerPath = path.join(global.appRoot, '/apps/product/models/public-handler.js');
const pager = require(pagerPath).handlePagerData;
const pager = require('./pager').handlePagerData;
const favoriteApi = require('./favorite-api');
... ... @@ -26,10 +26,104 @@ const getFavoriteTabs = (type) => {
});
};
const favoriteProductList = (uid, page, limit, type, sort, subscribe, reduction, promotion) => {
const _getSortInfo = (categoryList, sort)=> {
if (_.isEmpty(categoryList)) {
return false;
}
let result = {};
result.all = categoryList.map((category) => {
return {
name: category.category_name,
url: helpers.urlFormat('/home/favorite', {sort_id: category.category_id}),
count: category.num,
focus: category.category_id === sort
};
});
let defaultCategory = {
name: '全部',
url: helpers.urlFormat('/home/favorite'),
count: _.sumBy(categoryList, category => category.num),
focus: sort === 0
};
result.all.unshift(defaultCategory);
result.default = result.all.slice(0, 7);
return result;
};
const _getPager = (page, total, totalPage, size, type)=> {
let result = {};
if (page && total && totalPage) {
result = {
count: total,
curPage: page,
totalPages: totalPage,
hasCheckAll: true
};
}
return result;
};
const getGoodsInfo = (data, page, limit)=> {
let result = data.slice((page - 1) * limit, page * limit).map((item) => {
return {
skn: item.product_id,
img: helpers.image(item.image, 100, 100),
name: item.product_name,
url: helpers.getUrlBySkc(item.product_id, item.goodsId, item.cnAlphabet),
price: item.sales_price,
priceDown: item.price_down,
buyNow: helpers.getUrlBySkc(item.product_id, item.goodsId, item.cnAlphabet),
soldOut: item.storage === 0 ? true : '',
hadNoticed: item.is_subscribe_reduction === 'Y' ? true : '',
activites: {
count: item.promotion_list ? item.promotion_list.length : 0,
list: _.get(item, 'promotion_list', []).map((val) => {
return {
type: val.promotion_type,
name: val.promotion_title
};
})
}
};
});
if (_.isEmpty(result)) {
return {
empty: '您没有收藏商品'
};
}
return result;
};
/**
* 降价提醒
*/
const _redutionCount = (uid)=> {
return co(function*() {
let data = yield favoriteApi.redutionCount(uid);
let result = {
count: 0,
phone: '',
url: '/home/favorite/reduction'
};
if (data.data.num) {
result.count = +data.data.num;
result.phone = data.data.mobile;
}
return result;
})();
};
const favoriteProductList = (uid, page, limit, selectedSort, subscribe, reduction, promotion, query) => {
return co(function*() {
let data = {};
let product = {};
let result = {
sort: {},
reduction: {},
... ... @@ -38,68 +132,80 @@ const favoriteProductList = (uid, page, limit, type, sort, subscribe, reduction,
pager: {}
};
product = yield favoriteApi.getFavoriteProductList(uid);
if (product.data.category_list) {
result.sort = getSortInfo(product.data.category_list, sort);
}
result.reduction = yield redutionCount(uid);
let productList = [];
if (product.data.product_list) {
product.data.product_list.forEach(function(product) {
if (
(reduction === 'Y' && promotion === 'Y' && product.is_price_down === 'Y' && promotion === 'Y') ||
(sort && product.category_id === sort) ||
(subscribe && product.is_subscribe_reduction === 'Y') ||
(reduction === 'Y' && product.is_price_down === 'Y') ||
(promotion === 'Y' && product.is_join_promotion === 'Y')
) {
productList.push(product);
}
});
productList = product.data.product_list;
}
if (reduction === 'N' && promotion === 'N') {
result.filter = {
reductionUrl: helpers.urlFormat('/home/favorite', {is_reduction: 'Y'}),
reductionChecked: '',
activityUrl: helpers.urlFormat('/home/favorite', {is_promotion: 'Y'}),
activityChecked: ''
};
} else if (reduction === 'N' && promotion === 'Y') {
result.filter = {
reductionUrl: helpers.urlFormat('/home/favorite', {is_reduction: 'Y', is_promotion: 'Y'}),
reductionChecked: '',
activityUrl: helpers.urlFormat('/home/favorite', {is_promotion: 'Y'}),
activityChecked: ''
};
} else if (reduction === 'Y' && promotion === 'N') {
result.filter = {
reductionUrl: helpers.urlFormat('/home/favorite', {is_reduction: 'Y', is_promotion: 'Y'}),
reductionChecked: '',
activityUrl: helpers.urlFormat('/home/favorite', {is_promotion: 'Y', is_promotion: 'Y'}),
activityChecked: ''
};
} else {
result.filter = {
reductionUrl: helpers.urlFormat('/home/favorite', {is_reduction: 'Y', is_promotion: 'Y'}),
reductionChecked: '',
activityUrl: helpers.urlFormat('/home/favorite', {is_promotion: 'Y', is_promotion: 'Y'}),
activityChecked: ''
};
}
let product = yield favoriteApi.getFavoriteProductList(uid, 1, 500);
result.sort = _getSortInfo(_.get(product, 'data.category_list'), selectedSort);
result.reduction = yield _redutionCount(uid);
let productList = (function() {
let products = _.get(product, 'data.product_list', []);
if (reduction === 'Y' && promotion === 'Y') {
//参加活动的降价商品
return products.filter(pro => pro.is_price_down === 'Y' && pro.is_join_promotion === 'Y');
} else if (selectedSort) {
//商品分类过滤
return products.filter(pro => pro.category_id === selectedSort);
} else if (subscribe === 'Y') {
//订阅降价通知过滤
return products.filter(pro => pro.is_subscribe_reduction === 'Y');
} else if (reduction === 'Y') {
//降价商品过滤
return products.filter(pro => pro.is_price_down === 'Y');
} else if (promotion === 'Y') {
//参加活动商品过滤
return products.filter(pro => pro.is_join_promotion === 'Y');
} else {
return products;
}
}());
result.filter = (function() {
if (reduction === 'N' && promotion === 'N') {
return {
reductionUrl: helpers.urlFormat('/home/favorite', {is_reduction: 'Y'}),
reductionChecked: '',
activityUrl: helpers.urlFormat('/home/favorite', {is_promotion: 'Y'}),
activityChecked: ''
};
} else if (reduction === 'N' && promotion === 'Y') {
return {
reductionUrl: helpers.urlFormat('/home/favorite', {is_reduction: 'Y', is_promotion: 'Y'}),
reductionChecked: '',
activityUrl: helpers.urlFormat('/home/favorite'),
activityChecked: ''
};
} else if (reduction === 'Y' && promotion === 'N') {
return {
reductionUrl: helpers.urlFormat('/home/favorite'),
reductionChecked: '',
activityUrl: helpers.urlFormat('/home/favorite', {is_reduction: 'Y', is_promotion: 'Y'}),
activityChecked: ''
};
} else {
return {
reductionUrl: helpers.urlFormat('/home/favorite', {is_promotion: 'Y'}),
reductionChecked: '',
activityUrl: helpers.urlFormat('/home/favorite', {is_reduction: 'Y'}),
activityChecked: ''
};
}
}());
let total = productList;
let total = productList.length;
let pageTotal = Math.ceil(total / limit);
result.pager = getPager(page, total, pageTotal);
page = page > pageTotal ? pageTotal : page;
result.goods = getGoodsInfo(productList, page, limit);
result.pager = pager(total, query);
result.pager.hasCheckAll = true;
return result;
})();
};
const favoriteBrandList = (uid, page, limit, type)=> {
const favoriteBrandList = (uid, page, limit)=> {
return co(function*() {
let result = {
brands: {
... ... @@ -113,19 +219,12 @@ const favoriteBrandList = (uid, page, limit, type)=> {
return result;
}
if (brand.data.page_total < page) {
page = brand.data.page_total;
brand = yield favoriteApi.favoriteBrandData(uid, page, limit);
}
if (!brand.data.brand_list) {
return result;
}
let brands = [];
brand.data.brand_list.forEach((item, i)=> {
brands.push({
result.brands = _.get(brand, 'data.brand_list', []).map((item) => {
return {
id: item.brand_id,
brandOrShopType: item.brandOrShopType || '',
shop_id: item.shop_id || '',
... ... @@ -134,127 +233,54 @@ const favoriteBrandList = (uid, page, limit, type)=> {
name: item.brand_name,
naCount: item.new_product_num,
colCount: item.brand_favorite_num
});
};
});
result.brands = brands;
let total = brand.data.total || 0;
let pageTotal = brand.data.page_total || 0;
page = brand.data.page || 0;
result.pager = getPager(page, total, pageTotal);
result.pager = _getPager(page, total, pageTotal);
return result;
})();
};
const favoriteArticleListAsync = (uid, udid, page, limit)=> {
return co(function*() {
let result = {articles: [], pager: {}};
let result = {};
let articles = yield favoriteApi.favoriteArticleData(uid, udid, page, limit);
if (!articles.data && !articles.data.data) {
articles.data.data.forEach((item)=> {
result.articles.push({
id: item.id,
name: item.title,
img: helpers.image(item.src, 146, 96),
desc: item.intro,
url: helpers.urlFormat('/' + item.id + '.html', '', 'guang')
});
});
let total = articles.data.total || 0;
let pageTotal = articles.data.totalPage || 0;
let pageNum = articles.data.page || 0;
result.pager = getPager(pageNum, total, pageTotal);
} else {
result.articles = {empty: '你尚未收藏任何文章!'};
}
return result;
})();
};
result.articles = _.get(articles, 'data.data', []).map((item) => {
return {
id: item.id,
name: item.title,
img: helpers.image(item.src, 146, 96),
desc: item.intro,
url: helpers.urlFormat('/' + item.id + '.html', '', 'guang')
};
});
const getPager = (page, total, totalPage, size, type)=> {
let result = {};
let total = articles.data.total || 0;
let pageTotal = articles.data.totalPage || 0;
let pageNum = articles.data.page || 0;
if (page && total && totalPage) {
result = {
count: total,
curPage: page,
totalPages: totalPage,
hasCheckAll: true
};
}
return result;
};
result.pager = _getPager(pageNum, total, pageTotal);
const getGoodsInfo = (data, page, limit)=> {
let result = [];
let begin = (page - 1) * limit;
if (!data) {
data = data.slice(begin, limit);
data.forEach((item, i)=> {
let obj = {
skn: item.product_id,
img: helpers.img(item.image, 100, 100),
name: item.product_name,
url: helpers.getUrlBySkc(item.product_id, item.goodsId, item.cnAlphabet),
price: item.sales_price,
priceDown: item.price_down,
buyNow: helpers.getUrlBySkc(item.product_id, item.goodsId, item.cnAlphabet),
soldOut: item.storage === 0 ? true : '',
hadNoticed: item.is_subscribe_reduction === 'Y' ? true : '',
count: item.promotion_list ? item.promotion_list.length : 0
if (_.isEmpty(result.articles)) {
result.articles = {
empty: '你尚未收藏任何文章!'
};
if (item.promotion_list) {
item.promotion_list.forEach(function(item1) {
obj.activites.list.push({
type: item1.promotion_type,
name: item1.promotion_title
});
});
}
result.push(obj);
});
} else {
result = {empty: '您没有收藏商品'};
}
return result;
};
const redutionCount = (uid)=> {
return co(function*() {
let result = {count: 0, url: '/home/favorite/reduction', phone: ''};
let data = yield favoriteApi.redutionCount(uid);
if (data.data.num) {
result.count = +data.data.num;
result.phone = data.data.mobile;
}
return result;
})();
};
const getSortInfo = (categoryList, sort)=> {
let result = {default: {}, all: []};
let defaultCategory = {name: '全部', url: helpers.urlFormat('/home/favorite'), count: 0, focus: ''};
categoryList.forEach(function(category) {
result.all.push({
name: category.category_name,
url: helpers.urlFormat('/home/favorite', {sort_id: category.category_id}),
count: category.num,
focus: category.category_id === sort ? true : ''
});
defaultCategory.count += category.num;
defaultCategory.focus = sort === 0 ? true : '';
});
result.all.unshift(defaultCategory);
result.default = result.all.slice(result.all, 0, 7);
return result;
};
module.exports = {
getFavoriteTabs,
... ...
'use strict';
const _ = require('lodash');
const handleFilterUrl = (originParam, newParam, delParam) => {
let dest = '?';
let tempOriginParam = {};
delParam = delParam || {};
tempOriginParam = Object.assign(tempOriginParam, originParam, newParam);
delete tempOriginParam.uid;
_.forEach(tempOriginParam, function(value, key) {
if (!delParam[key] && value) {
dest += `${key}=${value}&`;
}
});
return _.trim(dest, '&');
};
exports.handlePagerData = (total, params) => {
let result = {
prePage: {
url: ''
},
nextPage: {
url: ''
},
pages: []
};
let currentPage = parseInt(_.get(params, 'page', 1), 10); // 当前页
let perPageCount = parseInt(_.get(params, 'limit', 10), 10); // 每页商品数
let totalPage = _.ceil(total / perPageCount); // 总页数
result.count = total;
result.curPage = currentPage;
result.totalPages = totalPage;
if (currentPage === 1) {
// 当前页为 1,一定没有上一页
delete result.prePage;
} else {
result.prePage.url = handleFilterUrl(params, {page: currentPage - 1});
}
if (currentPage === totalPage) {
// 当前页为最后一页,一定没有下一页
delete result.nextPage;
} else {
result.nextPage.url = handleFilterUrl(params, {page: currentPage + 1});
}
if (totalPage === 1) {
return result;
}
// 页码临时数据
let pages = [];
if (currentPage > 2 && currentPage <= totalPage - 2) {
for (let i = currentPage - 2; i <= ((currentPage + 2) > totalPage ? totalPage : (currentPage + 2)); i++) {
pages.push({
url: handleFilterUrl(params, {page: i}),
num: i,
cur: currentPage === i
});
}
// 处理页码小于等于 2 的情况
} else if (currentPage <= 2) {
for (let i = 1; i <= (totalPage < 5 ? totalPage : 5); i++) {
pages.push({
url: handleFilterUrl(params, {page: i}),
num: i,
cur: currentPage === i
});
}
} else if (currentPage > totalPage - 2) {
for (let i = totalPage; i >= totalPage - 4; i--) {
if (i > 0) {
pages.push({
url: handleFilterUrl(params, {page: i}),
num: i,
cur: currentPage === i
});
}
}
pages = _.sortBy(pages, ['num']);
}
let prevPages = [];
let nextPages = [];
if (_.size(pages) === 5) {
if (currentPage > 4) {
prevPages.push({
url: handleFilterUrl(params, {page: 1}),
num: 1
});
prevPages.push({
num: '...'
});
}
if (currentPage < totalPage - 2 && totalPage > 5) {
nextPages.push({
num: '...'
});
nextPages.push({
url: handleFilterUrl(params, {page: totalPage}),
num: totalPage
});
}
}
result.pages = _.concat(prevPages, pages, nextPages);
return result;
};
... ...
... ... @@ -210,7 +210,10 @@ router.get('/comment/order', [getCommonHeader, getHomeNav], commentController.co
// router.get('/vip', VipController.index);
// 我的收藏
router.get('/favorite', [getCommonHeader, getHomeNav], favoriteController.index);
router.get('/favorite', [getCommonHeader, getHomeNav],
favoriteController.udidRequired,
favoriteController.index
);
//
// router.get('/coupons', CouponsController.index);
... ...
{{# pager}}
<div class="me-pager clearfix">
{{#if hasCheckAll}}
<input id="me-checkall" class="check-all" type="checkbox">
全选
<span id="me-del-checked" class="del-checked">删除</span>
{{/if}}
<div class="pull-right">
<span>{{count}}条结果,{{curPage}}/{{totalPages}}</span>
{{#if pager}}
{{#with pager}}
<div class="me-pager clearfix">
{{#if hasCheckAll}}
<input id="me-checkall" class="check-all" type="checkbox">
全选
<span id="me-del-checked" class="del-checked">删除</span>
{{/if}}
<div class="pull-right">
<span>{{count}}条结果,{{curPage}}/{{totalPages}}</span>
<div class="pager-html pager">
{{# prePage}}
<a href="{{url}}" title="上一页"><span class="iconfont">&#xe60e;</span>上一页</a>
{{/ prePage}}
<div class="pager-html pager">
{{# prePage}}
<a href="{{url}}" title="上一页"><span class="iconfont">&#xe60e;</span>上一页</a>
{{/ prePage}}
{{# pages}}
<a{{#if url}} href="{{url}}"{{/if}}{{#if cur}} class="cur"{{/if}}>{{num}}</a>
{{/ pages}}
{{# nextPage}}
<a href="{{url}}" title="下一页">下一页<span class="iconfont">&#xe60c;</span></a>
{{/ nextPage}}
{{# pages}}
<a{{#if url}} href="{{url}}"{{/if}}{{#if cur}} class="cur"{{/if}}>{{num}}</a>
{{/ pages}}
{{# nextPage}}
<a href="{{url}}" title="下一页">下一页<span class="iconfont">&#xe60c;</span></a>
{{/ nextPage}}
</div>
</div>
</div>
</div>
{{/ pager}}
\ No newline at end of file
{{/with}}
{{/if}}
... ...