search.js
7.63 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/**
* search
* @Author: sefon 2016-7-12 16:31:56
*/
'use strict';
const utils = '../../../utils';
const api = global.yoho.API;
const helpers = global.yoho.helpers;
const saleApi = require('./sale-api');
const searchApi = require('./search-api');
const headerModel = require('../../../doraemon/models/header');
const productProcess = require(`${utils}/product-process`);
const searchHandler = require('./search-handler');
const _ = require('lodash');
const needParams = ['query', 'msort', 'misort'];
exports.getKeyActivity = (query) => {
return searchApi.getKeyActivityAsync(query).then(result => {
return _.get(result, 'data.urlobj.pcUrl', '');
});
};
/**
* 获取搜索数据
* @param {[type]} origin [description]
* @return {[type]} [description]
*/
exports.getSearchData = (params, channel) => {
let searchParams = searchHandler.getSearchParams(params);
switch (channel) {
case 'boys':
searchParams.physical_channel = 1;
break;
case 'girls':
searchParams.physical_channel = 2;
break;
case 'kids':
searchParams.physical_channel = 3;
break;
case 'lifestyle':
searchParams.physical_channel = 4;
break;
default:
break;
}
// 调用接口
let apiMethod = [
headerModel.requestHeaderData(channel),
searchApi.getSortList(Object.assign({}, searchParams, {msort: '', misort: '', sort: ''})),
searchApi.getProductList(searchParams),
searchApi.getShopList(params)
];
return api.all(apiMethod).then(result => {
let finalResult = {
headerData: Object.assign(result[0].headerData, {
header: true
}),
search: {}
};
// 获取左侧类目数据
if (result[1].code === 200) {
let dps = {};
_.forEach(needParams, (value) => {
if (params[value]) {
dps[value] = params[value];
}
});
finalResult.search = {
leftContent: searchHandler.handleSortData(result[1].data.sort, dps, params)
};
}
// 获取商品数据和顶部筛选条件
if (result[2].code === 200) {
let filters = Object.assign(searchHandler.handleFilterDataAll(result[2].data, params),
finalResult.search.leftContent.sort);
filters.checkedConditions.conditions = _.concat(filters.checkedConditions.conditions,
finalResult.search.leftContent.checked);
Object.assign(finalResult.search,
searchHandler.handlePathNavData({total: result[2].data.total }, params, 'search', channel),
{
filters: filters,
opts: searchHandler.handleOptsData(params, result[2].data.total, result[2].data.filter),
totalCount: result[2].data.total,
footPager: searchHandler.handlePagerData(result[2].data.total, params),
goods: productProcess.processProductList(result[2].data.product_list,
Object.assign({showDiscount: false, from: {type: 'search', params: params}}, params)),
latestWalk: 6,
hasNextPage: searchHandler.handleNextPage(params, result[2].data.total),
shopEntry: result[3]
});
finalResult.criteo = {skn: searchHandler.getCriteo(_.get(finalResult.search, 'goods'))};
}
return finalResult;
});
};
/**
* 搜索提示
*/
exports.getSuggest = (params) => {
return searchApi.getSuggest(params).then(result => {
let dest = [];
if (result.code === 200) {
dest = searchHandler.handleSuggest(result.data);
}
return dest;
});
};
exports.getListBrandsFilter = (params, nparams) => {
return searchApi.getBrands4Filter(Object.assign({}, params, nparams)).then(result => {
let dest = [];
if (result.code === 200) {
dest = searchHandler.handleFilterBrands(result.data, params);
}
return dest;
});
};
exports.getBrands4Filter = (params) => {
let nparams = {};
if (params && params.price) {
let mp = params.price.split(',');
let nmp = [];
nmp[0] = (mp && mp[0]) || 0;
nmp[1] = (mp && mp[1]) || 99999;
nparams.price = nmp.join(',');
}
if (params && params.from) {
nparams.from = params.from;
delete params.from;
}
// 生成路径包含callback 导致多次调用多个 callback问题
if (params && params.callback) {
delete params.callback;
}
if (params.specialsale_id) {
return saleApi.getSaleSpecialAsync(params.specialsale_id).then(special => {
if (special && special.data) {
let data = special.data;
// 传品牌ID参数
if (data.brand_id) {
nparams.brand = data.brand_id;
}
// 传促销id,促销id为空时传专区id
if (data.ispromotion) {
nparams.promotion = data.ispromotion;
}
}
return this.getListBrandsFilter(params, nparams);
});
}
// 商品详情页促销跳转
if (params && params.psp_id) {
Object.assign(nparams, {
promotion_id: params.psp_id,
method: 'app.promotion.aggBrand'
});
}
return this.getListBrandsFilter(params, nparams);
};
/**
* 搜索 历史记录提示
*/
// exports.getSearchHistory = (params) => {
// return searchApi.getSearchHistory(params).then(result => {
// let resData = [];
//
// if (result.code === 200) {
// resData = searchHandler.handleSuggest(result.data);
// }
//
// return resData;
// });
// };
/**
* 搜索 热门搜索和默认搜索
* @param {[type]} origin [description]
* @return {[type]} [description]
*/
exports.getSearchRecommend = (params) => {
return searchApi.getSearchRecommend(params).then(result => {
// console.log(result);
if (result.code === 200) {
let resData = {
// defaultTerms: [],
hotTerms: [],
// guessTerms: []
};
// resData.defaultTerms = _.map(result.data.defaultTerms, function(value) {
// return {
// href: helpers.urlFormat('', {query: value.url}, 'search'),
// content: value.content,
// sort: value.sort,
// status: value.status,
// type: value.type,
// };
// });
console.log(result.data.hotTerms);
resData.hotTerms = _.map(result.data.hotTerms, function(value) {
return {
href: helpers.urlFormat('', {query: value.content}, 'search'),
content: value.content,
sort: value.sort,
status: value.status,
type: value.type,
};
});
// resData.guessTerms = _.map(result.data.guessTerms, function(value) {
// return {
// href: helpers.urlFormat('', {query: value.url}, 'search'),
// content: value.content,
// sort: value.sort,
// status: value.status,
// type: value.type,
// };
// });
return resData;
}else{
return result;
}
});
};