Skip to content

Commit 824cb5f

Browse files
authored
Merge pull request #20 from DXY-F2E/refactor-egg-upgrade
refactor: egg upgrade
2 parents 8a8dba5 + de8207d commit 824cb5f

File tree

26 files changed

+1119
-1202
lines changed

26 files changed

+1119
-1202
lines changed

server/app.js

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,16 @@
11
module.exports = app => {
2-
class AbstractController extends app.Controller {
3-
success (data) {
4-
this.ctx.status = 200
5-
this.ctx.body = data
2+
// 数字校验-允许提交字符串格式的数字
3+
app.validator.addRule('unstrict_number', (rule, value) => {
4+
if (value && !isNaN(value)) {
5+
value = Number(value)
66
}
7-
fail (msg) {
8-
// 弃用
9-
this.ctx.body = {
10-
success: false,
11-
msg
12-
}
7+
if (typeof value !== 'number') {
8+
return 'should be a number'
139
}
14-
error (data, code = 403) {
15-
if (typeof data === 'string') {
16-
this.ctx.throw(code, data)
17-
} else {
18-
this.ctx.throw(data.code, data.msg)
19-
}
20-
}
21-
notFound (msg) {
22-
msg = msg || 'not found'
23-
this.ctx.throw(404, msg)
24-
}
25-
}
26-
27-
app.Controller = AbstractController
10+
})
11+
app.validator.addRule('unstrict_boolean', (rule, value) => {
12+
if (typeof value === 'boolean') return
13+
if (value === 'false' || value === 'true') return
14+
return 'should be a boolean'
15+
})
2816
}

server/app/controller/abstract.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const Controller = require('egg').Controller
2+
class AbstractController extends Controller {
3+
success (data) {
4+
this.ctx.status = 200
5+
this.ctx.body = data
6+
}
7+
fail (msg) {
8+
// 弃用
9+
this.ctx.body = {
10+
success: false,
11+
msg
12+
}
13+
}
14+
error (data, code = 403) {
15+
if (typeof data === 'string') {
16+
this.ctx.throw(code, data)
17+
} else {
18+
this.ctx.throw(data.code, data.msg)
19+
}
20+
}
21+
notFound (msg) {
22+
msg = msg || 'not found'
23+
this.ctx.throw(404, msg)
24+
}
25+
}
26+
module.exports = AbstractController

0 commit comments

Comments
 (0)