-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathsearch.js
45 lines (41 loc) · 1.06 KB
/
search.js
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
var tuerBase = require('../model/base'),
config = require('../lib/config'),
util = require('../lib/util'),
EventProxy = require('eventproxy').EventProxy;
var index = function(req, res, next) {
var q = decodeURIComponent(encodeURI(req.query.q)).trim(),
nothingMsg = [{
title: '没有找到相关日记'
}],
proxy = new EventProxy(),
render = function(diaryList) {
req.session.title = '搜索';
req.session.template = 'search';
res.render('search/search',{
config:config,
session:req.session,
result:diaryList
});
};
proxy.assign('findDiary', render);
if (q) {
var qstr = util.replaceReg(q);
searchReg = new RegExp('.*' + qstr + '|' + qstr + '.*', 'i');
tuerBase.findDiaryBy({
title: searchReg
},
'diary', 10, function(err, diaryList) {
if (err) throw err;
else {
if (diaryList.length) {
proxy.trigger('findDiary', diaryList);
} else {
proxy.trigger('findDiary', nothingMsg);
}
}
});
} else {
proxy.trigger('findDiary', nothingMsg);
}
};
exports.index = index;