lrf402788946 4 éve
szülő
commit
bdc141c55f

+ 21 - 0
app/controller/.logs.js

@@ -0,0 +1,21 @@
+module.exports = {
+  index: {
+    parameters: {
+      query: {
+        ip: "ip",
+        userid: "userid",
+        name: "name",
+        route: "route",
+        method: "method",
+        time: "time",
+      },
+    },
+    service: "query",
+    options: {
+      query: ["skip", "limit"],
+      sort: ["time", "meta.createdAt"],
+      desc: true,
+      count: true,
+    },
+  },
+};

+ 18 - 0
app/controller/logs.js

@@ -0,0 +1,18 @@
+'use strict';
+
+const _ = require('lodash');
+const meta = require('./.logs.js');
+const Controller = require('egg').Controller;
+const { CrudController } = require('naf-framework-mongoose/lib/controller');
+
+// 教师申请讲课表管理
+class LogsController extends Controller {
+
+  constructor(ctx) {
+    super(ctx);
+    this.service = this.ctx.service.logs;
+  }
+
+}
+
+module.exports = CrudController(LogsController, meta);

+ 41 - 0
app/middleware/logs.js

@@ -0,0 +1,41 @@
+'use strict';
+const moment = require('moment');
+const _ = require('lodash');
+const operaMethod = require('../utils/routerword.js');
+module.exports = (options, app) => {
+  return async (ctx, next) => {
+    try {
+      const { authorization } = ctx.request.header;
+      if (authorization) {
+        let user = decodeURI(authorization);
+        // console.log(user);
+        if (user) {
+          user = JSON.parse(user);
+          const { path: route, method, query, body, ip } = ctx.request;
+          if (method !== 'GET') {
+            const { id, name } = user;
+            const params = {};
+            if (query) params.query = query;
+            if (body) params.body = body;
+            if (method === 'DELETE') {
+              const pl = route.split('/');
+              const last = _.last(pl);
+              if (last && last.length > 20) {
+                // >20确保是ObjectId,ObjectId是24位
+                params.id = last;
+              }
+            }
+            const obj = { route, method, params, userid: id, name, ip };
+            const opera = operaMethod(route, method);
+            if (opera) obj.opera = opera;
+            await ctx.service.logs.create(obj);
+          }
+        }
+      }
+    } catch (error) {
+      console.error(`${moment().format('YYYY-MM-DD HH:mm:ss')}日志生成发生错误!`);
+    } finally {
+      await next();
+    }
+  };
+};

+ 23 - 0
app/model/logs.js

@@ -0,0 +1,23 @@
+'use strict';
+const Schema = require('mongoose').Schema;
+const moment = require('moment');
+const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
+
+const Logs = {
+  ip: { type: String, required: true, maxLength: 200 },
+  userid: { type: String, required: true, maxLength: 200 }, // 操作
+  name: { type: String, required: false, maxLength: 200 }, // 用户
+  route: { type: String, required: false, maxLength: 200 }, // 路由
+  method: { type: String, required: false, maxLength: 200 }, // 用户
+  params: { type: Object, required: false }, // 参数
+  opera: { type: String, required: false }, // 操作
+  time: { type: String, required: false, maxLength: 200, default: moment().format('YYYY-MM-DD HH:mm:ss') }, // 时间
+};
+const schema = new Schema(Logs, { toJSON: { virtuals: true } });
+schema.index({ id: 1 });
+schema.plugin(metaPlugin);
+
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('Logs', schema, 'logs');
+};

+ 5 - 11
app/router.js

@@ -361,7 +361,7 @@ module.exports = app => {
   );
 
 
-  // 职责说明表设置路由
+  // 培训心得表设置路由
   router.get('/api/train/experience/docx', controller.experience.docx); // index、create、show、destroy
   router.resources('experience', '/api/train/experience', controller.experience); // index、create、show、destroy
   router.post('experience', '/api/train/experience/update/:id', controller.experience.update);
