完善admin功能

This commit is contained in:
ap 2021-02-23 21:59:13 +08:00
parent e0fd305b28
commit 38f015658b
6 changed files with 186 additions and 6 deletions

View 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();
}
}

View 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;
}

View 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;
}

View 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();
}
}

View 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();
}
}
}

View File

@ -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)
// });
}
// 应用停止