mirror of
https://github.com/cool-team-official/cool-admin-midway.git
synced 2026-02-26 16:00:29 +00:00
111
This commit is contained in:
parent
8ef59d44ca
commit
d942aacccb
10
package.json
10
package.json
@ -4,10 +4,12 @@
|
||||
"description": "",
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"@midwayjs/web": "^2.3.0",
|
||||
"@midwayjs/decorator": "^2.3.0",
|
||||
"@midwayjs/orm": "^1.3.0",
|
||||
"@midwayjs/web": "^2.3.0",
|
||||
"egg": "^2.0.0",
|
||||
"egg-scripts": "^2.10.0"
|
||||
"egg-scripts": "^2.10.0",
|
||||
"mysql": "^2.18.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@midwayjs/cli": "^1.0.0",
|
||||
@ -16,8 +18,8 @@
|
||||
"@types/jest": "^26.0.10",
|
||||
"@types/node": "14",
|
||||
"cross-env": "^6.0.0",
|
||||
"mwts": "^1.0.5",
|
||||
"jest": "^26.4.0",
|
||||
"mwts": "^1.0.5",
|
||||
"ts-jest": "^26.2.0",
|
||||
"typescript": "^3.9.0"
|
||||
},
|
||||
@ -46,4 +48,4 @@
|
||||
},
|
||||
"author": "anonymous",
|
||||
"license": "MIT"
|
||||
}
|
||||
}
|
||||
|
||||
@ -9,7 +9,7 @@ export default (appInfo: EggAppInfo) => {
|
||||
config.keys = appInfo.name + '_1611125891022_7317';
|
||||
|
||||
// add your config here
|
||||
config.middleware = [];
|
||||
config.middleware = ['reportMiddleware'];
|
||||
|
||||
return config;
|
||||
};
|
||||
|
||||
@ -0,0 +1,20 @@
|
||||
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: 'midway',
|
||||
synchronize: true,
|
||||
logging: false,
|
||||
}
|
||||
|
||||
return config;
|
||||
};
|
||||
44
src/configuration.ts
Normal file
44
src/configuration.ts
Normal file
@ -0,0 +1,44 @@
|
||||
import { App, Configuration } from '@midwayjs/decorator';
|
||||
import { ILifeCycle, IMidwayContainer } from '@midwayjs/core';
|
||||
import * as orm from '@midwayjs/orm';
|
||||
import { listModule } from '@midwayjs/decorator';
|
||||
import { Application } from 'egg';
|
||||
|
||||
|
||||
@Configuration({
|
||||
imports: [
|
||||
orm
|
||||
],
|
||||
})
|
||||
export class ContainerLifeCycle implements ILifeCycle {
|
||||
|
||||
@App()
|
||||
app: Application;
|
||||
|
||||
async onReady(container?: IMidwayContainer): Promise<void> {
|
||||
|
||||
console.log('加载配置')
|
||||
// this.app.use(async (ctx, next) => {
|
||||
// console.log('这边请求到了')
|
||||
// await next();
|
||||
// });
|
||||
|
||||
|
||||
|
||||
|
||||
const MODEL_KEY = 'decorator:model';
|
||||
|
||||
// 可以获取到所有装饰了 @Model 装饰器的 class
|
||||
const modules = listModule(MODEL_KEY);
|
||||
for (let mod of modules) {
|
||||
console.log(666, mod)
|
||||
// 实现自定义能力
|
||||
// 从 mod 上拿元数据,做不同的处理
|
||||
// 提前初始化等 app.applicationContext.getAsync(getProvideId(mod));
|
||||
}
|
||||
}
|
||||
async onStop?(container?: IMidwayContainer): Promise<void> {
|
||||
console.log('应用停止')
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,6 +1,9 @@
|
||||
import { Inject, Controller, Post, Provide, Query } from '@midwayjs/decorator';
|
||||
import { Inject, Controller, Provide, Query, Get } from '@midwayjs/decorator';
|
||||
import { Context } from 'egg';
|
||||
import { UserService } from '../service/user';
|
||||
import { Repository } from 'typeorm';
|
||||
import { User } from '../model/user';
|
||||
import { InjectEntityModel } from '@midwayjs/orm';
|
||||
|
||||
@Provide()
|
||||
@Controller('/api')
|
||||
@ -11,9 +14,15 @@ export class APIController {
|
||||
@Inject()
|
||||
userService: UserService;
|
||||
|
||||
@Post('/get_user')
|
||||
@InjectEntityModel(User)
|
||||
userModel: Repository<User>;
|
||||
|
||||
@Get('/get_user')
|
||||
async getUser(@Query() uid) {
|
||||
const user = await this.userService.getUser({ uid });
|
||||
|
||||
console.log(await this.userModel.findOne({id: 1}))
|
||||
|
||||
return { success: true, message: 'OK', data: user };
|
||||
}
|
||||
}
|
||||
|
||||
20
src/decorator/CoolController.ts
Normal file
20
src/decorator/CoolController.ts
Normal file
@ -0,0 +1,20 @@
|
||||
import { Scope, ScopeEnum, saveClassMetadata, saveModule } from '@midwayjs/decorator';
|
||||
|
||||
const MODEL_KEY = 'decorator:model';
|
||||
|
||||
export function CoolController(): ClassDecorator {
|
||||
return (target: any) => {
|
||||
// 将装饰的类,绑定到该装饰器,用于后续能获取到 class
|
||||
saveModule(MODEL_KEY, target);
|
||||
// 保存一些元数据信息,任意你希望存的东西
|
||||
saveClassMetadata(
|
||||
MODEL_KEY,
|
||||
{
|
||||
test: 'abc'
|
||||
},
|
||||
target
|
||||
);
|
||||
// 指定 IoC 容器创建实例的作用域,这里注册为请求作用域,这样能取到 ctx
|
||||
Scope(ScopeEnum.Request)(target);
|
||||
};
|
||||
}
|
||||
17
src/middleware/report.ts
Normal file
17
src/middleware/report.ts
Normal file
@ -0,0 +1,17 @@
|
||||
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();
|
||||
console.log(666)
|
||||
await next();
|
||||
console.log('请求时间', Date.now() - startTime);
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
11
src/model/user.ts
Normal file
11
src/model/user.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import { EntityModel } from '@midwayjs/orm';
|
||||
import { PrimaryGeneratedColumn, Column } from 'typeorm';
|
||||
|
||||
@EntityModel('user')
|
||||
export class User {
|
||||
@PrimaryGeneratedColumn({ name: "id" })
|
||||
id: number;
|
||||
|
||||
@Column({ name: "name" })
|
||||
name: string;
|
||||
}
|
||||
7
typings/app/index.d.ts
vendored
Normal file
7
typings/app/index.d.ts
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
// This file is created by egg-ts-helper@1.25.8
|
||||
// Do not modify this file!!!!!!!!!
|
||||
|
||||
import 'egg';
|
||||
import '@midwayjs/web';
|
||||
export * from 'egg';
|
||||
export as namespace Egg;
|
||||
32
typings/config/index.d.ts
vendored
Normal file
32
typings/config/index.d.ts
vendored
Normal file
@ -0,0 +1,32 @@
|
||||
// This file is created by egg-ts-helper@1.25.8
|
||||
// Do not modify this file!!!!!!!!!
|
||||
|
||||
import '@midwayjs/web';
|
||||
import 'egg';
|
||||
import 'egg-onerror';
|
||||
import 'egg-session';
|
||||
import 'egg-i18n';
|
||||
import 'egg-multipart';
|
||||
import 'egg-security';
|
||||
import 'egg-schedule';
|
||||
import 'egg-jsonp';
|
||||
import 'egg-view';
|
||||
import 'midway-schedule';
|
||||
import { EggPluginItem } from 'egg';
|
||||
declare module 'egg' {
|
||||
interface EggPlugin {
|
||||
onerror?: EggPluginItem;
|
||||
session?: EggPluginItem;
|
||||
i18n?: EggPluginItem;
|
||||
watcher?: EggPluginItem;
|
||||
multipart?: EggPluginItem;
|
||||
security?: EggPluginItem;
|
||||
development?: EggPluginItem;
|
||||
logrotator?: EggPluginItem;
|
||||
schedule?: EggPluginItem;
|
||||
static?: EggPluginItem;
|
||||
jsonp?: EggPluginItem;
|
||||
view?: EggPluginItem;
|
||||
schedulePlus?: EggPluginItem;
|
||||
}
|
||||
}
|
||||
31
typings/config/plugin.d.ts
vendored
Normal file
31
typings/config/plugin.d.ts
vendored
Normal file
@ -0,0 +1,31 @@
|
||||
// This file is created by egg-ts-helper@1.25.8
|
||||
// Do not modify this file!!!!!!!!!
|
||||
|
||||
import 'egg';
|
||||
import 'egg-onerror';
|
||||
import 'egg-session';
|
||||
import 'egg-i18n';
|
||||
import 'egg-multipart';
|
||||
import 'egg-security';
|
||||
import 'egg-schedule';
|
||||
import 'egg-jsonp';
|
||||
import 'egg-view';
|
||||
import 'midway-schedule';
|
||||
import { EggPluginItem } from 'egg';
|
||||
declare module 'egg' {
|
||||
interface EggPlugin {
|
||||
onerror?: EggPluginItem;
|
||||
session?: EggPluginItem;
|
||||
i18n?: EggPluginItem;
|
||||
watcher?: EggPluginItem;
|
||||
multipart?: EggPluginItem;
|
||||
security?: EggPluginItem;
|
||||
development?: EggPluginItem;
|
||||
logrotator?: EggPluginItem;
|
||||
schedule?: EggPluginItem;
|
||||
static?: EggPluginItem;
|
||||
jsonp?: EggPluginItem;
|
||||
view?: EggPluginItem;
|
||||
schedulePlus?: EggPluginItem;
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user