@@ -438,11 +438,7 @@ module.exports = app => {
 
   // 行政区划表设置路由
   router.resources('region', '/api/train/region', controller.region); // index、create、show、destroy
-  router.post(
-    'region',
-    '/api/train/region/update/:id',
-    controller.region.update
-  );
+  router.post('region', '/api/train/region/update/:id', controller.region.update);
 
   // 用户表设置路由
   router.get('/api/train/user/schoolregister', controller.user.schoolregister); // 学校账号一键生成
@@ -455,11 +451,7 @@ module.exports = app => {
 
   // 行政区划表设置路由
   router.resources('termquest', '/api/train/termquest', controller.termquest); // index、create、show、destroy
-  router.post(
-    'termquest',
-    '/api/train/termquest/update/:id',
-    controller.termquest.update
-  );
+  router.post('termquest', '/api/train/termquest/update/:id', controller.termquest.update);
 
   // 微信端访问地址
   router.get('/api/train/auth', controller.weixin.auth); // 微信登录
@@ -684,4 +676,6 @@ module.exports = app => {
   router.post('task', '/api/train/task/export', controller.task.export);
   // 学生证书打印状态更改
   router.post('student', '/api/train/student/printcert', controller.student.printCert);
+  // 日志
+  router.get('/api/train/logs', controller.logs.index);
 };

+ 18 - 0
app/service/logs.js

@@ -0,0 +1,18 @@
+'use strict';
+
+
+const assert = require('assert');
+const _ = require('lodash');
+const { ObjectId } = require('mongoose').Types;
+const { CrudService } = require('naf-framework-mongoose/lib/service');
+const { BusinessError, ErrorCode } = require('naf-core').Error;
+
+class LogsService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'logs');
+    this.model = this.ctx.model.Logs;
+  }
+
+}
+
+module.exports = LogsService;

+ 74 - 0
app/utils/routerword.js

@@ -0,0 +1,74 @@
+'use strict';
+module.exports = (route, method) => {
+  const rou = route.replace('/api/train/', '');
+  const keys = rou.split('/');
+  if (keys <= 0) return;
+  const routeWord = {
+    common: '工具函数相关',
+    util: '工具函数相关',
+    setting: '设置相关',
+    subject: '科目',
+    teacher: '教师',
+    task: '作业',
+    question: '问卷题库',
+    questionnaire: '问卷',
+    student: '学生',
+    headteacher: '班主任',
+    bedroom: '寝室',
+    class: '班级',
+    department: '部门',
+    location: '位置',
+    trainplan: '培训计划',
+    trainplanyear: '批次计划',
+    festival: '节假日',
+    lesson: '课表',
+    schtime: '学校时间上报',
+    teaplan: '班主任安排',
+    apply: '教师安排',
+    leave: '请假/退出',
+    group: '小组',
+    experience: '培训心得',
+    duty: '职责',
+    uploadtask: '上交作业',
+    uploadquestion: '填写问卷',
+    attendance: '考勤',
+    school: '学校',
+    nation: '字典表-民族',
+    region: '字典表-行政区划',
+    user: '用户',
+    auth: '微信登录',
+    login: '用户密码登录',
+    qrcode: '扫码登陆',
+    score: '教师分数',
+    material: '教师资料',
+    materialscore: '教师资料评分',
+    notice: '通知',
+    lessonmode: '课程模板',
+    trainmodel: '计划模板',
+    countstudent: '统计',
+    classtype: '班级类型',
+    job: '学校上传名单',
+    message: '消息',
+    personalscore: '学生个人分',
+    groupscore: '学生小组分',
+    cerconfirm: '校验学生证书资格',
+    talented: '新人才报',
+  };
+  let word = routeWord[keys[0]];
+  if (method === 'DELETE') {
+    return `${word}删除`;
+  } else if (!word.includes('相关')) {
+    if (rou.includes('update')) {
+      word = `${word}修改`;
+    } else if (rou.includes('export')) {
+      word = `${word}导出`;
+    } else if (keys.length === 1) {
+      word = `新建${word}`;
+    }
+  }
+  // const student = {
+  //   '/api/train/student/upjob': '修改职位',
+  //   '/api/train/student/deleteclass': '删除学生班级',
+  // };
+  return word;
+};

+ 1 - 1
config/config.default.js

@@ -18,7 +18,7 @@ module.exports = appInfo => {
   config.keys = appInfo.name + '_1578642242928_5726';
 
   // add your middleware config here
-  config.middleware = [];
+  config.middleware = [ 'logs' ];
 
   // add your user config here
   const userConfig = {