search.js
1.91 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
/**
* 商品搜索controller
* @author: sefon
* @date: 2016-7-12 10:09:25
*/
'use strict';
const mRoot = '../models';
const search = require(`${mRoot}/search`);
/**
* search 首页
* @param {[type]} req [description]
* @param {[type]} res [description]
* @return {[type]} [description]
*/
exports.index = (req, res, next) => {
let resData = {};
search.getSearchData(req.query, req.yoho.channel).then(result => {
Object.assign(resData, result);
res.render('search/index', resData);
}).catch(next);
};
/**
* search 提示
* @param {[type]} req [description]
* @param {[type]} res [description]
* @return {[type]} [description]
*/
exports.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);
};
exports.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);
};