forked from Imfdj/egg-beehive
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
添加操作日志;
- Loading branch information
Showing
11 changed files
with
577 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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], | ||
}, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
64
database/migrations/20200110080210-create-operation_log.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}, | ||
}; |
Oops, something went wrong.