This commit is contained in:
啊平 2021-02-22 19:04:26 +08:00
parent 73b75eeb00
commit 669e7f0232
7 changed files with 86 additions and 27 deletions

31
.vscode/controller.code-snippets vendored Normal file
View File

@ -0,0 +1,31 @@
{
"controller": {
"prefix": "controller",
"body": [
"import { BaseController } from 'egg-cool-controller';",
"import router from 'egg-cool-router';\n",
"/**",
"* 控制器功能描述",
"*/",
"@router.prefix(['add', 'delete', 'update', 'info', 'list', 'page'])",
"export default class XXXController extends BaseController {",
"\tinit() {",
"\t\tthis.setEntity(this.ctx.repo.xxx);",
"\t\tthis.setService(this.ctx.service.xxx);",
"\t}\n",
"\t/**",
"\t* 其他接口",
"\t*/",
"\t@router.post('/xxx')",
"\tasync xxx() {",
"\t\tawait this.OpService.xxx(this.getBody());",
"\t\tthis.res();",
"\t}",
"}"
],
"description": "cool-admin controller代码片段 node后端"
}
}

View File

@ -7,8 +7,8 @@ export default (appInfo: EggAppInfo) => {
// cookie sign key
config.keys = appInfo.name + 'cool-admin-next';
// 中间件
config.middleware = ['reportMiddleware'];
// 启用中间件 这里需要设置为 [] 否则CoolController设置的中间件也会无效
config.middleware = [];
// 关闭安全校验
config.security = {

View File

@ -13,7 +13,7 @@ export default (appInfo: EggAppInfo) => {
password: '123123',
database: 'cool-admin-next',
synchronize: true,
logging: false,
logging: true,
}
config.logger = {

View File

@ -1,28 +1,26 @@
import { Get, Provide, Inject, Query, Body, ALL, Post, Validate } from '@midwayjs/decorator';
import { Get, Provide, Inject, Query, Post } from '@midwayjs/decorator';
import { CoolController, CoolCache, BaseController, RESCODE } from 'midwayjs-cool-core';
//import { InjectEntityModel } from '@midwayjs/orm';
import { User } from '../entity/user';
import { TestDTO } from '../dto/test';
import { InjectEntityModel } from '@midwayjs/orm';
import { Repository } from 'typeorm';
import { Role } from '../entity/role';
@Provide()
@CoolController({
api: ['info', 'add'],
entity: User,
infoIgnoreProperty: ['age']
api: ['add','delete','update','info','list','page'],
entity: 实体,
pageQueryOp: {
fieldEq: ['id', 'name'],
keyWordLikeFields: ['a.name'],
select: ['a.*, b.name AS roleName'],
leftJoin: [
{
entity: Role,
alias: 'b',
condition: 'a.id = b.userId'
}
]
}
})
export class HomeController extends BaseController {
@Inject('cool:cache')
coolCache: CoolCache;
@Get('/1')
async home(@Query() data: string) {
//console.log(this.coolCache)
await this.coolCache.set('a', data);
return await this.coolCache.get('a');
}
@Post('/2')
async 2(@Body(ALL) test: TestDTO) {
return this.fail('哈哈', RESCODE.VALIDATEFAIL);
}
export class XxxController extends BaseController {
}

14
src/entity/role.ts Normal file
View File

@ -0,0 +1,14 @@
import { EntityModel } from '@midwayjs/orm';
import { BaseModel } from 'midwayjs-cool-core';
import { Column } from 'typeorm';
@EntityModel('role')
export class Role extends BaseModel {
@Column()
name: string;
@Column('bigint')
userId: number;
}

View File

@ -6,11 +6,11 @@ import { Rule, RuleType } from "@midwayjs/decorator";
@EntityModel('user')
export class User extends BaseModel {
@Rule(RuleType.number().required())
@Rule(RuleType.string().required())
@Column()
name: string;
@Rule(RuleType.number().max(60))
@Rule(RuleType.number().max(18))
@Column('int')
age: number;

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

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