Skip to content

Commit

Permalink
feat: 添加操作日志;
Browse files Browse the repository at this point in the history
添加操作日志;
  • Loading branch information
Imfdj committed Apr 30, 2021
1 parent 8abdb54 commit 428836d
Show file tree
Hide file tree
Showing 11 changed files with 577 additions and 61 deletions.
76 changes: 76 additions & 0 deletions app/contract/request/operation_log.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
'use strict';

const body = {
operation_logId: {
id: { type: 'number', required: true, description: 'id' },
},
operation_logBodyReq: {
operator_id: {
type: 'number',
required: true,
min: 1,
example: '1',
description: '发起者ID',
},
status: {
type: 'number',
required: true,
min: 1,
example: '200',
description: '请求返回状态 ',
},
ip: {
type: 'string',
required: true,
min: 1,
max: 100,
trim: true,
example: '127.0.0.1',
description: '请求ip地址',
},
method: {
type: 'string',
required: true,
min: 1,
max: 15,
trim: true,
example: 'GET',
description: '请求方法',
},
url: {
type: 'string',
required: true,
min: 1,
max: 255,
trim: true,
example: '',
description: '请求路径',
},
params: {
type: 'string',
required: true,
min: 1,
max: 600,
trim: true,
example: '',
description: '请求参数',
},
},
};

module.exports = {
...body,
operation_logPutBodyReq: {
...body.operation_logId,
...body.operation_logBodyReq,
},
operation_logDelBodyReq: {
ids: {
type: 'array',
required: true,
itemType: 'number',
description: 'ids',
example: [1, 2],
},
},
};
84 changes: 84 additions & 0 deletions app/controller/v1/operation_logs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
'use strict';

const Controller = require('egg').Controller;

/**
* @controller 操作日志 operation_log
*/

class RoleController extends Controller {
/**
* @apikey
* @summary 获取 操作日志
* @description 获取 操作日志
* @request query string name operation_log名
* @request query number limit limit
* @request query number offset offset
* @router get /api/v1/operation_logs/list
*/
async findAll() {
const { ctx, service } = this;
const { allRule, query } = ctx.helper.tools.findAllParamsDeal(ctx.rule.operation_logPutBodyReq, ctx.query);
ctx.validate(allRule, query);
const res = await service.operationLogs.findAll(query);
ctx.helper.body.SUCCESS({ ctx, res });
}

/**
* @apikey
* @summary 获取某个 操作日志
* @description 获取某个 操作日志
* @router get /api/v1/operation_logs
* @request query number *id eg:1 operation_logID
*/
async findOne() {
const { ctx, service } = this;
ctx.validate(ctx.rule.operation_logId, ctx.query);
const res = await service.operationLogs.findOne(ctx.query.id);
res ? ctx.helper.body.SUCCESS({ ctx, res }) : ctx.helper.body.NOT_FOUND({ ctx });
}

/**
* @apikey
* @summary 创建 操作日志
* @description 创建 操作日志
* @router post /api/v1/operation_logs
* @request body operation_logBodyReq
*/
async create() {
const ctx = this.ctx;
ctx.validate(ctx.rule.operation_logBodyReq, ctx.request.body);
await ctx.service.operationLogs.create(ctx.request.body);
ctx.helper.body.CREATED_UPDATE({ ctx });
}

/**
* @apikey
* @summary 更新 操作日志
* @description 更新 操作日志
* @router put /api/v1/operation_logs
* @request body operation_logPutBodyReq
*/
async update() {
const { ctx, service } = this;
ctx.validate(ctx.rule.operation_logPutBodyReq, ctx.request.body);
const res = await service.operationLogs.update(ctx.request.body);
res && res[0] !== 0 ? ctx.helper.body.CREATED_UPDATE({ ctx }) : ctx.helper.body.NOT_FOUND({ ctx });
}

/**
* @apikey
* @summary 批量删除 操作日志
* @description 批量删除 操作日志
* @router delete /api/v1/operation_logs
* @request body operation_logDelBodyReq
*/
async destroy() {
const { ctx, service } = this;
ctx.validate(ctx.rule.operation_logDelBodyReq, ctx.request.body);
const res = await service.operationLogs.destroy(ctx.request.body);
res ? ctx.helper.body.NO_CONTENT({ ctx, res }) : ctx.helper.body.NOT_FOUND({ ctx });
}
}

module.exports = RoleController;
23 changes: 23 additions & 0 deletions app/middleware/log_handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

