@@ -8,14 +8,20 @@ var listsService = require('../services/lists.service');
8
8
/**
9
9
* 搜索页
10
10
* @param {Object } req
11
+ * {String} req.query.words
12
+ * {String} req.query.page
11
13
* @param {Object } res
12
14
* @param {Function } next
13
15
*/
14
- module . exports = function ( req , res , next ) {
16
+ module . exports = function ( req , res ) {
15
17
req . checkQuery ( {
16
18
'words' : {
17
19
optional : true ,
18
20
isString : { errorMessage : 'words 需为字符串' }
21
+ } ,
22
+ 'page' : {
23
+ optional : true ,
24
+ isString : { errorMessage : 'page 需为数字' }
19
25
}
20
26
} ) ;
21
27
@@ -30,12 +36,37 @@ module.exports = function (req, res, next) {
30
36
categoriesService . navigation ( { current : '/search' } , callback ) ;
31
37
} ,
32
38
list : function ( callback ) {
33
- listsService . search ( { words : req . query . words } , function ( err , list ) {
39
+ var query = {
40
+ words : req . query . words ,
41
+ pageSize : 15
42
+ } ;
43
+
44
+ if ( req . query . page ) query . currentPage = parseInt ( req . query . page ) ;
45
+
46
+ listsService . search ( query , function ( err , result ) {
34
47
if ( err ) return callback ( err ) ;
35
48
36
- if ( ! list ) return callback ( ) ;
49
+ if ( ! result ) return callback ( ) ;
50
+
51
+ if ( _ . get ( result , 'pagination.length' ) <= 1 ) {
52
+ delete result . pagination
53
+ return callback ( null , result ) ;
54
+ }
55
+
56
+ var pagination = _ . map ( result . pagination , function ( page ) {
57
+ if ( page . index === 1 ) {
58
+ page . url = '/search?words=' + req . query . words ;
59
+ } else {
60
+ page . url = '/search?words=' + req . query . words + '&page=' + page . index ;
61
+ }
62
+
63
+ delete page . index ;
64
+ return page ;
65
+ } ) ;
66
+
67
+ result . pagination = pagination ;
37
68
38
- return callback ( null , list ) ;
69
+ return callback ( null , result ) ;
39
70
} ) ;
40
71
} ,
41
72
readingTotal : function ( callback ) {
0 commit comments