1
- import Controller from './common ' ;
1
+ import Controller from '../core/base_controller ' ;
2
2
const request = require ( 'request' ) ;
3
3
const cheerio = require ( 'cheerio' ) ;
4
4
@@ -9,63 +9,45 @@ enum NAV_STATUS {
9
9
}
10
10
11
11
export default class NavController extends Controller {
12
+ tableName ( ) : string {
13
+ return 'Nav'
14
+ }
15
+
12
16
async list ( ) {
13
17
const { ctx } = this
14
18
const { model } = ctx
15
- try {
16
- let { pageSize = 10 , pageNumber = 1 , status = 0 , categoryId, name } = ctx . query
17
- pageSize = Number ( pageSize )
18
- pageNumber = Number ( pageNumber )
19
- const skipNumber = pageSize * pageNumber - pageSize
19
+ let { status = 0 , categoryId, name } = ctx . query
20
20
21
- let findParam : any = {
22
- status,
23
- }
24
- if ( ! status ) {
25
- findParam = {
26
- $or : [
27
- { status : { $exists : false } } ,
28
- { status : 0 } ,
29
- ]
30
- }
21
+ let findParam : any = {
22
+ status,
23
+ }
24
+ if ( ! status ) {
25
+ findParam = {
26
+ $or : [
27
+ { status : { $exists : false } } ,
28
+ { status : 0 } ,
29
+ ]
31
30
}
32
- if ( categoryId ) {
33
- findParam . categoryId = {
34
- $eq : categoryId
35
- }
31
+ }
32
+ if ( categoryId ) {
33
+ findParam . categoryId = {
34
+ $eq : categoryId
36
35
}
37
- if ( name ) {
38
- let reg = new RegExp ( name , 'i' ) ;
39
- findParam . name = {
40
- $regex : reg
41
- }
36
+ }
37
+ if ( name ) {
38
+ let reg = new RegExp ( name , 'i' ) ;
39
+ findParam . name = {
40
+ $regex : reg
42
41
}
43
-
44
- const [ resData , total ] = await Promise . all ( [
45
- model . Nav . find ( findParam ) . skip ( skipNumber ) . limit ( pageSize ) . sort ( { _id : - 1 } ) ,
46
- model . Nav . find ( findParam ) . count ( ) ,
47
- ] )
48
- this . success ( {
49
- data : resData ,
50
- total,
51
- pageNumber : Math . ceil ( total / pageSize ) ,
52
- } )
53
- } catch ( error ) {
54
- this . error ( error . message )
55
42
}
43
+
44
+ await super . getList ( findParam ) ;
56
45
}
57
46
58
47
async add ( ) {
59
- const { ctx } = this
60
- const { request : req , model } = ctx
61
- try {
62
- req . body . status = NAV_STATUS . wait
63
- req . body . createTime = new Date ( )
64
- const res = await model . Nav . create ( req . body ) ;
65
- this . success ( res )
66
- } catch ( error ) {
67
- this . error ( error . message )
68
- }
48
+ this . ctx . request . body . status = NAV_STATUS . wait
49
+ this . ctx . request . body . createTime = new Date ( )
50
+ await super . add ( )
69
51
}
70
52
71
53
async reptile ( ) {
@@ -94,23 +76,17 @@ export default class NavController extends Controller {
94
76
}
95
77
96
78
async del ( ) {
97
- const { id } = this . ctx . request . body
98
- const res = await this . ctx . service . common . remove ( id , 'Nav' ) ;
99
- this . success ( res )
79
+ await super . remove ( ) ;
100
80
}
101
81
102
82
async edit ( ) {
103
- const { body } = this . ctx . request
104
- body . updateTime = new Date ( )
105
- const res = this . ctx . service . common . update ( body , 'Nav' )
106
- this . success ( res ) ;
83
+ this . ctx . request . body . updateTime = new Date ( )
84
+ await super . update ( ) ;
107
85
}
108
86
109
87
async audit ( ) {
110
- const { body } = this . ctx . request
111
- body . auditTime = new Date ( )
112
- const res = this . ctx . service . common . update ( body , 'Nav' )
113
- this . success ( res ) ;
88
+ this . ctx . request . body . auditTime = new Date ( )
89
+ await super . update ( ) ;
114
90
}
115
91
116
92
/**
@@ -150,21 +126,21 @@ export default class NavController extends Controller {
150
126
151
127
async get ( ) {
152
128
const { ctx } = this
153
- const { id, keyword, limit = 10 } = ctx . query
129
+ const { id, keyword } = ctx . query
154
130
155
131
let res
156
132
157
133
if ( id ) {
158
- res = await ctx . service . common . get ( ctx . query . id , 'Nav' ) ;
134
+ res = await super . get ( ) ;
159
135
} else if ( keyword ) {
160
- res = await ctx . service . nav . find ( keyword , limit ) ;
136
+ let reg = new RegExp ( keyword , 'i' ) ;
137
+ await super . getList ( {
138
+ name : { $regex : reg } ,
139
+ } , ( table ) => table . limit ( 10 ) ) ;
161
140
}
162
- this . success ( res )
163
141
}
164
142
165
143
async random ( ) {
166
- const { ctx } = this
167
- const res = await ctx . service . common . getRandomData ( 10 , 'Nav' ) ;
168
- this . success ( res )
144
+ await super . getRandomList ( ) ;
169
145
}
170
146
}
0 commit comments