search.js
2.16 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
/**
* search model
* @author: wsl<shuiling.wang@yoho.cn>
* @date: 2016/07/21
*/
'use strict';
const utils = '../../../utils';
const logger = global.yoho.logger;
const productProcess = require(`${utils}/product-process`);
const api = global.yoho.API;
/**
* 排序转换
*/
const typeCont = {
price: ['s_p_desc', 's_p_asc'],
discount: ['p_d_desc', 'p_d_asc'],
sale: ['s_n_desc', 's_n_asc'],
newest: ['s_t_desc', 's_t_asc'],
stock: ['s_s_desc', 's_s_asc'],
all: ['d_s_desc', 'd_s_asc'],
category: ['s_t_desc', 's_t_asc']
};
/**
* 商品搜索接口请求
* @param {[object]} params
* @return {[array]}
*/
const _searchGoods = (params) => {
let method = 'app.search.li';
// 排除基本筛选项默认值为0的对象
for (let str in params) {
if (str !== 'order' && params[str] === '0' || params[str] === null) {
delete params[str];
}
}
// params.yh_channel = channelType[params.yh_channel];
params = Object.assign({
limit: '60',
status: 1,
sales: 'Y',
stocknumber: 1,
attribute_not: 2
}, params);
if (typeCont[params.type]) {
params.order = typeCont[params.type][params.order];
}
return api.get('', Object.assign({
method: method
}, params), {
cache: true
});
};
/**
* 获取商品数据
*/
const getSearchData = (params) => {
return _searchGoods(params).then((result) => {
if (result && result.code === 200) {
return productProcess.processProductList(result.data.product_list || []);
} else {
logger.error('get product search api return code is not 200');
return [];
}
});
};
/**
* 获取筛选数据
* @param {[object]} params
* @return {[array]}
*/
const getFilterData = (params) => {
return _searchGoods(params).then((result) => {
if (result && result.code === 200) {
return productProcess.processFilter(result.data.filter || []);
} else {
logger.error('get filter data api return code is not 200');
return [];
}
});
};
module.exports = {
getSearchData,
getFilterData
};