recommend.js
2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/**
* 推荐商品(店铺推荐|为您优选|最近浏览)model
* @author: wsl<shuiling.wang@yoho.cn>
* @date: 2016/11/21
*/
'use strict';
const api = global.yoho.API;
const _ = require('lodash');
const config = global.yoho.config;
/**
* 根据性别来决定 默认图片获取字段 如果是 2、3
*
* 则优先从cover_2 --》 cover_1 -- 》 images_url
* 否则优先从cover_1 --》 cover_2 -- 》 images_url
*
*/
const _procProductImg = (product) => {
if (product.gender === '2,3' || product.gender === '2' || product.gender === '3') {
return product.cover_2 || product.images_url || product.cover_1 || '';
}
return product.cover_1 || product.images_url || product.cover_2 || '';
};
const _processProduct = (list) => {
let newRes = [];
list = list || [];
list.forEach(item => {
let defaultGoods = _.find(item.goods_list, {is_default: 'Y'});
// 无默认商品取商品列表第一个
if (!defaultGoods) {
defaultGoods = item.goods_list[0];
}
item.default_images = _procProductImg(defaultGoods);
item.goodsUrl = `${config.siteUrl}/product/pro_${item.product_id}_${defaultGoods.goods_id}/${item.cn_alphabet}.html`; // eslint-disable-line
});
_.chunk(list, 5).forEach(item => {
newRes.push({
list: item
});
});
return newRes;
};
const getRecommendProduct = (params) => {
return api.get('', _.assign({
method: 'app.home.newPreference',
limit: 20,
page: 1,
rec_pos: 100003
}, params), {
code: 200,
catch: true
}).then(data => {
return _processProduct(data.data.product_list);
});
};
const getRecommendShop = (productSkn) => {
return api.get('', {
method: 'web.product.shopRecommend',
limit: 20,
page: 1,
product_skn: productSkn
}, {
code: 200
}).then(data => {
return _processProduct(data.data.product_list);
});
};
const recentPreview = (skn, limit) => {
return api.get('', {
method: 'h5.product.batch',
productSkn: skn,
limit: limit,
page: 1
}, {
code: 200
}).then(data => {
return _processProduct(data.data.product_list);
});
};
module.exports = {
getRecommendProduct,
getRecommendShop,
recentPreview
};