search.js
3.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
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
/**
* search controller
* @author: wsl<shuiling.wang@yoho.cn>
* @date: 2016/7/21
*/
'use strict';
const mRoot = '../models';
const headerModel = require('../../../doraemon/models/header');
const searchModel = require(`${mRoot}/search`);
const _ = require('lodash');
const helpers = global.yoho.helpers;
const list = (req, res, next) => {
let params = Object.assign({}, req.query);
let title = '';
let query = req.query.query;
let isQueryFirstClass = false; // 标识用户搜的是不是一级品类
let isQuerySecondClass = false; // 标识用户搜的是不是二级品类
let domain = null;
params = _.assign({
isSearch: true,
cartUrl: helpers.urlFormat('/cart/index/index')
}, params);
/* 判断是不是品牌, 是品牌跳到品牌列表页(显示搜索框),判断是不是品类, 是品类加导航标题(不显示搜索框) */
Promise.all([searchModel.getAllBrandNames(), searchModel.getClassNames()]).then((result) => {
if (query) {
_.forEach(result[0], function(obj) {
if (query === obj.brandDomain) { // 精确查品牌域名
domain = query;
return false;
}
if (query === obj.brandName) { // 精确查品牌名称
domain = obj.brandName;
return false;
}
if (obj.brandDomain.indexOf(query) > 0) { // 模糊查品牌域名
domain = obj.brandDomain;
return false;
}
});
// 跳转到品牌商品列表页
if (domain !== null && !params.shop_id) {
let url = helpers.urlFormat('', {
from: 'search',
query: query
}, domain);
res.redirect(url);
}
// 品类名称为空时跳出
if (!result[1]) {
return;
}
_.forEach(result[1].first, (obj) => {
// 精确查一级品类
if (obj === query) {
isQueryFirstClass = true;
return false;
}
});
_.forEach(result[1].second, (obj) => {
// 精确查二级品类
if (obj === query) {
isQuerySecondClass = true;
return false;
}
});
} else {
params.query = '';
}
// 搜索是一级品类
if (isQueryFirstClass) {
title = '全部' + query;
} else if (isQuerySecondClass) { // 搜索是二级品类
title = query;
} else { // 搜索其它内容
if (query) {
params.search = {
default: query === '' ? false : query,
url: ''
};
}
title = '搜索';
}
res.render('search/list', {
module: 'product',
page: 'search-list',
pageHeader: headerModel.setNav({
navTitle: title
}),
goodList: params,
pageFooter: true
});
}).catch(next);
};
const search = (req, res, next) => {
let params = Object.assign({}, req.query);
searchModel.getSearchData(params).then((result) => {
res.render('search/page', {
layout: false,
new: result
});
}).catch(next);
};
/**
* 筛选
* @param {[object]} req
* @param {[object]} res
* @return {[type]}
*/
let filter = (req, res, next) => {
let params = Object.assign({}, req.query);
searchModel.getFilterData(params).then((result) => {
res.render('search/filter', {
layout: false,
params: params,
filter: result
});
}).catch(next);
};
module.exports = {
list,
filter,
search
};