mirror of
https://github.com/cool-team-official/cool-admin-midway-packages.git
synced 2025-12-12 22:32:48 +00:00
优化CoolController 新增 serviceApis
This commit is contained in:
parent
3925e4fedb
commit
df24c6295d
@ -12,6 +12,7 @@ import { BaseService } from '../service/base';
|
|||||||
import { IMidwayApplication } from '@midwayjs/core';
|
import { IMidwayApplication } from '@midwayjs/core';
|
||||||
import { Context } from '@midwayjs/koa';
|
import { Context } from '@midwayjs/koa';
|
||||||
import { TypeORMDataSourceManager } from '@midwayjs/typeorm';
|
import { TypeORMDataSourceManager } from '@midwayjs/typeorm';
|
||||||
|
import { CoolValidateException } from '../exception/validate';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 控制器基类
|
* 控制器基类
|
||||||
@ -48,6 +49,41 @@ export abstract class BaseController {
|
|||||||
await this.setService(curdOption);
|
await this.setService(curdOption);
|
||||||
// 设置实体
|
// 设置实体
|
||||||
await this.setEntity(curdOption);
|
await this.setEntity(curdOption);
|
||||||
|
// 创建动态方法
|
||||||
|
await this.createDynamicMethods(curdOption);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建动态方法
|
||||||
|
* @param curdOption 配置
|
||||||
|
*/
|
||||||
|
private async createDynamicMethods(curdOption: CurdOption) {
|
||||||
|
if (!curdOption.serviceApis) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// 过滤出非标准方法
|
||||||
|
const customMethods = curdOption.serviceApis;
|
||||||
|
|
||||||
|
// 为每个自定义方法创建对应的控制器方法
|
||||||
|
for (const api of customMethods) {
|
||||||
|
const methodName = typeof api === 'string' ? api : api.method;
|
||||||
|
if (this[methodName]) {
|
||||||
|
continue; // 如果方法已存在则跳过
|
||||||
|
}
|
||||||
|
|
||||||
|
this[methodName] = async function () {
|
||||||
|
const { body } = this.baseCtx.request;
|
||||||
|
const serviceMethod = this.service[methodName];
|
||||||
|
|
||||||
|
if (typeof serviceMethod !== 'function') {
|
||||||
|
throw new CoolValidateException(
|
||||||
|
`Service method ${methodName} not found`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.ok(await serviceMethod.call(this.service, body));
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async before(curdOption: CurdOption) {
|
private async before(curdOption: CurdOption) {
|
||||||
|
|||||||
@ -14,13 +14,23 @@ import * as _ from 'lodash';
|
|||||||
import location from '../util/location';
|
import location from '../util/location';
|
||||||
|
|
||||||
export type ApiTypes = 'add' | 'delete' | 'update' | 'page' | 'info' | 'list';
|
export type ApiTypes = 'add' | 'delete' | 'update' | 'page' | 'info' | 'list';
|
||||||
// Crud配置
|
|
||||||
|
|
||||||
|
/** 服务映射接口 */
|
||||||
|
export type ServiceApis = {
|
||||||
|
/** 方法 */
|
||||||
|
method: string;
|
||||||
|
/** 描述 */
|
||||||
|
summary: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Crud配置
|
||||||
export interface CurdOption {
|
export interface CurdOption {
|
||||||
// 路由前缀,不配置默认是按Controller下的文件夹路径
|
// 路由前缀,不配置默认是按Controller下的文件夹路径
|
||||||
prefix?: string;
|
prefix?: string;
|
||||||
// curd api接口
|
// curd api接口
|
||||||
api: ApiTypes[];
|
api?: ApiTypes[];
|
||||||
|
// 服务映射接口
|
||||||
|
serviceApis?: (ServiceApis | string)[];
|
||||||
// 分页查询配置
|
// 分页查询配置
|
||||||
pageQueryOp?: QueryOp | Function;
|
pageQueryOp?: QueryOp | Function;
|
||||||
// 非分页查询配置
|
// 非分页查询配置
|
||||||
@ -32,7 +42,7 @@ export interface CurdOption {
|
|||||||
// info 忽略返回属性
|
// info 忽略返回属性
|
||||||
infoIgnoreProperty?: string[];
|
infoIgnoreProperty?: string[];
|
||||||
// 实体
|
// 实体
|
||||||
entity: any;
|
entity?: any;
|
||||||
// 服务
|
// 服务
|
||||||
service?: any;
|
service?: any;
|
||||||
// api标签
|
// api标签
|
||||||
@ -206,12 +216,28 @@ function saveMetadata(prefix, routerOptions, target, curdOption, module) {
|
|||||||
path: `/${path}`,
|
path: `/${path}`,
|
||||||
requestMethod: path == 'info' ? 'get' : 'post',
|
requestMethod: path == 'info' ? 'get' : 'post',
|
||||||
method: path,
|
method: path,
|
||||||
summary: apiDesc[path],
|
summary: apiDesc[path] || path,
|
||||||
description: '',
|
description: '',
|
||||||
},
|
},
|
||||||
target
|
target
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
Scope(ScopeEnum.Request)(target);
|
|
||||||
}
|
}
|
||||||
|
if (!_.isEmpty(curdOption?.serviceApis)) {
|
||||||
|
curdOption.serviceApis.forEach(api => {
|
||||||
|
const methodName = typeof api === 'string' ? api : api.method;
|
||||||
|
attachClassMetadata(
|
||||||
|
WEB_ROUTER_KEY,
|
||||||
|
{
|
||||||
|
path: `/${methodName}`,
|
||||||
|
requestMethod: 'post',
|
||||||
|
method: methodName,
|
||||||
|
summary: typeof api === 'string' ? api : api.summary,
|
||||||
|
description: '',
|
||||||
|
},
|
||||||
|
target
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
Scope(ScopeEnum.Request)(target);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -153,7 +153,7 @@ export abstract class BaseService {
|
|||||||
* @param autoSort 是否自动排序
|
* @param autoSort 是否自动排序
|
||||||
* @param connectionName 连接名称
|
* @param connectionName 连接名称
|
||||||
*/
|
*/
|
||||||
async sqlRenderPage(sql, query, autoSort = true, connectionName?) {
|
async sqlRenderPage(sql, query = {}, autoSort = true, connectionName?) {
|
||||||
return await this.service.sqlRenderPage(
|
return await this.service.sqlRenderPage(
|
||||||
sql,
|
sql,
|
||||||
query,
|
query,
|
||||||
|
|||||||
@ -42,7 +42,7 @@ class SelectQueryBuilder extends QueryBuilder_1.QueryBuilder {
|
|||||||
this.orderBys = [];
|
this.orderBys = [];
|
||||||
this.relationMetadatas = [];
|
this.relationMetadatas = [];
|
||||||
this.broadcaster = new Broadcaster_1.Broadcaster(this.queryRunner);
|
this.broadcaster = new Broadcaster_1.Broadcaster(this.queryRunner);
|
||||||
this.broadcaster.broadcastAfterQueryBuilder(this, "select");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// -------------------------------------------------------------------------
|
// -------------------------------------------------------------------------
|
||||||
@ -52,6 +52,7 @@ class SelectQueryBuilder extends QueryBuilder_1.QueryBuilder {
|
|||||||
* Gets generated SQL query without parameters being replaced.
|
* Gets generated SQL query without parameters being replaced.
|
||||||
*/
|
*/
|
||||||
getQuery() {
|
getQuery() {
|
||||||
|
this.broadcaster.broadcastAfterQueryBuilder(this, "select");
|
||||||
let sql = this.createComment();
|
let sql = this.createComment();
|
||||||
sql += this.createCteExpression();
|
sql += this.createCteExpression();
|
||||||
sql += this.createSelectExpression();
|
sql += this.createSelectExpression();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user