Authored by htoooth

add ajax

... ... @@ -226,6 +226,22 @@ const createConsult = (req, res, next) => {
}
};
const recommend = (req, res, next) => {
let skn = req.query.skn || '';
let pageSize = req.query.size || 5;
let pageNum = req.query.num || 1;
if (!skn) {
return res.json({
code: 400,
message: '服务器错误'
});
}
service.recommendAsync(skn, pageNum, pageSize)
.then(result => res.json(result))
.catch(next);
};
module.exports = {
showMain,
... ... @@ -234,7 +250,8 @@ module.exports = {
indexConsult,
createConsult,
productHeader: detailHeader,
detailReturn
detailReturn,
recommend
};
... ...
... ... @@ -104,6 +104,20 @@ const getLimitedProductStatusAsync = (code, uid, skn) => {
return api.get('', params, config.apiCache);
};
/**
*
* @param skn product_skn
* @param sid shop_id
*/
const getShopRecommendAsync = (skn, page, limit) => {
return api.get('', {
method: 'web.product.shopRecommend',
product_skn: skn,
page: page,
limit: limit
});
};
module.exports = {
getProductBannerAsync,
sizeInfoAsync,
... ... @@ -113,5 +127,6 @@ module.exports = {
getProductAsync,
getPromotionAsync,
isSupportReturnedSale,
getLimitedProductStatusAsync
getLimitedProductStatusAsync,
getShopRecommendAsync
};
... ...
... ... @@ -9,6 +9,7 @@
const Promise = require('bluebird');
const co = Promise.coroutine;
const _ = require('lodash');
const Fn = require('lodash/fp');
const url = require('url');
const helpers = global.yoho.helpers;
... ... @@ -1404,6 +1405,57 @@ const showMainAsync = (data) => {
})();
};
const recommendAsync = (skn, page, limit) => {
return co(function * () {
let recommendData = productAPI.getShopRecommendAsync(skn, page, limit);
if (_.get(recommendData, 'code', 400) !== 200) {
return {
code: 400,
message: '服务器错误!'
};
}
const formatPrice = p => ${p}`;
const productUrl = (pid, gid, cn) => helpers.urlFormat(`/product/pro_${pid}_${gid}/${cn}.html`, null, 'item');
const productImageUrl = Fn.pipe(Fn.prop('default_images'), _.partial(helpers.image, _, 150, 200, 2, 70));
let products = _.get(recommendData, 'product_list', []).map(rp => {
let marketPrice = rp.sales_price;
let salePrice = rp.market_price;
let defaultGoods = _.find(rp.goods_list, {is_default: 'Y'});
// 无默认商品取商品列表第一个
if (!defaultGoods) {
defaultGoods = rp.goods_list[0];
}
return {
market_price: marketPrice === salePrice ? '' : formatPrice(helpers.round(marketPrice, 2)),
price: formatPrice(helpers.round(salePrice, 2)),
product_name: rp.product_name,
url: productUrl(rp.product_id, defaultGoods.goods_id, rp.cn_alphabet),
pic_url: productImageUrl(rp)
};
});
return {
code: 200,
data: {
pager: {
total: _.get(recommendData, 'page_total', 0),
size: _.get(recommendData, 'page_size', 5),
num: _.get(recommendData, 'page', 1)
},
products: products,
total: _.get(recommendData, 'total', 0)
}
};
})();
};
module.exports = {
getShareOrderListAsync: commentService.getShareOrderListAsync, // 获取评论列表
indexConsultAsync: consultService.indexAsync, // 获取咨询列表
... ... @@ -1412,5 +1464,6 @@ module.exports = {
indexHotAreaAsync: hotAreaService.indexAsync, // 获取某一个商品的热区数据
saveRecentGoodInCookies, // 保存最近的商品
getDetailHeader,
saleReturn
saleReturn,
recommendAsync
};
... ...
... ... @@ -61,6 +61,7 @@ router.post('/index/favoriteBrand', favorite.changeFavoriteBrand);// 收è—å“ç‰
router.post('/item/togglecollect', favorite.collectProduct); // 收藏商品
router.get('/detail/header', detail.productHeader); // 价格数据重新获取接口
router.get('/detail/return', detail.detailReturn);// 特殊商品退换货
router.get('/detail/recommend', detail.recommend);// 推荐商品
router.get('/index/isfav', favorite.isFavoriteBrand);// 品牌收藏状态
// 搜索
... ...