Repository navigation
Expand file tree
/
Copy pathuser.js
More file actions
95 lines (83 loc) · 2.67 KB
/
Copy pathuser.js
File metadata and controls
95 lines (83 loc) · 2.67 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
91
92
93
94
95
const Controller = require('egg').Controller
class UserController extends Controller {
constructor(ctx) {
super(ctx)
this.UserCreateTransfer = {
mobile: {type: 'string', required: true, allowEmpty: false, format: /^[0-9]{11}$/},
password: {type: 'password', required: true, allowEmpty: false, min: 6},
realName: {type: 'string', required: true, allowEmpty: false, format: /^[\u2E80-\u9FFF]{2,6}$/}
}
this.UserUpdateTransfer = {
mobile: { type: 'string', required: true, allowEmpty: false },
realName: {type: 'string', required: true, allowEmpty: false, format: /^[\u2E80-\u9FFF]{2,6}$/}
}
}
// 创建用户
async create() {
const { ctx, service } = this
// 校验参数
ctx.validate(this.UserCreateTransfer)
// 组装参数
const payload = ctx.request.body || {}
// 调用 Service 进行业务处理
const res = await service.user.create(payload)
// 设置响应内容和响应状态码
ctx.helper.success({ctx, res})
}
// 删除单个用户
async destroy() {
const { ctx, service } = this
// 校验参数
const { id } = ctx.params
// 调用 Service 进行业务处理
await service.user.destroy(id)
// 设置响应内容和响应状态码
ctx.helper.success({ctx})
}
// 修改用户
async update() {
const { ctx, service } = this
// 校验参数
ctx.validate(this.UserUpdateTransfer)
// 组装参数
const { id } = ctx.params
const payload = ctx.request.body || {}
// 调用 Service 进行业务处理
await service.user.update(id, payload)
// 设置响应内容和响应状态码
ctx.helper.success({ctx})
}
// 获取单个用户
async show() {
const { ctx, service } = this
// 组装参数
const { id } = ctx.params
// 调用 Service 进行业务处理
const res = await service.user.show(id)
// 设置响应内容和响应状态码
ctx.helper.success({ctx, res})
}
// 获取所有用户(分页/模糊)
async index() {
const { ctx, service } = this
// 组装参数
const payload = ctx.query
// 调用 Service 进行业务处理
const res = await service.user.index(payload)
// 设置响应内容和响应状态码
ctx.helper.success({ctx, res})
}
// 删除所选用户(条件id[])
async removes() {
const { ctx, service } = this
// 组装参数
// const payload = ctx.queries.id
const { id } = ctx.request.body
const payload = id.split(',') || []
// 调用 Service 进行业务处理
const result = await service.user.removes(payload)
// 设置响应内容和响应状态码
ctx.helper.success({ctx})
}
}
module.exports = UserController