operation_log.js
1.29 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
/**
*
* @author: jiangfeng<jeff.jiang@yoho.cn>
* @date: 16/8/22
*/
'use strict';
const Router = require('koa-router');
const moment = require('moment');
const {
OperationLogger
} = require('../../models');
const r = new Router();
r.get('/log', async(ctx) => {
await ctx.render('action/operation_log');
});
r.post('/log/query', async(ctx) => {
let q = ctx.request.body;
let start = parseInt(q.start || 0, 10);
let length = parseInt(q.length || 10, 10);
let draw = parseInt(q.draw, 10);
let sort = {time: -1};
if (q.order && q.order.length > 0) {
let col = q.order[0].column;
let dir = q.order[0].dir === 'desc' ? -1 : 1;
col = parseInt(col, 10);
if (q.columns && q.columns.length > col) {
sort = {};
sort[q.columns[col].data] = dir;
}
}
let logs = await OperationLogger.cfind({}).sort(sort).skip(start).limit(length).exec();
let total = await OperationLogger.count();
logs.forEach(l => {
l.username = l.user.username;
l.time = moment(l.time).format('YYYY-MM-DD HH:mm:ss.sss');
l.meta = JSON.stringify(l.meta || {});
});
ctx.body = {
draw: draw,
recordsTotal: total,
recordsFiltered: total,
data: logs
};
});
module.exports = r;