users.js
1.53 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
/**
* 用户管理
* @author: jiangfeng<jeff.jiang@yoho.cn>
* @date: 16/8/16
*/
'use strict';
import Router from 'koa-router';
import md5 from 'md5';
import {User} from '../../models';
const r = new Router;
const user = {
async setting_page(ctx) {
let users = await User.findAll();
await ctx.render('action/users', {
users: users
});
},
async new_page(ctx) {
await ctx.render('action/users_form');
},
async edit_page(ctx) {
let id = ctx.query.id;
let user = await User.findById(id);
user.password = "******";
await ctx.render('action/users_form', user);
},
async save(ctx) {
let {_id, username, password, role, state} = ctx.request.body;
let user = {
username: username,
role: role,
state: state
};
if (password !== '******') {
user.password = md5(password);
}
if (_id) {
await User.update({
_id: _id
}, {
$set: user
});
} else {
await User.insert(user);
}
ctx.redirect('/users/setting');
ctx.status = 301;
},
async del(ctx) {
let id = ctx.request.body.id;
await User.removeById(id);
ctx.body = {
code: 200
};
}
};
r.get('/setting', user.setting_page);
r.get('/new', user.new_page);
r.get('/edit', user.edit_page);
r.post('/save', user.save);
r.post('/del', user.del);
export default r;