123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- 'use strict';
- const _ = require('lodash');
- const tableList = require('../public/tableName');
- const URL = require('url');
- // 关键词
- const keyToWord = {
- query: '列表查询',
- fetch: '单查询',
- create: '创建',
- update: '修改',
- delete: '删除',
- import: '导入',
- statis: '统计',
- };
- // 导入关键词
- const importKW = {
- examinee: '考试相关',
- staff: '考试保安员相关',
- securityGuard: '保安员',
- guardWork: '批量入职',
- };
- // 处理query变成object
- const queryToObject = queryStr => {
- const object = {};
- const arr = queryStr.split('&');
- if (arr.length > 0) {
- for (const a of arr) {
- const midArr = a.split('=');
- if (midArr.length === 2) {
- const key = _.head(midArr);
- const value = _.last(midArr);
- object[key] = value;
- }
- }
- }
- return object;
- };
- module.exports = options => {
- return async function operalogs(ctx, next) {
- await next();
- // 非生产模式不记录
- if (process.env.NODE_ENV !== 'production') return;
- try {
- let obj = { params: {} };
- const { url: reqUrl, method, body } = ctx.request;
- const i = reqUrl.indexOf('?');
- let url = reqUrl;
- // query
- if (i >= 0) {
- url = reqUrl.substring(0, i);
- const q = reqUrl.substring(i + 1, reqUrl.length);
- const queryObject = queryToObject(q);
- obj.params.query = queryObject;
- }
- // body
- if (body) obj.params.body = body;
- const arr = _.trim(url, ' ').split('/');
- const last = _.last(arr);
- const lsec = arr[arr.length - 2];
- const ip = _.get(ctx.request, 'header.x-real-ip');
- if (ip) obj.ip = ip;
- const user = ctx.user;
- if (user) {
- obj.name = user.name;
- obj.user_id = user.id;
- }
- if (method === 'GET') {
- // 列表,单查
- // if (last !== 'openid') {
- // obj.opera = keyToWord[last];
- // const tr = tableList.find(f => f.key === lsec);
- // if (tr) obj.table = tr.name;
- // } else {
- // obj.opera = '获取微信小程序openid';
- // }
- // 暂不计 get 查询相关记录
- obj = undefined;
- } else if (method === 'POST') {
- // 创建,修改,其他操作
- obj.opera = keyToWord[last];
- if (lsec === 'login') {
- // login 的 router中的配置
- if (last === 'checkToken') obj.opera = '检查token令牌';
- else obj.opera = '登陆';
- } else if (lsec === 'import') {
- // import 的 router中的配置
- obj.opera = importKW[last];
- } else {
- if (last === 'update') {
- const tr = tableList.find(f => f.key === lsec);
- if (tr) obj.table = tr.name;
- } else if (last === 'sendTemplate') {
- obj.opera = '发送公众号消息';
- } else {
- obj.opera = '创建';
- const tr = tableList.find(f => f.key === last);
- if (tr) obj.table = tr.name;
- }
- }
- } else if (method === 'DELETE') {
- // 删除操作
- obj.opera = keyToWord[last];
- if (obj.opera) {
- const tr = tableList.find(f => f.key === lsec);
- if (tr) obj.table = tr.name;
- }
- }
- if (obj.params) {
- obj.params = JSON.stringify(obj.params);
- }
- if (obj) await ctx.service.create.index({ table: 'opera_logs' }, obj);
- } catch (error) {
- ctx.logger.debug('创建日志失败');
- }
- };
- };
|