search.js
13.6 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/**
* search controller
* @author: wsl<shuiling.wang@yoho.cn>
* @date: 2016/7/21
*/
'use strict';
const utils = '../../../utils';
const mRoot = '../models';
const headerModel = require('../../../doraemon/models/header');
const searchModel = require(`${mRoot}/search`);
const _ = require('lodash');
const helpers = global.yoho.helpers;
const searchProcess = require(`${utils}/search-process`);
const productProcess = require(`${utils}/product-process`);
const stringCode = require(`${utils}/string-code`);
const co = require('bluebird').coroutine;
/**
* 搜索落地页
*/
const list = (req, res, next) => {
let params = Object.assign({
isSearch: true, // 搜索列表将最新改成默认的标识
cartUrl: helpers.urlFormat('/cart/index/index')
}, req.query, {query: (req.query.query || '').toString()});
let title = '';
let isQueryFirstClass = false; // 标识用户搜的是不是一级品类
let isQuerySecondClass = false; // 标识用户搜的是不是二级品类
// let domain = null;
let uid = req.user.uid || 0;
if (params.shop_id) {
params.shopId = params.shop_id;
}
params.isApp = req.yoho.isApp;
params.physical_channel = req.yoho.channel && searchProcess.getChannelType(req.yoho.channel);
// 唤起 APP 的路径
res.locals.appPath = `yohobuy://yohobuy.com/goapp?openby:yohobuy={"action":"go.list","params":${JSON.stringify(params)}}`;
return searchModel.searchKeyActivity(params.query || '').then(activityResult => {
let activity = _.get(activityResult, 'urlobj.appUrl', '');
if (activity) {
return res.redirect(activity);
} else {
// 获取第一页数据做服务端渲染
let initialData = _.assign({
type: 'default',
order: '0',
page: 1,
limit: 24
}, params);
if (uid) {
initialData.uid = uid;
}
/* 判断是不是品牌, 是品牌跳到品牌列表页(显示搜索框),判断是不是品类, 是品类加导航标题(不显示搜索框) */
return Promise.all([
searchModel.getBrandDomain(params.query.toLowerCase()),
searchModel.getClassNames(),
searchModel.getSearchData(initialData)
]).then(result => {
// 推荐词条件判断 redmine: 18567
if (result[2].suggestion && result[2].suggestion.isNeedSuggestion &&
result[2].suggestion.termsSuggestion.length) {
params.newquery = result[2].suggestion.termsSuggestion[0].name;
}
if (result[2] && result[2].brandWay) {
params.brandWay = result[2].brandWay;
}
if (params.query) {
// domain = result[0];
// 跳转到品牌商品列表页
// if (domain !== null && !params.shop_id) {
// let urlPro = {
// from: 'search',
// query: params.query
// };
// if (req.query.app_type) {
// urlPro = _.assign(urlPro, {
// app_type: req.query.app_type
// });
// }
// let url = helpers.urlFormat('', urlPro, domain);
// return res.redirect(url);
// }
// 品类名称为空时跳出
if (!result[1]) {
return;
}
_.forEach(result[1].first, (obj) => {
// 精确查一级品类
if (obj === params.query) {
isQueryFirstClass = true;
return false;
}
});
_.forEach(result[1].second, (obj) => {
// 精确查二级品类
if (obj === params.query) {
isQuerySecondClass = true;
return false;
}
});
} else {
params.query = '';
}
// 搜索是一级品类
if (isQueryFirstClass) {
title = '全部' + params.query;
} else if (isQuerySecondClass) { // 搜索是二级品类
title = params.query;
} else { // 搜索其它内容
if (params.query || params.form) {
params.search = {
default: params.query === '' ? false : params.query,
url: helpers.urlFormat('', null, 'search')
};
}
title = '搜索';
}
title = params.title ? params.title : title;
res.render('search/list', {
_noLazy: true,
module: 'product',
page: 'search-list',
pageHeader: headerModel.setNav({
navTitle: title
}),
title: title,
goodList: params,
firstPageGoods: result[2] || [],
suggestion: result[2].suggestion || [],
pageFooter: true,
shopId: params.shop_id || ''
});
}).catch(next);
}
}).catch(next);
};
/**
* 搜索主页
*/
const index = (req, res, next) => {
let title = '搜索',
uid = req.user.uid || 0;
((render) => {
if (_.get(req, 'app.locals.wap.search.removeHotSearch', false)) {
render([]);
} else {
searchModel.getSearchIndex({
gender: req.yoho.channel || 'boys',
uid: uid
}).then((result) => {
render(result);
}).catch(next);
}
})((result) => {
res.render('search/index', Object.assign({
module: 'product',
page: 'search-index',
pageHeader: headerModel.setNav({
navTitle: title
}),
pageFooter: true,
width750: true,
search: {
defaultTerms: (result && result.defaultTerms && result.defaultTerms.length) ?
result.defaultTerms[0].content : '',
url: helpers.urlFormat('', null, 'search'),
hotTerms: result.hotTerms,
wantTerms: result.guessTerms
}
}, searchProcess.getListSeoData()));
});
};
/**
* 联动搜索
*/
let fuzzyDatas = (req, res, next) => {
let params = req.query.keyword;
searchModel.getFuzzyDatas(params).then((result) => {
res.json(result);
}).catch(next);
};
/**
* ajax 商品数据请求
*/
const search = (req, res, next) => {
let allowOrigin = _.get(req, 'headers.origin', null) ?
req.headers.origin : req.protocol + '://' + req.headers.host;
res.setHeader('Access-Control-Allow-Origin', allowOrigin);
res.setHeader('Access-Control-Allow-Credentials', 'true');
let params = Object.assign({}, req.query);
let uid = req.user.uid || 0;
if (uid) {
params.uid = uid;
}
params.isApp = req.yoho.isApp;
params.limit = 24;
searchModel.getSearchData(params).then((result) => {
if (result.list && result.list.length > 0) {
res.render('search/page', {
layout: false,
new: result.list,
suggestion: result.suggestion || [],
total: result.total,
_noLazy: params.noLazy || false
});
} else {
res.json(result);
}
}).catch(next);
};
/**
* 筛选
*/
let filter = (req, res, next) => {
let allowOrigin = _.get(req, 'headers.origin', null) ?
req.headers.origin : req.protocol + '://' + req.headers.host;
res.setHeader('Access-Control-Allow-Origin', allowOrigin);
res.setHeader('Access-Control-Allow-Credentials', 'true');
let params = Object.assign({}, req.query);
let uid = req.user.uid || 0;
if (uid) {
params.uid = uid;
}
searchModel.getFilterData(params).then((result) => {
res.render('search/filter', {
layout: false,
params: params,
filter: result
});
}).catch(next);
};
// 关键词页
const keyword = (req, res, next) => {
let queryKey = stringCode.hexToUtf8(`${req.params.query}`);
let params = {
isSearch: true, // 搜索列表将最新改成默认的标识
cartUrl: helpers.urlFormat('/cart/index/index'),
query: queryKey
};
params.isApp = req.yoho.isApp;
params.physical_channel = req.yoho.channel && searchProcess.getChannelType(req.yoho.channel);
// 唤起 APP 的路径
res.locals.appPath = `yohobuy://yohobuy.com/goapp?openby:yohobuy={"action":"go.list","params":${JSON.stringify(params)}}`;
return searchModel.getSearchKeywordData(params, req.user.uid).then(result => {
res.render('search/list', {
_noLazy: true,
module: 'product',
page: 'search-list',
pageHeader: headerModel.setNav({
navTitle: queryKey
}),
goodList: params,
firstPageGoods: result || [],
fuzzyWord: result.fuzzyWord,
title: `${queryKey}价格_图片_品牌_怎么样-YOHO!BUY有货`,
keywords: `${queryKey},${queryKey}价格,${queryKey}图片,${queryKey}怎么样,${queryKey}品牌,YOHO!BUY有货`,
description: `YOHO!BUY有货网yohobuy.com是国内专业的${queryKey}网上潮流购物商城,为您找到${_.get(result,
'total', 0)}条${queryKey}、产品的详细参数,实时报价,价格行情,图片、评价、品牌等信息。买${queryKey},就上YOHO!BUY有货`,
pageFooter: true
});
}).catch(next);
};
// 关键词页with id
const keyId = (req, res, next) => {
let params = {
isSearch: true, // 搜索列表将最新改成默认的标识
cartUrl: helpers.urlFormat('/cart/index/index')
};
params.isApp = req.yoho.isApp;
params.physical_channel = req.yoho.channel && searchProcess.getChannelType(req.yoho.channel);
return searchModel.getSearchKeywordDataById(req.params.id, params, req.user.uid).then(result => {
let queryKey = result.queryKey;
if (!result) {
return next();
}
// 唤起 APP 的路径
res.locals.appPath = `yohobuy://yohobuy.com/goapp?openby:yohobuy={"action":"go.list","params":${JSON.stringify(params)}}`;
res.render('search/list', {
_noLazy: true,
module: 'product',
page: 'search-list',
pageHeader: headerModel.setNav({
navTitle: queryKey
}),
goodList: params,
firstPageGoods: result || [],
fuzzyWord: result.fuzzyWord,
title: `${queryKey}价格_图片_品牌_怎么样-YOHO!BUY有货`,
keywords: `${queryKey},${queryKey}价格,${queryKey}图片,${queryKey}怎么样,${queryKey}品牌,YOHO!BUY有货`,
description: `YOHO!BUY有货网yohobuy.com是国内专业的${queryKey}网上潮流购物商城,为您找到${_.get(result,
'total', 0)}条${queryKey}、产品的详细参数,实时报价,价格行情,图片、评价、品牌等信息。买${queryKey},就上YOHO!BUY有货`,
pageFooter: true
});
}).catch(next);
};
/**
* 搜索品牌下的商品
*/
const searchBrandGoods = (req, res, next) => {
let allowOrigin = _.get(req, 'headers.origin', null) ?
req.headers.origin : req.protocol + '://' + req.headers.host;
res.setHeader('Access-Control-Allow-Origin', allowOrigin);
res.setHeader('Access-Control-Allow-Credentials', 'true');
co(function* () {
let goodListApi = yield searchModel.getBrandGoods(req.query);
let goodsData = _.get(goodListApi, 'data', []);
let goodsList = productProcess.processProductList(_.get(goodListApi, 'data.product_list', []));
if (goodsList) {
res.render('search/goods-list-ajax', {
layout: false,
goodsList: goodsList,
_noLazy: req.query.noLazy || false
});
} else {
res.json(goodsData);
}
})().catch(next);
};
/**
* 搜索店铺下的商品
*/
const searchShopGoods = (req, res, next) => {
let allowOrigin = _.get(req, 'headers.origin', null) ?
req.headers.origin : req.protocol + '://' + req.headers.host;
res.setHeader('Access-Control-Allow-Origin', allowOrigin);
res.setHeader('Access-Control-Allow-Credentials', 'true');
co(function* () {
let goodListApi = yield searchModel.getShopGoods(req.query);
let goodsData = _.get(goodListApi, 'data', []);
let goodsList = productProcess.processProductList(_.get(goodListApi, 'data.product_list', []));
if (goodsList) {
res.render('search/goods-list-ajax', {
layout: false,
goodsList: goodsList,
_noLazy: req.query.noLazy || false
});
} else {
res.json(goodsData);
}
})().catch(next);
};
module.exports = {
list,
filter,
search,
index,
fuzzyDatas,
keyword,
keyId,
searchBrandGoods,
searchShopGoods
};