Skip to content

Commit 3099950

Browse files
authored
Merge pull request #76 from TaleLin/develop
feat(*): 优化 permission 设置方式; 添加 code-message 映射; 支持自定义工作目录; 更新核心库 lin-mizar 到 0.3.3; HttpException 不允许直接修改 status; 传入的参数由 errorCode 改为 code
2 parents 10ec44d + 85b07fb commit 3099950

36 files changed

+878
-607
lines changed

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ Lin-CMS 是林间有风团队经过大量项目实践所提炼出的一套**内
3535

3636
### 当前最新版本
3737

38-
lin-mizar(核心库) :0.3.2
38+
lin-mizar(核心库) :0.3.3
3939

40-
lin-cms-koa(当前示例工程):0.3.3
40+
lin-cms-koa(当前示例工程):0.3.4
4141

4242
### 文档地址
4343

@@ -61,7 +61,15 @@ QQ 群号:643205479
6161

6262
## 版本日志
6363

64-
最新版本 `0.3.3`
64+
最新版本 `0.3.4`
65+
66+
### 0.3.4
67+
68+
1. `U` 更新路由视图权限挂载的方式
69+
2. `U` HttpException 不允许直接修改 status,传入的参数由 errorCode 改为 code
70+
3. `U` 新增 code-message 配置,返回的成功码和错误码都在这里配置
71+
4. `U` 支持自定义工作目录
72+
5. `U` 更新核心库 lin-mizar 到 0.3.3 版本
6573

6674
### 0.3.3
6775

app/api/cms/admin.js

Lines changed: 31 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,24 @@ 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: '管理员',
20+
// 管理员权限暂不支持分配,开启分配后也无实际作用
21+
mountPermission: false
2222
});
2323

2424
const adminDao = new AdminDao();
2525

