22
33const {
44 LinRouter,
5- paginate,
65 routeMetaInfo,
76 adminRequired,
87 Success,
9- ParametersException,
108 NotFound,
119 Failed
1210} = require ( "lin-mizar" ) ;
1311
14- const { has, set, get, toSafeInteger, isInteger } = require ( "lodash" ) ;
12+ const { has, set } = require ( "lodash" ) ;
13+
1514const {
15+ DispatchAuthsValidator,
16+ RemoveAuthsValidator,
17+ UpdateGroupValidator,
1618 ResetPasswordValidator,
19+ AdminUsersValidator,
1720 UpdateUserInfoValidator,
1821 NewGroupValidator,
19- UpdateGroupValidator,
20- DispatchAuthValidator,
21- DispatchAuthsValidator,
22- RemoveAuthsValidator
23- } = require ( "../../validators/cms" ) ;
24- const { getSafeParamId } = require ( "../../libs/util" ) ;
22+ DispatchAuthValidator
23+ } = require ( "../../validators/admin" ) ;
24+
25+ const {
26+ PositiveIdValidator,
27+ PaginateValidator
28+ } = require ( "../../validators/common" ) ;
29+
2530const { AdminDao } = require ( "../../dao/admin" ) ;
2631
2732const admin = new LinRouter ( {
@@ -65,13 +70,12 @@ admin.linGet(
6570 } ,
6671 adminRequired ,
6772 async ctx => {
68- const groupId = get ( ctx . request . query , "group_id" ) ;
69- const { start, count } = paginate ( ctx ) ;
73+ const v = await new AdminUsersValidator ( ) . validate ( ctx ) ;
7074 const { users, total } = await adminDao . getUsers (
7175 ctx ,
72- groupId ,
73- start ,
74- count
76+ v . get ( "query.group_id" ) ,
77+ v . get ( "query. start" ) ,
78+ v . get ( "query. count" )
7579 ) ;
7680 ctx . json ( {
7781 collection : users ,
@@ -92,13 +96,7 @@ admin.linPut(
9296 adminRequired ,
9397 async ctx => {
9498 const v = await new ResetPasswordValidator ( ) . validate ( ctx ) ;
95- const id = toSafeInteger ( get ( ctx . params , "id" ) ) ;
96- if ( ! isInteger ( id ) ) {
97- throw new ParametersException ( {
98- msg : "路由参数错误"
99- } ) ;
100- }
101- await adminDao . changeUserPassword ( ctx , v , id ) ;
99+ await adminDao . changeUserPassword ( ctx , v ) ;
102100 ctx . json (
103101 new Success ( {
104102 msg : "密码修改成功"
@@ -117,12 +115,8 @@ admin.linDelete(
117115 } ,
118116 adminRequired ,
119117 async ctx => {
120- const id = toSafeInteger ( get ( ctx . params , "id" ) ) ;
121- if ( ! isInteger ( id ) ) {
122- throw new ParametersException ( {
123- msg : "路由参数错误"
124- } ) ;
125- }
118+ const v = await new PositiveIdValidator ( ) . validate ( ctx ) ;
119+ const id = v . get ( "path.id" ) ;
126120 await adminDao . deleteUser ( ctx , id ) ;
127121 ctx . json (
128122 new Success ( {
@@ -143,13 +137,7 @@ admin.linPut(
143137 adminRequired ,
144138 async ctx => {
145139 const v = await new UpdateUserInfoValidator ( ) . validate ( ctx ) ;
146- const id = toSafeInteger ( get ( ctx . params , "id" ) ) ;
147- if ( ! isInteger ( id ) ) {
148- throw new ParametersException ( {
149- msg : "路由参数错误"
150- } ) ;
151- }
152- await adminDao . updateUserInfo ( ctx , v , id ) ;
140+ await adminDao . updateUserInfo ( ctx , v ) ;
153141 ctx . json (
154142 new Success ( {
155143 msg : "操作成功"
@@ -168,8 +156,12 @@ admin.linGet(
168156 } ,
169157 adminRequired ,
170158 async ctx => {
171- const { start, count } = paginate ( ctx ) ;
172- const { groups, total } = await adminDao . getGroups ( ctx , start , count ) ;
159+ const v = await new PaginateValidator ( ) . validate ( ctx ) ;
160+ const { groups, total } = await adminDao . getGroups (
161+ ctx ,
162+ v . get ( "query.start" ) ,
163+ v . get ( "query.count" )
164+ ) ;
173165 if ( total < 1 ) {
174166 throw new NotFound ( {
175167 msg : "未找到任何权限组"
@@ -212,13 +204,8 @@ admin.linGet(
212204 } ,
213205 adminRequired ,
214206 async ctx => {
215- const id = toSafeInteger ( get ( ctx . params , "id" ) ) ;
216- if ( ! isInteger ( id ) ) {
217- throw new ParametersException ( {
218- msg : "路由参数错误"
219- } ) ;
220- }
221- const group = await adminDao . getGroup ( ctx , id ) ;
207+ const v = await new PositiveIdValidator ( ) . validate ( ctx ) ;
208+ const group = await adminDao . getGroup ( ctx , v . get ( "path.id" ) ) ;
222209 ctx . json ( group ) ;
223210 }
224211) ;
@@ -262,8 +249,7 @@ admin.linPut(
262249 adminRequired ,
263250 async ctx => {
264251 const v = await new UpdateGroupValidator ( ) . validate ( ctx ) ;
265- const id = getSafeParamId ( ctx ) ;
266- await adminDao . updateGroup ( ctx , v , id ) ;
252+ await adminDao . updateGroup ( ctx , v ) ;
267253 ctx . json (
268254 new Success ( {
269255 msg : "更新分组成功"
@@ -282,7 +268,8 @@ admin.linDelete(
282268 } ,
283269 adminRequired ,
284270 async ctx => {
285- const id = getSafeParamId ( ctx ) ;
271+ const v = await new PositiveIdValidator ( ) . validate ( ctx ) ;
272+ const id = v . get ( "path.id" ) ;
286273 await adminDao . deleteGroup ( ctx , id ) ;
287274 ctx . json (
288275 new Success ( {
0 commit comments