This repository has been archived by the owner. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathrole.js
More file actions
90 lines (78 loc) · 2.37 KB
/
Copy pathrole.js
File metadata and controls
90 lines (78 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
const Controller = require('egg').Controller
class RoleController extends Controller {
constructor(ctx) {
super(ctx)
this.createRule = {
name: { type: 'string', required: true, allowEmpty: false },
access: { type: 'string', required: true, allowEmpty: false }
}
}
// 创建角色
async create() {
const { ctx, service } = this
// 校验参数
ctx.validate(this.createRule)
// 组装参数
const payload = ctx.request.body || {}
// 调用 Service 进行业务处理
const res = await service.role.create(payload)
// 设置响应内容和响应状态码
ctx.helper.success({ctx, res})
}
// 删除单个角色
async destroy() {
const { ctx, service } = this
// 校验参数
const { id } = ctx.params
// 调用 Service 进行业务处理
await service.role.destroy(id)
// 设置响应内容和响应状态码
ctx.helper.success({ctx})
}
// 修改角色
async update() {
const { ctx, service } = this
// 校验参数
ctx.validate(this.createRule)
// 组装参数
const { id } = ctx.params
const payload = ctx.request.body || {}
// 调用 Service 进行业务处理
await service.role.update(id, payload)
// 设置响应内容和响应状态码
ctx.helper.success({ctx})
}
// 获取单个角色
async show() {
const { ctx, service } = this
// 组装参数
const { id } = ctx.params
// 调用 Service 进行业务处理
const res = await service.role.show(id)
// 设置响应内容和响应状态码
ctx.helper.success({ctx, res})
}
// 获取所有角色(分页/模糊)
async index() {
const { ctx, service } = this
// 组装参数
const payload = ctx.query
// 调用 Service 进行业务处理
const res = await service.role.index(payload)
// 设置响应内容和响应状态码
ctx.helper.success({ctx, res})
}
// 删除所选角色(条件id[])
async removes() {
const { ctx, service } = this
// 组装参数
// const payload = ctx.queries.id
const { id } = ctx.request.body // {id: "5a452a44ab122b16a0231b42,5a452a3bab122b16a0231b41"}
const payload = id.split(',') || []
// 调用 Service 进行业务处理
const result = await service.role.removes(payload)
// 设置响应内容和响应状态码
ctx.helper.success({ctx})
}
}
module.exports = RoleController