controller.js
1.81 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
const _ = require('lodash');
const { login, account } = require('./model');
const config = global.config;
const loginByPassword = (req, res, next) => {
login.password(req.body).then(result => {
if (result.access) {
req.session.user = {
name: result.username
};
}
res.json({
code: 200,
data: {
access: result.access
},
message: result.message
});
}).catch(next)
};
const checkAdmin = (req, res, next) => {
let account = config.rootAccount || {};
if (account.username !== _.get(req.session, 'user.name')) {
return res.json({
code: 400,
message: '没有访问权限,请联系管理员开通'
});;
}
return next();
};
const accountList = (req, res, next) => {
account.list().then(list => {
return res.json({
code: 200,
data: { list },
message: ''
});
}).catch(next);
};
const accountAdd = (req, res, next) => {
account.add(req.body.account, _.get(req.session, 'user.name')).then(result => {
if (result.err) {
return res.json({
code: 400,
message: err
});
}
if (req.body.needList) {
accountList(req, res, next);
} else {
return res.json({
code: 200,
message: '添加成功'
});
}
}).catch(next);
};
const accountDelete = (req, res, next) => {
account.delete(req.body.id).then(() => {
return res.json({
code: 200,
message: '删除成功'
});
}).catch(next);
};
module.exports = {
loginByPassword,
checkAdmin,
accountList,
accountAdd,
accountDelete
};