mirror of
https://github.com/cool-team-official/cool-admin-midway.git
synced 2025-12-28 21:00:17 +00:00
完善admin功能
This commit is contained in:
parent
e0fd305b28
commit
38f015658b
38
src/app/modules/admin/controller/sys/log.ts
Normal file
38
src/app/modules/admin/controller/sys/log.ts
Normal file
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
17
src/app/modules/admin/entity/sys/conf.ts
Normal file
17
src/app/modules/admin/entity/sys/conf.ts
Normal file
@ -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;
|
||||
}
|
||||
29
src/app/modules/admin/entity/sys/log.ts
Normal file
29
src/app/modules/admin/entity/sys/log.ts
Normal file
@ -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;
|
||||
}
|
||||
38
src/app/modules/admin/service/sys/conf.ts
Normal file
38
src/app/modules/admin/service/sys/conf.ts
Normal file
@ -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<AdminSysConfEntity>;
|
||||
|
||||
/**
|
||||
* 获得配置参数值
|
||||
* @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();
|
||||
}
|
||||
}
|
||||
62
src/app/modules/admin/service/sys/log.ts
Normal file
62
src/app/modules/admin/service/sys/log.ts
Normal file
@ -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<AdminSysLogEntity>;
|
||||
|
||||
/**
|
||||
* 记录
|
||||
* @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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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)
|
||||
// });
|
||||
|
||||
|
||||
}
|
||||
// 应用停止
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user