recent-view.js
2.41 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
98
99
/**
* recent view model
* @author: xuqi<qi.xu@yoho.cn>
* @date: 2016/10/11
*/
'use strict';
const _ = require('lodash');
const api = global.yoho.API;
const helper = global.yoho.helpers;
/**
* 处理商品
*/
const _handleProduct = (products) => {
return products.map((product) => {
return {
href: helper.getUrlBySkc(product.product_skn),
thumb: product.default_images,
name: product.product_name,
price: product.sales_price,
productId: product.product_id
};
});
};
const index = (skn, limit) => {
return api.get('', {
method: 'h5.product.batch',
productSkn: skn,
limit: limit
}).then(result => {
if (result.code === 200) {
let data = [];
let historyProduct = result.data.product_list;
_.forEach(historyProduct, hp => {
if (!hp) {
return;
}
let mp = hp.market_price;
let sp = hp.sales_price;
let defaultGoods = _.find(hp.goods_list, {is_default: 'Y'});
// 无默认商品取商品列表第一个
if (!defaultGoods) {
defaultGoods = hp.goods_list[0];
}
data.push({
market_price: mp === sp ? '' : `¥${helper.round(mp, 2)}`,
price: `¥${helper.round(sp, 2)}`,
product_name: hp.product_name,
url: helper.getUrlBySkc(hp.product_skn),
pic_url: helper.image(defaultGoods.images_url, 280, 382, 2, 70)
});
});
return {
code: 200,
data: data,
message: result.message
};
} else {
// get list error
return {
code: result.code,
message: result.message,
data: []
};
}
});
};
/**
* [为你优选]
* @param {[type]} params [(yh_channel、uid、udid、rec_pos、limit)]
* @return {[type]} [{}]
*/
const recommend = (params) => {
params.limit = params.limit || 30;
return api.get('', Object.assign({
method: 'app.home.newPreference'
}, params)).then(d => {
return _handleProduct(_.get(d, 'data.product_list', []));
});
};
module.exports = {
index,
recommend
};