Skip to content

Commit 43d4cc8

Browse files
committed
feat(*): 优化 permission 设置方式, 添加 code-message 映射
1 parent 73c284b commit 43d4cc8

34 files changed

+847
-601
lines changed

app/api/cms/admin.js

Lines changed: 29 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,22 @@ import {
99
DispatchPermissionsValidator,
1010
RemovePermissionsValidator
1111
} from '../../validator/admin';
12-
import {
13-
PositiveIdValidator,
14-
PaginateValidator
15-
} from '../../validator/common';
12+
import { PositiveIdValidator, PaginateValidator } from '../../validator/common';
1613

1714
import { adminRequired } from '../../middleware/jwt';
1815
import { AdminDao } from '../../dao/admin';
1916

2017
const admin = new LinRouter({
21-
prefix: '/cms/admin'
18+
prefix: '/cms/admin',
19+
module: '管理员'
2220
});
2321

2422
const adminDao = new AdminDao();
2523

2624
admin.linGet(
2725
'getAllPermissions',
2826
'/permission',
29-
{
30-
permission: '查询所有可分配的权限',
31-
module: '管理员',
32-
mount: false
33-
},
27+
admin.permission('查询所有可分配的权限'),
3428
adminRequired,
3529
async ctx => {
3630
const permissions = await adminDao.getAllPermissions();
@@ -41,11 +35,7 @@ admin.linGet(
4135
admin.linGet(
4236
'getAdminUsers',
4337
'/users',
44-
{
45-
permission: '查询所有用户',
46-
module: '管理员',
47-
mount: false
48-
},
38+
admin.permission('查询所有用户'),
4939
adminRequired,
5040
async ctx => {
5141
const v = await new AdminUsersValidator().validate(ctx);
@@ -66,69 +56,50 @@ admin.linGet(
6656
admin.linPut(
6757
'changeUserPassword',
6858
'/user/:id/password',
69-
{
70-
permission: '修改用户密码',
71-
module: '管理员',
72-
mount: false
73-
},
59+
admin.permission('修改用户密码'),
7460
adminRequired,
7561
async ctx => {
7662
const v = await new ResetPasswordValidator().validate(ctx);
7763
await adminDao.changeUserPassword(ctx, v);
7864
ctx.success({
79-
msg: '密码修改成功',
80-
errorCode: 2
65+
code: 4
8166
});
8267
}
8368
);
8469

8570
admin.linDelete(
8671
'deleteUser',
8772
'/user/:id',
88-
{
89-
permission: '删除用户',
90-
module: '管理员',
91-
mount: false
92-
},
73+
admin.permission('删除用户'),
9374
adminRequired,
9475
async ctx => {
9576
const v = await new PositiveIdValidator().validate(ctx);
9677
const id = v.get('path.id');
9778
await adminDao.deleteUser(ctx, id);
9879
ctx.success({
99-
msg: '删除用户成功',
100-
errorCode: 3
80+
code: 5
10181
});
10282
}
10383
);
10484

10585
admin.linPut(
10686
'updateUser',
10787
'/user/:id',
108-
{
109-
permission: '管理员更新用户信息',
110-
module: '管理员',
111-
mount: false
112-
},
88+
admin.permission('管理员更新用户信息'),
11389
adminRequired,
11490
async ctx => {
11591
const v = await new UpdateUserInfoValidator().validate(ctx);
11692
await adminDao.updateUserInfo(ctx, v);
11793
ctx.success({
118-
msg: '更新用户成功',
119-
errorCode: 4
94+
code: 6
12095
});
12196
}
12297
);
12398

12499
admin.linGet(
125100
'getAdminGroups',
126101
'/group',
127-
{
128-
permission: '查询所有权限组及其权限',
129-
module: '管理员',
130-
mount: false
131-
},
102+
admin.permission('查询所有权限组及其权限'),
132103
adminRequired,
133104
async ctx => {
134105
const v = await new PaginateValidator().validate(ctx);
@@ -139,7 +110,7 @@ admin.linGet(
139110
);
140111
if (groups.length < 1) {
141112
throw new NotFound({
142-
msg: '未找到任何权限组'
113+
code: 10024
143114
});
144115
}
145116
ctx.json({
@@ -154,17 +125,13 @@ admin.linGet(
154125
admin.linGet(
155126
'getAllGroup',
156127
'/group/all',
157-
{
158-
permission: '查询所有权限组',
159-
module: '管理员',
160-
mount: false
161-
},
128+
admin.permission('查询所有权限组'),
162129
adminRequired,
163130
async ctx => {
164131
const groups = await adminDao.getAllGroups();
165132
if (!groups || groups.length < 1) {
166133
throw new NotFound({
167-
msg: '未找到任何权限组'
134+
code: 10024
168135
});
169136
}
170137
ctx.json(groups);
@@ -174,11 +141,7 @@ admin.linGet(
174141
admin.linGet(
175142
'getGroup',
176143
'/group/:id',
177-
{
178-
permission: '查询一个权限组及其权限',
179-
module: '管理员',
180-
mount: false
181-
},
144+
admin.permission('查询一个权限组及其权限'),
182145
adminRequired,
183146
async ctx => {
184147
const v = await new PositiveIdValidator().validate(ctx);
@@ -190,119 +153,89 @@ admin.linGet(
190153
admin.linPost(
191154
'createGroup',
192155
'/group',
193-
{
194-
permission: '新建权限组',
195-
module: '管理员',
196-
mount: false
197-
},
156+
admin.permission('新建权限组'),
198157
adminRequired,
199158
async ctx => {
200159
const v = await new NewGroupValidator().validate(ctx);
201160
const ok = await adminDao.createGroup(ctx, v);
202161
if (!ok) {
203162
throw new Failed({
204-
msg: '新建分组失败'
163+
code: 10027
205164
});
206165
}
207166
ctx.success({
208-
msg: '新建分组成功',
209-
errorCode: 13
167+
code: 15
210168
});
211169
}
212170
);
213171

214172
admin.linPut(
215173
'updateGroup',
216174
'/group/:id',
217-
{
218-
permission: '更新一个权限组',
219-
module: '管理员',
220-
mount: false
221-
},
175+
admin.permission('更新一个权限组'),
222176
adminRequired,
223177
async ctx => {
224178
const v = await new UpdateGroupValidator().validate(ctx);
225179
await adminDao.updateGroup(ctx, v);
226180
ctx.success({
227-
msg: '更新分组成功',
228-
errorCode: 5
181+
code: 7
229182
});
230183
}
231184
);
232185

233186
admin.linDelete(
234187
'deleteGroup',
235188
'/group/:id',
236-
{
237-
permission: '删除一个权限组',
238-
module: '管理员',
239-
mount: false
240-
},
189+
admin.permission('删除一个权限组'),
241190
adminRequired,
242191
async ctx => {
243192
const v = await new PositiveIdValidator().validate(ctx);
244193
const id = v.get('path.id');
245194
await adminDao.deleteGroup(ctx, id);
246195
ctx.success({
247-
msg: '删除分组成功',
248-
errorCode: 6
196+
code: 8
249197
});
250198
}
251199
);
252200

253201
admin.linPost(
254202
'dispatchPermission',
255203
'/permission/dispatch',
256-
{
257-
permission: '分配单个权限',
258-
module: '管理员',
259-
mount: false
260-
},
204+
admin.permission('分配单个权限'),
261205
adminRequired,
262206
async ctx => {
263207
const v = await new DispatchPermissionValidator().validate(ctx);
264208
await adminDao.dispatchPermission(ctx, v);
265209
ctx.success({
266-
msg: '添加权限成功',
267-
errorCode: 6
210+
code: 9
268211
});
269212
}
270213
);
271214

272215
admin.linPost(
273216
'dispatchPermissions',
274217
'/permission/dispatch/batch',
275-
{
276-
permission: '分配多个权限',
277-
module: '管理员',
278-
mount: false
279-
},
218+
admin.permission('分配多个权限'),
280219
adminRequired,
281220
async ctx => {
282221
const v = await new DispatchPermissionsValidator().validate(ctx);
283222
await adminDao.dispatchPermissions(ctx, v);
284223
ctx.success({
285-
msg: '添加权限成功',
286-
errorCode: 7
224+
code: 9
287225
});
288226
}
289227
);
290228

291229
admin.linPost(
292230
'removePermissions',
293231
'/permission/remove',
294-
{
295-
permission: '删除多个权限',
296-
module: '管理员',
297-
mount: false
298-
},
232+
admin.permission('删除多个权限'),
299233
adminRequired,
300234
async ctx => {
301235
const v = await new RemovePermissionsValidator().validate(ctx);
302236
await adminDao.removePermissions(ctx, v);
303237
ctx.success({
304-
msg: '删除权限成功',
305-
errorCode: 8
238+
code: 10
306239
});
307240
}
308241
);

app/api/cms/file.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ const file = new LinRouter({
77
prefix: '/cms/file'
88
});
99

10-
file.linPost('upload', '/', {}, loginRequired, async ctx => {
10+
file.linPost('upload', '/', loginRequired, async ctx => {
1111
const files = await ctx.multipart();
1212
if (files.length < 1) {
13-
throw new ParametersException({ msg: '未找到符合条件的文件资源' });
13+
throw new ParametersException({ code: 10033 });
1414
}
1515
const uploader = new LocalUploader('app/assets');
1616
const arr = await uploader.upload(files);

app/api/cms/log.js

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,23 @@ import { groupRequired } from '../../middleware/jwt';
66
import { LogDao } from '../../dao/log';
77

88
const log = new LinRouter({
9-
prefix: '/cms/log'
9+
prefix: '/cms/log',
10+
module: '日志'
1011
});
1112

1213
const logDao = new LogDao();
1314

1415
log.linGet(
1516
'getLogs',
1617
'/',
17-
{
18-
permission: '查询所有日志',
19-
module: '日志',
20-
mount: true
21-
},
18+
log.permission('查询所有日志'),
2219
groupRequired,
2320
async ctx => {
2421
const v = await new LogFindValidator().validate(ctx);
2522
const { rows, total } = await logDao.getLogs(v);
2623
if (!rows || rows.length < 1) {
2724
throw new NotFound({
28-
msg: '没有找到相关日志'
25+
code: 10220
2926
});
3027
}
3128
ctx.json({
@@ -40,11 +37,7 @@ log.linGet(
4037
log.linGet(
4138
'getUserLogs',
4239
'/search',
43-
{
44-
permission: '搜索日志',
45-
module: '日志',
46-
mount: true
47-
},
40+
log.permission('搜索日志'),
4841
groupRequired,
4942
async ctx => {
5043
const v = await new LogFindValidator().validate(ctx);
@@ -62,11 +55,7 @@ log.linGet(
6255
log.linGet(
6356
'getUsers',
6457
'/users',
65-
{
66-
permission: '查询日志记录的用户',
67-
module: '日志',
68-
mount: true
69-
},
58+
log.permission('查询日志记录的用户'),
7059
groupRequired,
7160
async ctx => {
7261
const v = await new PaginateValidator().validate(ctx);

0 commit comments

Comments
 (0)