diff --git a/src/app/modules/admin/controller/sys/log.ts b/src/app/modules/admin/controller/sys/log.ts new file mode 100644 index 0000000..ee84412 --- /dev/null +++ b/src/app/modules/admin/controller/sys/log.ts @@ -0,0 +1,38 @@ +import { Provide, Post, Inject } from '@midwayjs/decorator'; +import { CoolController, BaseController } from 'midwayjs-cool-core'; +import { AdminSysLogEntity } from '../../entity/sys/log'; +import { AdminSysUserEntity } from '../../entity/sys/user'; +import { AdminSysLogService } from '../../service/sys/log'; + +/** + * 系统日志 + */ +@Provide() +@CoolController({ + api: ['page'], + entity: AdminSysLogEntity, + pageQueryOp: { + keyWordLikeFields: ['b.name', 'a.params', 'a.ipAddr'], + select: ['a.*, b.name'], + leftJoin: [{ + entity: AdminSysUserEntity, + alias: 'b', + condition: 'a.userId = b.id' + }] + } +}) +export class AdminSysLogController extends BaseController { + + @Inject() + adminSysLogService: AdminSysLogService; + + /** + * 清空日志 + */ + @Post('/clear') + public async clear() { + await this.adminSysLogService.clear(true); + this.ok(); + } + +} \ No newline at end of file diff --git a/src/app/modules/admin/entity/sys/conf.ts b/src/app/modules/admin/entity/sys/conf.ts new file mode 100644 index 0000000..f9c13dd --- /dev/null +++ b/src/app/modules/admin/entity/sys/conf.ts @@ -0,0 +1,17 @@ +import { Column, Index } from 'typeorm'; +import { EntityModel } from '@midwayjs/orm'; +import { BaseEntity } from 'midwayjs-cool-core'; + +/** + * 系统配置 + */ +@EntityModel('admin_sys_conf') +export class AdminSysConfEntity extends BaseEntity { + + @Index({ unique: true }) + @Column({ comment: '配置键' }) + cKey: string; + + @Column({ comment: '配置值' }) + cValue: string; +} diff --git a/src/app/modules/admin/entity/sys/log.ts b/src/app/modules/admin/entity/sys/log.ts new file mode 100644 index 0000000..2049ce6 --- /dev/null +++ b/src/app/modules/admin/entity/sys/log.ts @@ -0,0 +1,29 @@ +import { EntityModel } from '@midwayjs/orm'; +import { BaseEntity } from 'midwayjs-cool-core'; +import { Column, Index } from 'typeorm'; + +/** + * 系统日志 + */ +@EntityModel('admin_sys_log') +export class AdminSysLogEntity extends BaseEntity { + + @Index() + @Column({ comment: '用户ID', nullable: true, type: 'bigint' }) + userId: number; + + @Index() + @Column({ comment: '行为', length: 100 }) + action: string; + + @Index() + @Column({ comment: 'ip', nullable: true, length: 50 }) + ip: string; + + @Index() + @Column({ comment: 'ip地址', nullable: true, length: 50 }) + ipAddr: string; + + @Column({ comment: '参数', nullable: true, type: 'text' }) + params: string; +} diff --git a/src/app/modules/admin/service/sys/conf.ts b/src/app/modules/admin/service/sys/conf.ts new file mode 100644 index 0000000..815f999 --- /dev/null +++ b/src/app/modules/admin/service/sys/conf.ts @@ -0,0 +1,38 @@ +import { Provide } from '@midwayjs/decorator'; +import { BaseService } from 'midwayjs-cool-core'; +import { InjectEntityModel } from '@midwayjs/orm'; +import { Repository } from 'typeorm'; +import { AdminSysConfEntity } from '../../entity/sys/conf'; + +/** + * 系统配置 + */ +@Provide() +export class AdminSysConfService extends BaseService { + + @InjectEntityModel(AdminSysConfEntity) + adminSysConfEntity: Repository; + + /** + * 获得配置参数值 + * @param key + */ + async getValue(key) { + const conf = await this.adminSysConfEntity.findOne({ cKey: key }); + if (conf) { + return conf.cValue; + } + } + + /** + * 更新配置参数 + * @param cKey + * @param cValue + */ + async updateVaule(cKey, cValue) { + await this.adminSysConfEntity.createQueryBuilder() + .update() + .set({ cKey, cValue }) + .execute(); + } +} \ No newline at end of file diff --git a/src/app/modules/admin/service/sys/log.ts b/src/app/modules/admin/service/sys/log.ts new file mode 100644 index 0000000..a30daec --- /dev/null +++ b/src/app/modules/admin/service/sys/log.ts @@ -0,0 +1,62 @@ +import { Inject, Provide } from '@midwayjs/decorator'; +import { BaseService } from 'midwayjs-cool-core'; +import { InjectEntityModel } from '@midwayjs/orm'; +import { Repository } from 'typeorm'; +import { Context } from 'egg'; +import * as _ from 'lodash'; +import { AdminSysLogEntity } from '../../entity/sys/log'; +import * as moment from 'moment'; + +/** + * 描述 + */ +@Provide() +export class AdminSysLogService extends BaseService { + + @Inject() + ctx: Context; + + @InjectEntityModel(AdminSysLogEntity) + adminSysLogEntity: Repository; + + /** + * 记录 + * @param url URL地址 + * @param params 参数 + * @param userId 用户ID + */ + async record(url, params, userId) { + const ip = await this.ctx.helper.getReqIP(); + const sysLog = new AdminSysLogEntity(); + sysLog.userId = userId; + sysLog.ip = ip; + const ipAddrArr = new Array(); + for (const e of ip.split(',')) ipAddrArr.push(await this.ctx.helper.getIpAddr(e)); + sysLog.ipAddr = ipAddrArr.join(','); + sysLog.action = url; + if (!_.isEmpty(params)) { + sysLog.params = JSON.stringify(params); + } + await this.adminSysLogEntity.insert(sysLog); + } + + /** + * 日志 + * @param isAll 是否清除全部 + */ + async clear(isAll?) { + if (isAll) { + await this.adminSysLogEntity.clear(); + return; + } + const keepDay = await this.ctx.service.sys.conf.getValue('logKeep'); + if (keepDay) { + const beforeDate = `${moment().subtract(keepDay, 'days').format('YYYY-MM-DD')} 00:00:00`; + await this.adminSysLogEntity.createQueryBuilder() + .where('createTime < :createTime', { createTime: beforeDate }) + await this.nativeQuery(' delete from sys_log where createTime < ? ', [beforeDate]); + } else { + await this.adminSysLogEntity.clear(); + } + } +} \ No newline at end of file diff --git a/src/configuration.ts b/src/configuration.ts index 372a521..13e6dce 100644 --- a/src/configuration.ts +++ b/src/configuration.ts @@ -3,14 +3,13 @@ import { ILifeCycle, IMidwayContainer } from '@midwayjs/core'; import { Application } from 'egg'; import * as orm from '@midwayjs/orm'; import * as cool from 'midwayjs-cool-core'; -import * as bodyParser from 'koa-bodyparser'; //import * as redis from 'midwayjs-cool-redis'; @Configuration({ // 注意组件顺序 cool 有依赖orm组件, 所以必须放在,orm组件之后 cool的其他组件必须放在cool 核心组件之后 imports: [ // 必须,不可移除, https://typeorm.io 打不开? https://typeorm.biunav.com/zh/ - //orm, + orm, // 必须,不可移除, cool-admin 官方组件 https://www.cool-js.com cool, //redis @@ -23,10 +22,7 @@ export class ContainerLifeCycle implements ILifeCycle { app: Application; // 应用启动完成 async onReady(container?: IMidwayContainer) { - // this.app.use(bodyParser()); - // this.app.use(async (ctx: Context, next)=>{ - // console.log(ctx.request.body) - // }); + } // 应用停止