2626
admin.linGet(
2727
'getAllPermissions',
2828
'/permission',
29-
{
30-
permission: '查询所有可分配的权限',
31-
module: '管理员',
32-
mount: false
33-
},
29+
admin.permission('查询所有可分配的权限'),
3430
adminRequired,
3531
async ctx => {
3632
const permissions = await adminDao.getAllPermissions();
@@ -41,11 +37,7 @@ admin.linGet(
4137
admin.linGet(
4238
'getAdminUsers',
4339
'/users',
44-
{
45-
permission: '查询所有用户',
46-
module: '管理员',
47-
mount: false
48-
},
40+
admin.permission('查询所有用户'),
4941
adminRequired,
5042
async ctx => {
5143
const v = await new AdminUsersValidator().validate(ctx);
@@ -66,69 +58,50 @@ admin.linGet(
6658
admin.linPut(
6759
'changeUserPassword',
6860
'/user/:id/password',
69-
{
70-
permission: '修改用户密码',
71-
module: '管理员',
72-
mount: false
73-
},
61+
admin.permission('修改用户密码'),
7462
adminRequired,
7563
async ctx => {
7664
const v = await new ResetPasswordValidator().validate(ctx);
7765
await adminDao.changeUserPassword(ctx, v);
7866
ctx.success({
79-
msg: '密码修改成功',
80-
errorCode: 2
67+
code: 4
8168
});
8269
}
8370
);
8471

8572
admin.linDelete(
8673
'deleteUser',
8774
'/user/:id',
88-
{
89-
permission: '删除用户',
90-
module: '管理员',
91-
mount: false
92-
},
75+
admin.permission('删除用户'),
9376
adminRequired,
9477
async ctx => {
9578
const v = await new PositiveIdValidator().validate(ctx);
9679
const id = v.get('path.id');
9780
await adminDao.deleteUser(ctx, id);
9881
ctx.success({
99-
msg: '删除用户成功',
100-
errorCode: 3
82+
code: 5
10183
});
10284
}
10385
);
10486

10587
admin.linPut(
10688
'updateUser',
10789
'/user/:id',
108-
{
109-
permission: '管理员更新用户信息',
110-
module: '管理员',
111-
mount: false
112-
},
90+
admin.permission('管理员更新用户信息'),
11391
adminRequired,
11492
async ctx => {
11593
const v = await new UpdateUserInfoValidator().validate(ctx);
11694
await adminDao.updateUserInfo(ctx, v);
11795
ctx.success({
118-
msg: '更新用户成功',
119-
errorCode: 4
96+
code: 6
12097
});
12198
}
12299
);
123100

124101
admin.linGet(
125102
'getAdminGroups',
126103
'/group',
127-
{
128-
permission: '查询所有权限组及其权限',
129-
module: '管理员',
130-
mount: false
131-
},
104+
admin.permission('查询所有权限组及其权限'),
132105
adminRequired,
133106
async ctx => {
134107
const v = await new PaginateValidator().validate(ctx);
@@ -139,7 +112,7 @@ admin.linGet(
139112
);
140113
if (groups.length < 1) {
141114
throw new NotFound({
142-
msg: '未找到任何权限组'
115+
code: 10024
143116
});
144117
}
145118
ctx.json({
@@ -154,17 +127,13 @@ admin.linGet(
154127
admin.linGet(
155128
'getAllGroup',
156129
'/group/all',
157-
{
158-
permission: '查询所有权限组',
159-
module: '管理员',
160-
mount: false
161-
},
130+
admin.permission('查询所有权限组'),
162131
adminRequired,
163132
async ctx => {
164133
const groups = await adminDao.getAllGroups();
165134
if (!groups || groups.length < 1) {
166135
throw new NotFound({
167-
msg: '未找到任何权限组'
136+
code: 10024
168137
});
169138
}
170139
ctx.json(groups);
@@ -174,11 +143,7 @@ admin.linGet(
174143
admin.linGet(
175144
'getGroup',
176145
'/group/:id',
177-
{
178-
permission: '查询一个权限组及其权限',
179-
module: '管理员',
180-
mount: false
181-
},
146+
admin.permission('查询一个权限组及其权限'),
182147
adminRequired,
183148
async ctx => {
184149
const v = await new PositiveIdValidator().validate(ctx);
@@ -190,119 +155,89 @@ admin.linGet(
190155
admin.linPost(
191156
'createGroup',
192157
'/group',
193-
{
194-
permission: '新建权限组',
195-
module: '管理员',
196-
mount: false
197-
},
158+
admin.permission('新建权限组'),
198159
adminRequired,
199160
async ctx => {
200161
const v = await new NewGroupValidator().validate(ctx);
201162
const ok = await adminDao.createGroup(ctx, v);
202163
if (!ok) {
203164
throw new Failed({
204-
msg: '新建分组失败'
165+
code: 10027
205166
});
206167
}
207168
ctx.success({
208-
msg: '新建分组成功',
209-
errorCode: 13
169+
code: 15
210170
});
211171
}
212172
);
213173

214174
admin.linPut(
215175
'updateGroup',
216176
'/group/:id',
217-
{
218-
permission: '更新一个权限组',
219-
module: '管理员',
220-
mount: false
221-
},
177+
admin.permission('更新一个权限组'),
222178
adminRequired,
223179
async ctx => {
224180
const v = await new UpdateGroupValidator().validate(ctx);
225181
await adminDao.updateGroup(ctx, v);
226182
ctx.success({
227-
msg: '更新分组成功',
228-
errorCode: 5
183+
code: 7
229184
});
230185
}
231186
);
232187

233188
admin.linDelete(
234189
'deleteGroup',
235190
'/group/:id',
236-
{
237-
permission: '删除一个权限组',
238-
module: '管理员',
239-
mount: false
240-
},
191+
admin.permission('删除一个权限组'),
241192
adminRequired,
242193
async ctx => {
243194
const v = await new PositiveIdValidator().validate(ctx);
244195
const id = v.get('path.id');
245196
await adminDao.deleteGroup(ctx, id);
246197
ctx.success({
247-
msg: '删除分组成功',
248-
errorCode: 6
198+
code: 8
249199
});
250200
}
251201
);
252202

253203
admin.linPost(
254204
'dispatchPermission',
255205
'/permission/dispatch',
256-
{
257-
permission: '分配单个权限',
258-
module: '管理员',
259-
mount: false
260-
},
206+
admin.permission('分配单个权限'),
261207
adminRequired,
262208
async ctx => {
263209
const v = await new DispatchPermissionValidator().validate(ctx);
264210
await adminDao.dispatchPermission(ctx, v);
265211
ctx.success({
266-
msg: '添加权限成功',
267-
errorCode: 6
212+
code: 9
268213
});
269214
}
270215
);
271216

272217
admin.linPost(
273218
'dispatchPermissions',
274219
'/permission/dispatch/batch',
275-
{
276-
permission: '分配多个权限',
277-
module: '管理员',
278-
mount: false
279-
},
220+
admin.permission('分配多个权限'),
280221
adminRequired,
281222
async ctx => {
282223
const v = await new DispatchPermissionsValidator().validate(ctx);
283224
await adminDao.dispatchPermissions(ctx, v);
284225
ctx.success({
285-
msg: '添加权限成功',
286-
errorCode: 7
226+
code: 9
287227
});
288228
}
289229
);
290230

291231
admin.linPost(
292232
'removePermissions',
293233
'/permission/remove',
294-
{
295-
permission: '删除多个权限',
296-
module: '管理员',
297-
mount: false
298-
},
234+
admin.permission('删除多个权限'),
299235
adminRequired,
300236
async ctx => {
301237
const v = await new RemovePermissionsValidator().validate(ctx);
302238
await adminDao.removePermissions(ctx, v);
303239
ctx.success({
304-
msg: '删除权限成功',
305-
errorCode: 8
240+
code: 10
306241
});
307242
}
308243
);

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)