search.js
5.31 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
/**
* 商品搜索controller
* @author: sefon
* @date: 2016-7-12 10:09:25
*/
'use strict';
const mRoot = '../models';
const search = require(`${mRoot}/search`);
const _ = require('lodash');
const helpers = global.yoho.helpers;
const config = global.yoho.config;
/**
* search 首页
* @param {[type]} req [description]
* @param {[type]} res [description]
* @return {[type]} [description]
*/
const index = (req, res, next) => {
let params = req.query;
let resData = {
title: '潮流商品搜索 | YOHO!BUY 有货',
keywords: 'Yoho! 有货,潮流,时尚,流行,购物,B2C,正品,购物网站,网上购物,货到付款,' +
'品牌服饰,男士护肤,黑框眼镜,匡威,板鞋,i.t,izzue,5cm,eastpak,vans,lylescott,g-shock,' +
'new balance,lacoste,melissa,casio,卡西欧手表,舒雅,jasonwood,odm,AAAA,香港购物,日本潮流',
description: '潮流商品搜索,上衣,衬衫,TEE,卫衣,冲锋衣,风衣,羽绒服,裤子,休闲鞋,板鞋,配饰,复古眼镜'
};
search.getKeyActivity(params.query).then(activityUrl => {
if (activityUrl) {
activityUrl = encodeURI(activityUrl);
res.redirect(activityUrl);
return;
}
return search.getSearchData(Object.assign(params, {uid: req.user.uid}), req.yoho.channel).then(result => {
Object.assign(resData, result);
if (!_.get(resData, 'search.goods') || !_.get(resData, 'search.goods').length) {
_.set(resData, 'search.keyWord', req.query.query);
return res.render('search/no-result', resData);
}
res.render('search/index', resData);
});
}).catch(next);
};
/**
* search 提示
* @param {[type]} req [description]
* @param {[type]} res [description]
* @return {[type]} [description]
*/
const suggest = (req, res, next) => {
search.getSuggest(req.query).then(result => {
let dest = {
code: 200,
message: 'suggest',
data: result || ''
};
res.type('text/javascript');
res.send(req.query.callback + '(' + JSON.stringify(dest) + ')');
}).catch(next);
};
const suggest4Old = (req, res, next) => {
search.getSuggest(Object.assign({}, req.query, { keyword: req.query.query })).then(result => {
let dest = {
code: 200,
message: 'suggest'
};
let data = [];
if (result) {
for (let it of result) {
let item = '<li>' +
`<a style="display: block;" href="${it.href}" class="clearfix clear search-item" title="${it.keyword}" act="${it.href}">` + // eslint-disable-line
`<span class="searchvalue" >${it.keyword}</span><span class="valuenum">约${it.count}个商品</span>` + // eslint-disable-line
'</a>' +
'</li>';
data.push(item);
}
}
dest.data = data;
res.send(req.query.callback + '(' + JSON.stringify(dest) + ')');
}).catch(next);
};
const serachFilterBrands = (req, res, next) => {
search.getBrands4Filter(Object.assign({}, req.query, {
keyword: req.query.query || ''
})).then(result => {
let dest = {
code: 200,
data: result || ''
};
res.type('text/javascript');
res.send(req.query.callback + '(' + JSON.stringify(dest) + ')');
}).catch(next);
};
/**
* search 历史记录提示
* @param {[type]} req [description]
* @param {[type]} res [description]
* @return {[type]} [description]
*/
const searchHistory = (req, res) => {
let history = _.trim(req.cookies['_History']); //eslint-disable-line
let q = _.trim(decodeURIComponent(req.query.query) || '');
let data = [];
let ResData = {};
history = history ? history.split(',') : [];
history = _.reject(history, old => old === q ? true : false);
if (q !== '') {
history.unshift(q);
res.cookie('_History', _.take(history, 9).join(','), {
domain: config.cookieDomain,
maxAge: 60 * 60 * 24 * 1000 * 30 // 一月
});
}
_.forEach(history, function(item) {
let list = {};
list['keyword'] = item; //eslint-disable-line
list['href'] = helpers.urlFormat('', {query: list['keyword']}, 'search'); //eslint-disable-line
data.push(list);
});
ResData['data'] = data; //eslint-disable-line
res.type('text/javascript');
res.send(req.query.callback + '(' + JSON.stringify(ResData) + ')');
};
/**
* search 热门搜索和默认搜索
* @param {[type]} req [description]
* @param {[type]} res [description]
* @return {[type]} [description]
*/
const searchRecommend = (req, res, next) => {
search.getSearchRecommend(req.yoho.channel, req.query).then(result => {
if (req.query.json === '1') {
return res.send(result);
}
let dest = {
code: 200,
message: 'recommend',
data: result || ''
};
res.type('text/javascript');
res.send(req.query.callback + '(' + JSON.stringify(dest) + ')');
}).catch(next);
};
module.exports = {
index,
suggest,
suggest4Old,
serachFilterBrands,
searchHistory,
searchRecommend
};