operaLogs.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. 'use strict';
  2. const _ = require('lodash');
  3. const tableList = require('../public/tableName');
  4. const URL = require('url');
  5. // 关键词
  6. const keyToWord = {
  7. query: '列表查询',
  8. fetch: '单查询',
  9. create: '创建',
  10. update: '修改',
  11. delete: '删除',
  12. import: '导入',
  13. statis: '统计',
  14. };
  15. // 导入关键词
  16. const importKW = {
  17. examinee: '考试相关',
  18. staff: '考试保安员相关',
  19. securityGuard: '保安员',
  20. guardWork: '批量入职',
  21. };
  22. // 处理query变成object
  23. const queryToObject = queryStr => {
  24. const object = {};
  25. const arr = queryStr.split('&');
  26. if (arr.length > 0) {
  27. for (const a of arr) {
  28. const midArr = a.split('=');
  29. if (midArr.length === 2) {
  30. const key = _.head(midArr);
  31. const value = _.last(midArr);
  32. object[key] = value;
  33. }
  34. }
  35. }
  36. return object;
  37. };
  38. module.exports = options => {
  39. return async function operalogs(ctx, next) {
  40. await next();
  41. // 非生产模式不记录
  42. if (process.env.NODE_ENV !== 'production') return;
  43. try {
  44. let obj = { params: {} };
  45. const { url: reqUrl, method, body } = ctx.request;
  46. const i = reqUrl.indexOf('?');
  47. let url = reqUrl;
  48. // query
  49. if (i >= 0) {
  50. url = reqUrl.substring(0, i);
  51. const q = reqUrl.substring(i + 1, reqUrl.length);
  52. const queryObject = queryToObject(q);
  53. obj.params.query = queryObject;
  54. }
  55. // body
  56. if (body) obj.params.body = body;
  57. const arr = _.trim(url, ' ').split('/');
  58. const last = _.last(arr);
  59. const lsec = arr[arr.length - 2];
  60. const ip = _.get(ctx.request, 'header.x-real-ip');
  61. if (ip) obj.ip = ip;
  62. const user = ctx.user;
  63. if (user) {
  64. obj.name = user.name;
  65. obj.user_id = user.id;
  66. }
  67. if (method === 'GET') {
  68. // 列表,单查
  69. // if (last !== 'openid') {
  70. // obj.opera = keyToWord[last];
  71. // const tr = tableList.find(f => f.key === lsec);
  72. // if (tr) obj.table = tr.name;
  73. // } else {
  74. // obj.opera = '获取微信小程序openid';
  75. // }
  76. // 暂不计 get 查询相关记录
  77. obj = undefined;
  78. } else if (method === 'POST') {
  79. // 创建,修改,其他操作
  80. obj.opera = keyToWord[last];
  81. if (lsec === 'login') {
  82. // login 的 router中的配置
  83. if (last === 'checkToken') obj.opera = '检查token令牌';
  84. else obj.opera = '登陆';
  85. } else if (lsec === 'import') {
  86. // import 的 router中的配置
  87. obj.opera = importKW[last];
  88. } else {
  89. if (last === 'update') {
  90. const tr = tableList.find(f => f.key === lsec);
  91. if (tr) obj.table = tr.name;
  92. } else if (last === 'sendTemplate') {
  93. obj.opera = '发送公众号消息';
  94. } else {
  95. obj.opera = '创建';
  96. const tr = tableList.find(f => f.key === last);
  97. if (tr) obj.table = tr.name;
  98. }
  99. }
  100. } else if (method === 'DELETE') {
  101. // 删除操作
  102. obj.opera = keyToWord[last];
  103. if (obj.opera) {
  104. const tr = tableList.find(f => f.key === lsec);
  105. if (tr) obj.table = tr.name;
  106. }
  107. }
  108. if (obj.params) {
  109. obj.params = JSON.stringify(obj.params);
  110. }
  111. if (obj) await ctx.service.create.index({ table: 'opera_logs' }, obj);
  112. } catch (error) {
  113. ctx.logger.debug('创建日志失败');
  114. }
  115. };
  116. };