Skip to content

Commit 374b9a7

Browse files
author
pedro
committed
feat:重构校验器
1 parent 905b10b commit 374b9a7

13 files changed

Lines changed: 76 additions & 76 deletions

File tree

app/api/cms/log.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
"use strict";
22

3-
const {
4-
LinRouter,
5-
groupRequired,
6-
NotFound,
7-
paginate
8-
} = require("lin-mizar");
3+
const { LinRouter, groupRequired, NotFound, paginate } = require("lin-mizar");
94
const { LogFindValidator } = require("../../validators/cms");
105
const { get } = require("lodash");
116
const { LogDao } = require("../../dao/log");

app/api/cms/test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
"use strict";
22

3-
const { loginRequired, LinRouter, logger, groupRequired } = require("lin-mizar");
3+
const {
4+
loginRequired,
5+
LinRouter,
6+
logger,
7+
groupRequired
8+
} = require("lin-mizar");
49

510
const test = new LinRouter({
611
prefix: "/cms/test"

app/api/cms/user.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ user.linPost(
5757
async ctx => {
5858
const v = await new LoginValidator().validate(ctx);
5959
let user = await ctx.manager.userModel.verify(
60-
v.get("nickname"),
61-
v.get("password")
60+
v.get("body.nickname"),
61+
v.get("body.password")
6262
);
6363
const { accessToken, refreshToken } = getTokens(user);
6464
ctx.json({
@@ -101,8 +101,8 @@ user.linPut(
101101
const v = await new ChangePasswordValidator().validate(ctx);
102102
let user = ctx.currentUser;
103103
const ok = user.changePassword(
104-
v.get("old_password"),
105-
v.get("new_password")
104+
v.get("body.old_password"),
105+
v.get("body.new_password")
106106
);
107107
if (ok) {
108108
user.save();

app/api/v1/book.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ bookApi.get("/", async ctx => {
4545

4646
bookApi.get("/search/one", async ctx => {
4747
const v = await new BookSearchValidator().validate(ctx);
48-
const book = await bookDto.getBookByKeyword(v.get("q"));
48+
const book = await bookDto.getBookByKeyword(v.get("query.q"));
4949
if (!book) {
5050
throw new BookNotFound();
5151
}

app/dao/admin.js

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class AdminDao {
6969
msg: "用户不存在"
7070
});
7171
}
72-
user.resetPassword(v.get("new_password"));
72+
user.resetPassword(v.get("body.new_password"));
7373
user.save();
7474
}
7575

@@ -101,10 +101,10 @@ class AdminDao {
101101
msg: "用户不存在"
102102
});
103103
}
104-
if (user.email !== v.get("email")) {
104+
if (user.email !== v.get("body.email")) {
105105
const exit = await ctx.manager.userModel.findOne({
106106
where: {
107-
email: v.get("email"),
107+
email: v.get("body.email"),
108108
delete_time: null
109109
}
110110
});
@@ -114,8 +114,8 @@ class AdminDao {
114114
});
115115
}
116116
}
117-
user.group_id = v.get("group_id");
118-
user.email = v.get("email");
117+
user.group_id = v.get("body.group_id");
118+
user.email = v.get("body.email");
119119
user.save();
120120
}
121121

@@ -172,7 +172,7 @@ class AdminDao {
172172
async createGroup (ctx, v) {
173173
const exit = await ctx.manager.groupModel.findOne({
174174
where: {
175-
name: v.get("name")
175+
name: v.get("body.name")
176176
}
177177
});
178178
if (exit) {
@@ -185,14 +185,14 @@ class AdminDao {
185185
transaction = await db.transaction();
186186
const group = await ctx.manager.groupModel.create(
187187
{
188-
name: v.get("name"),
189-
info: v.get("info")
188+
name: v.get("body.name"),
189+
info: v.get("body.info")
190190
},
191191
{
192192
transaction
193193
}
194194
);
195-
for (const item of v.get("auths")) {
195+
for (const item of v.get("body.auths")) {
196196
const { auth, module } = findMetaByAuth(item);
197197
await ctx.manager.authModel.create(
198198
{
@@ -219,8 +219,8 @@ class AdminDao {
219219
msg: "分组不存在,更新失败"
220220
});
221221
}
222-
exit.name = v.get("name");
223-
exit.info = v.get("info");
222+
exit.name = v.get("body.name");
223+
exit.info = v.get("body.info");
224224
exit.save();
225225
}
226226

@@ -260,16 +260,16 @@ class AdminDao {
260260
}
261261

262262
async dispatchAuth (ctx, v) {
263-
const group = await ctx.manager.groupModel.findById(v.get("group_id"));
263+
const group = await ctx.manager.groupModel.findById(v.get("body.group_id"));
264264
if (!group) {
265265
throw new NotFound({
266266
msg: "分组不存在"
267267
});
268268
}
269269
const one = await ctx.manager.authModel.findOne({
270270
where: {
271-
group_id: v.get("group_id"),
272-
auth: v.get("auth")
271+
group_id: v.get("body.group_id"),
272+
auth: v.get("body.auth")
273273
}
274274
});
275275
if (one) {
@@ -278,15 +278,15 @@ class AdminDao {
278278
});
279279
}
280280
const au = new ctx.manager.authModel();
281-
const { auth, module } = findMetaByAuth(v.get("auth"));
281+
const { auth, module } = findMetaByAuth(v.get("body.auth"));
282282
au.auth = auth;
283283
au.module = module;
284-
au.group_id = v.get("group_id");
284+
au.group_id = v.get("body.group_id");
285285
await au.save();
286286
}
287287

288288
async dispatchAuths (ctx, v) {
289-
const group = await ctx.manager.groupModel.findById(v.get("group_id"));
289+
const group = await ctx.manager.groupModel.findById(v.get("body.group_id"));
290290
if (!group) {
291291
throw new NotFound({
292292
msg: "分组不存在"
@@ -295,7 +295,7 @@ class AdminDao {
295295
v.get("auths").forEach(async item => {
296296
const one = await ctx.manager.authModel.findOne({
297297
where: {
298-
group_id: v.get("group_id"),
298+
group_id: v.get("body.group_id"),
299299
auth: item
300300
}
301301
});
@@ -304,24 +304,24 @@ class AdminDao {
304304
const { auth, module } = findMetaByAuth(item);
305305
au.auth = auth;
306306
au.module = module;
307-
au.group_id = v.get("group_id");
307+
au.group_id = v.get("body.group_id");
308308
await au.save();
309309
}
310310
});
311311
}
312312

313313
async removeAuths (ctx, v) {
314-
const group = await ctx.manager.groupModel.findById(v.get("group_id"));
314+
const group = await ctx.manager.groupModel.findById(v.get("body.group_id"));
315315
if (!group) {
316316
throw new NotFound({
317317
msg: "分组不存在"
318318
});
319319
}
320320
await ctx.manager.authModel.destroy({
321321
where: {
322-
group_id: v.get("group_id"),
322+
group_id: v.get("body.group_id"),
323323
auth: {
324-
[db.Op.in]: v.get("auths")
324+
[db.Op.in]: v.get("body.auths")
325325
}
326326
}
327327
});

app/dao/book.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class BookDao {
4444
async createBook (v) {
4545
const book = await Book.findOne({
4646
where: {
47-
title: v.get("title"),
47+
title: v.get("body.title"),
4848
delete_time: null
4949
}
5050
});
@@ -54,10 +54,10 @@ class BookDao {
5454
});
5555
}
5656
const bk = new Book();
57-
bk.title = v.get("title");
58-
bk.author = v.get("author");
59-
bk.summary = v.get("summary");
60-
bk.image = v.get("image");
57+
bk.title = v.get("body.title");
58+
bk.author = v.get("body.author");
59+
bk.summary = v.get("body.summary");
60+
bk.image = v.get("body.image");
6161
bk.save();
6262
}
6363

@@ -68,10 +68,10 @@ class BookDao {
6868
msg: "没有找到相关书籍"
6969
});
7070
}
71-
book.title = v.get("title");
72-
book.author = v.get("author");
73-
book.summary = v.get("summary");
74-
book.image = v.get("image");
71+
book.title = v.get("body.title");
72+
book.author = v.get("body.author");
73+
book.summary = v.get("body.summary");
74+
book.image = v.get("body.image");
7575
book.save();
7676
}
7777

app/dao/log.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ const Sequelize = require("sequelize");
88
class LogDao {
99
async getLogs (v, start, count1) {
1010
let condition = {};
11-
v.get("name") && set(condition, "user_name", v.get("name"));
12-
v.get("start") &&
13-
v.get("end") &&
11+
v.get("body.name") && set(condition, "user_name", v.get("body.name"));
12+
v.get("body.start") &&
13+
v.get("body.end") &&
1414
set(condition, "time", {
15-
[Sequelize.Op.between]: [v.get("start"), v.get("end")]
15+
[Sequelize.Op.between]: [v.get("body.start"), v.get("body.end")]
1616
});
1717
let { rows, count } = await Log.findAndCountAll({
1818
where: Object.assign({}, condition),
@@ -28,11 +28,11 @@ class LogDao {
2828

2929
async searchLogs (v, start, count1, keyword) {
3030
let condition = {};
31-
v.get("name") && set(condition, "user_name", v.get("name"));
32-
v.get("start") &&
33-
v.get("end") &&
31+
v.get("body.name") && set(condition, "user_name", v.get("body.name"));
32+
v.get("body.start") &&
33+
v.get("body.end") &&
3434
set(condition, "time", {
35-
[Sequelize.Op.between]: [v.get("start"), v.get("end")]
35+
[Sequelize.Op.between]: [v.get("body.start"), v.get("body.end")]
3636
});
3737
let { rows, count } = await Log.findAndCount({
3838
where: Object.assign({}, condition, {

app/dao/user.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ class UserDao {
88
async createUser (ctx, v) {
99
let user = await ctx.manager.userModel.findOne({
1010
where: {
11-
nickname: v.get("nickname")
11+
nickname: v.get("body.nickname")
1212
}
1313
});
1414
if (user) {
1515
throw new RepeatException({
1616
msg: "用户名重复,请重新输入"
1717
});
1818
}
19-
if (v.get("email") && v.get("email").trim() !== "") {
19+
if (v.get("body.email") && v.get("body.email").trim() !== "") {
2020
user = await ctx.manager.userModel.findOne({
2121
where: {
22-
email: v.get("email")
22+
email: v.get("body.email")
2323
}
2424
});
2525
if (user) {
@@ -33,10 +33,10 @@ class UserDao {
3333

3434
async updateUser (ctx, v) {
3535
let user = ctx.currentUser;
36-
if (user.email !== v.get("email")) {
36+
if (user.email !== v.get("body.email")) {
3737
const exit = await ctx.manager.userModel.findOne({
3838
where: {
39-
email: v.get("email")
39+
email: v.get("body.email")
4040
}
4141
});
4242
if (exit) {
@@ -45,7 +45,7 @@ class UserDao {
4545
});
4646
}
4747
}
48-
user.email = v.get("email");
48+
user.email = v.get("body.email");
4949
user.save();
5050
}
5151

@@ -88,11 +88,11 @@ class UserDao {
8888

8989
registerUser (ctx, v) {
9090
const user = new ctx.manager.userModel();
91-
user.nickname = v.get("nickname");
92-
user.password = v.get("password");
93-
user.group_id = v.get("group_id");
94-
if (v.get("email") && v.get("email").trim() !== "") {
95-
user.email = v.get("email");
91+
user.nickname = v.get("body.nickname");
92+
user.password = v.get("body.password");
93+
user.group_id = v.get("body.group_id");
94+
if (v.get("body.email") && v.get("body.email").trim() !== "") {
95+
user.email = v.get("body.email");
9696
}
9797
user.save();
9898
}

app/plugins/notify/app/message.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class MessageDao {
6666
},
6767
{
6868
where: {
69-
[Sequelize.Op.in]: v.get("ids")
69+
[Sequelize.Op.in]: v.get("body.ids")
7070
}
7171
}
7272
);

app/plugins/notify/app/sse.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class EventDao {
3636
async createEvents (v) {
3737
const event = await Event.findOne({
3838
where: {
39-
group_id: v.get("group_id")
39+
group_id: v.get("body.group_id")
4040
}
4141
});
4242
if (event) {
@@ -45,22 +45,22 @@ class EventDao {
4545
});
4646
}
4747
const ev = new Event();
48-
ev.group_id = v.get("group_id");
49-
ev.message_events = v.get("events").join(",");
48+
ev.group_id = v.get("body.group_id");
49+
ev.message_events = v.get("body.events").join(",");
5050
ev.save();
5151
}
5252
async updateEvents (v) {
5353
const event = await Event.findOne({
5454
where: {
55-
group_id: v.get("group_id")
55+
group_id: v.get("body.group_id")
5656
}
5757
});
5858
if (!event) {
5959
throw new NotFound({
6060
msg: "当前权限组不存在推送项"
6161
});
6262
}
63-
event.message_events = v.get("events").join(",");
63+
event.message_events = v.get("body.events").join(",");
6464
event.save();
6565
}
6666
}

0 commit comments

Comments
 (0)