module.exports = (option, app) => {
return async function(ctx, next) {
try {
await next();
// 如果是开发环境或者是生产环境 存储操作日志
if (app.config.env === 'prod' || app.config.env === 'local') {
const payload = {
operator_id: ctx.currentRequestData.userInfo.id,
method: ctx.request.method,
url: ctx.request.url.split('?')[0],
ip: ctx.request.ip,
status: ctx.response.status,
params: ctx.request.method.toLocaleUpperCase() === 'GET' ? JSON.stringify(ctx.request.query) : JSON.stringify(ctx.request.body),
};
ctx.service.operationLogs.create(payload);
}
} catch (err) {
app.logger.errorAndSentry(err);
}
};
};
22 changes: 22 additions & 0 deletions app/model/operation_logs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';
module.exports = app => {
const Sequelize = app.Sequelize;

const operation_log = app.model.define(
'operation_logs',
{
id: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true },
operator_id: Sequelize.INTEGER(11),
status: Sequelize.INTEGER(11),
ip: Sequelize.STRING(100),
method: Sequelize.STRING(15),
url: Sequelize.STRING(255),
params: Sequelize.TEXT,
},
{}
);
operation_log.associate = function(models) {
// associations can be defined here
};
return operation_log;
};
12 changes: 12 additions & 0 deletions app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,11 +278,23 @@ module.exports = app => {
router.get('/api/v1/messages', controller.v1.messages.findOne);
router.delete('/api/v1/messages', controller.v1.messages.destroy);

/**
* 邀请
*/
router.post('/api/v1/invites', controller.v1.invites.create);
router.put('/api/v1/invites', controller.v1.invites.update);
router.get('/api/v1/invites/list', controller.v1.invites.findAll);
router.get('/api/v1/invites', controller.v1.invites.findOne);
router.delete('/api/v1/invites', controller.v1.invites.destroy);
router.get('/api/v1/invites/valid', controller.v1.invites.findValidOne);
router.get('/api/v1/invites/uuid', controller.v1.invites.findOneByUUID);

/**
* 操作日志
*/
router.post('/api/v1/operation_logs', controller.v1.operationLogs.create);
router.put('/api/v1/operation_logs', controller.v1.operationLogs.update);
router.get('/api/v1/operation_logs/list', controller.v1.operationLogs.findAll);
router.get('/api/v1/operation_logs', controller.v1.operationLogs.findOne);
router.delete('/api/v1/operation_logs', controller.v1.operationLogs.destroy);
};
47 changes: 47 additions & 0 deletions app/service/operation_logs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict';

const Service = require('egg').Service;
const { Op } = require('sequelize');

class _objectName_Service extends Service {
async findAll(payload) {
const { ctx } = this;
const { limit, offset, prop_order, order, name } = payload;
const where = payload.where;
const Order = [];
name ? (where.name = { [Op.like]: `%${name}%` }) : null;
prop_order && order ? Order.push([prop_order, order]) : null;
return await ctx.model.OperationLogs.findAndCountAll({
limit,
offset,
where,
order: Order,
});
}

async findOne(id) {
const { ctx } = this;
return await ctx.model.OperationLogs.findOne({ where: { id } });
}

async create(payload) {
const { ctx } = this;
return await ctx.model.OperationLogs.create(payload);
}

async update(payload) {
const { ctx } = this;
return await ctx.model.OperationLogs.update(payload, {
where: { id: payload.id },
});
}

async destroy(payload) {
const { ctx } = this;
return await ctx.model.OperationLogs.destroy({
where: { id: payload.ids },
});
}
}

module.exports = _objectName_Service;
64 changes: 64 additions & 0 deletions database/migrations/20200110080210-create-operation_log.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable(
'operation_logs',
{
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER(11).UNSIGNED,
},
operator_id: {
type: Sequelize.INTEGER(11).UNSIGNED,
allowNull: false,
comment: '发起者ID',
references: {
model: 'users',
key: 'id',
},
onUpdate: 'CASCADE',
onDelete: 'CASCADE',
},
status: {
type: Sequelize.INTEGER(11),
allowNull: false,
comment: '请求返回状态',
},
ip: {
type: Sequelize.STRING(100),
allowNull: false,
comment: '请求ip地址',
},
method: {
type: Sequelize.STRING(15),
allowNull: false,
comment: '请求方法',
},
url: {
type: Sequelize.STRING(255),
allowNull: false,
comment: '请求路径',
},
params: {
type: Sequelize.TEXT,
allowNull: false,
comment: '请求参数',
},
created_at: {
allowNull: false,
type: Sequelize.DATE,
},
updated_at: {
allowNull: false,
type: Sequelize.DATE,
},
},
{}
);
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('operation_logs');
},
};
Loading

0 comments on commit 428836d

Please sign in to comment.