This commit is contained in:
啊平 2021-02-04 18:37:49 +08:00
parent 2c755d84e5
commit 97676570c8
8 changed files with 106 additions and 37 deletions

View File

@ -4,10 +4,13 @@
"description": "",
"private": true,
"dependencies": {
"@midwayjs/web": "^2.7.3",
"@midwayjs/decorator": "^2.7.0",
"@midwayjs/orm": "^1.3.0",
"@midwayjs/web": "^2.7.3",
"egg": "^2.29.1",
"egg-scripts": "^2.13.0"
"egg-scripts": "^2.13.0",
"midwayjs-cool-core": "file:/Users/ap/Documents/srcs/cool-admin/midway-core/core/dist",
"mysql2": "^2.2.5"
},
"devDependencies": {
"@midwayjs/cli": "^1.2.40",
@ -17,8 +20,8 @@
"@types/jest": "^26.0.20",
"@types/node": "14",
"cross-env": "^7.0.3",
"mwts": "^1.1.2",
"jest": "^26.6.3",
"mwts": "^1.1.2",
"ts-jest": "^26.5.0",
"typescript": "^4.1.3"
},
@ -53,4 +56,4 @@
},
"author": "",
"license": "MIT"
}
}

View File

@ -8,15 +8,29 @@ export default (appInfo: EggAppInfo) => {
config.keys = appInfo.name + 'cool-admin-next';
// 中间件
//config.middleware = [];
config.middleware = [];
// 关闭安全校验
config.security = {
csrf: {
enable: false,
},
};
// 替换成midway的日志
// cool-admin特有的配置
config.cool = {
// 全局路由前缀
router: {
prefix: ''
},
// 分页配置
page: {
// 分页查询每页条数
size: 15,
}
}
// 将egg日志替换成midway
config.midwayFeature = {
replaceEggLogger: true
}

View File

@ -0,0 +1,26 @@
import { EggAppConfig, EggAppInfo, PowerPartial } from 'egg';
export type DefaultConfig = PowerPartial<EggAppConfig>;
export default (appInfo: EggAppInfo) => {
const config = {} as DefaultConfig;
config.orm = {
type: 'mysql',
host: '127.0.0.1',
port: 3306,
username: 'root',
password: '123123',
database: 'cool-admin-next',
synchronize: true,
logging: false,
}
config.logger = {
coreLogger: {
consoleLevel: 'INFO'
}
}
return config;
};

View File

@ -1,17 +1,26 @@
import { App, Configuration } from '@midwayjs/decorator';
import { ILifeCycle } from '@midwayjs/core';
import { ILifeCycle, IMidwayContainer } from '@midwayjs/core';
import { Application } from 'egg';
// import * as orm from '@midwayjs/orm';
import * as cool from 'midwayjs-cool-core';
@Configuration()
@Configuration({
imports: [
// cool-admin 官方组件 https://www.cool-js.com
cool
//orm
]
})
export class ContainerLifeCycle implements ILifeCycle {
@App()
app: Application;
// 应用启动完成
async onReady(container?: IMidwayContainer) {
//this.app.use(await this.app.generateMiddleware('reportMiddleware'));
}
// 应用停止
async onStop() {
async onReady() {
this.app.use(async (ctx, next) => {
console.log('这边请求到了')
await next();
});
}
}

View File

@ -1,19 +0,0 @@
import { Inject, Controller, Provide, Query, Get } from '@midwayjs/decorator';
import { Context } from 'egg';
import { UserService } from '../service/user';
@Provide()
@Controller('/api')
export class APIController {
@Inject()
ctx: Context;
@Inject()
userService: UserService;
@Get('/get_user')
async getUser(@Query() uid) {
const user = await this.userService.getUser({ uid });
return { success: true, message: 'OK', data: user };
}
}

View File

@ -1,10 +1,15 @@
import { Controller, Get, Provide } from '@midwayjs/decorator';
import { Get, Provide, Inject, Query } from '@midwayjs/decorator';
import { CoolController, CoolCache } from 'midwayjs-cool-core';
@Provide()
@Controller('/')
@CoolController()
export class HomeController {
@Get('/')
async home() {
return 'Hello Midwayjs!';
@Inject('cool-core:coolCache')
coolCache: CoolCache;
@Get('/1')
async home(@Query() data: string) {
console.log(await this.coolCache.set('a', data))
return await this.coolCache.get('a');
}
}

15
src/controller/home1.ts Executable file
View File

@ -0,0 +1,15 @@
import { Get, Provide, Inject } from '@midwayjs/decorator';
import { CoolController, CoolCache } from 'midwayjs-cool-core';
@Provide()
@CoolController()
export class Home1Controller {
@Inject('cool-core:coolCache')
coolCache: CoolCache;
@Get('/1')
async home() {
const data = await this.coolCache.get('a');
console.log('获得到的数据', data)
return data;
}
}

16
src/middleware/report.ts Normal file
View File

@ -0,0 +1,16 @@
import { Provide } from '@midwayjs/decorator';
import { IWebMiddleware, IMidwayWebNext } from '@midwayjs/web';
import { Context } from 'egg';
@Provide()
export class ReportMiddleware implements IWebMiddleware {
resolve() {
return async (ctx: Context, next: IMidwayWebNext) => {
const startTime = Date.now();
await next();
console.log('请求时间', Date.now() - startTime);
};
}
}