diff --git a/package.json b/package.json index 911ddef..0042a3a 100755 --- a/package.json +++ b/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" -} \ No newline at end of file +} diff --git a/src/config/config.default.ts b/src/config/config.default.ts index 6670ac7..487324c 100644 --- a/src/config/config.default.ts +++ b/src/config/config.default.ts @@ -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; }; diff --git a/src/config/config.local.ts b/src/config/config.local.ts index e69de29..4c9bb94 100644 --- a/src/config/config.local.ts +++ b/src/config/config.local.ts @@ -0,0 +1,20 @@ +import { EggAppConfig, EggAppInfo, PowerPartial } from 'egg'; + +export type DefaultConfig = PowerPartial; + +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; +}; diff --git a/src/configuration.ts b/src/configuration.ts new file mode 100644 index 0000000..c43bc57 --- /dev/null +++ b/src/configuration.ts @@ -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 { + + 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 { + console.log('应用停止') + } + +} \ No newline at end of file diff --git a/src/controller/api.ts b/src/controller/api.ts index 1e75740..728c22d 100644 --- a/src/controller/api.ts +++ b/src/controller/api.ts @@ -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; + + @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 }; } } diff --git a/src/decorator/CoolController.ts b/src/decorator/CoolController.ts new file mode 100644 index 0000000..b4184a5 --- /dev/null +++ b/src/decorator/CoolController.ts @@ -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); + }; +} \ No newline at end of file diff --git a/src/middleware/report.ts b/src/middleware/report.ts new file mode 100644 index 0000000..fe79760 --- /dev/null +++ b/src/middleware/report.ts @@ -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); + }; + } + +} \ No newline at end of file diff --git a/src/model/user.ts b/src/model/user.ts new file mode 100644 index 0000000..e9950e1 --- /dev/null +++ b/src/model/user.ts @@ -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; +} \ No newline at end of file diff --git a/typings/app/index.d.ts b/typings/app/index.d.ts new file mode 100644 index 0000000..69543bc --- /dev/null +++ b/typings/app/index.d.ts @@ -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; diff --git a/typings/config/index.d.ts b/typings/config/index.d.ts new file mode 100644 index 0000000..ca60818 --- /dev/null +++ b/typings/config/index.d.ts @@ -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; + } +} \ No newline at end of file diff --git a/typings/config/plugin.d.ts b/typings/config/plugin.d.ts new file mode 100644 index 0000000..e857358 --- /dev/null +++ b/typings/config/plugin.d.ts @@ -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; + } +} \ No newline at end of file