From fb67148f4320f9d85cb2c4e511bf9fa470e20e63 Mon Sep 17 00:00:00 2001 From: cool Date: Sat, 18 Nov 2023 15:37:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84Eps=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/src/configuration.ts | 9 +++----- core/src/decorator/controller.ts | 34 +++++++++++++++++++----------- core/src/interface.ts | 2 ++ core/src/rest/eps.ts | 36 ++++++++++++++++++++++++++++---- core/src/tag/data.ts | 7 +++++-- 5 files changed, 64 insertions(+), 24 deletions(-) diff --git a/core/src/configuration.ts b/core/src/configuration.ts index 4204797..ba99bdb 100644 --- a/core/src/configuration.ts +++ b/core/src/configuration.ts @@ -55,12 +55,9 @@ export class CoolConfiguration implements ILifeCycle { this.app.useFilter([CoolExceptionFilter]); // 装饰器 await container.getAsync(CoolDecorator); - - if (this.app.getEnv() == "local") { - // 实体与路径 - const eps: CoolEps = await container.getAsync(CoolEps); - eps.init(); - } + // 实体与路径 + const eps: CoolEps = await container.getAsync(CoolEps); + eps.init(); // 缓存设置为全局 global["COOL-CACHE"] = await container.getAsync(CacheManager); // 清除 location diff --git a/core/src/decorator/controller.ts b/core/src/decorator/controller.ts index b072f12..76d9482 100644 --- a/core/src/decorator/controller.ts +++ b/core/src/decorator/controller.ts @@ -97,25 +97,35 @@ export interface ControllerOption { }; } +// 路由配置 +export interface RouterOptions { + sensitive?: boolean; + middleware?: MiddlewareParamArray; + description?: string; + tagName?: string; + ignoreGlobalPrefix?: boolean; +} + // COOL的装饰器 export function CoolController( - curdOption?: CurdOption | string, - routerOptions: { - sensitive?: boolean; - middleware?: MiddlewareParamArray; - description?: string; - tagName?: string; - ignoreGlobalPrefix?: boolean; - } = { middleware: [], sensitive: true } + curdOption?: CurdOption | string | RouterOptions, + routerOptions: RouterOptions = { middleware: [], sensitive: true } ): ClassDecorator { return (target: any) => { // 将装饰的类,绑定到该装饰器,用于后续能获取到 class saveModule(CONTROLLER_KEY, target); let prefix; - if (typeof curdOption === "string") { - prefix = curdOption; - } else { - prefix = curdOption?.prefix || ""; + if(curdOption){ + // 判断 curdOption 的类型 + if (typeof curdOption === "string") { + prefix = curdOption; + } else if (curdOption && 'api' in curdOption) { + // curdOption 是 CurdOption 类型 + prefix = curdOption.prefix || ""; + } else { + // curdOption 是 RouterOptions 类型 合并到 routerOptions + routerOptions = { ...curdOption , ...routerOptions, }; + } } // 如果不存在路由前缀,那么自动根据当前文件夹路径 location.scriptPath(target).then(async (res: any) => { diff --git a/core/src/interface.ts b/core/src/interface.ts index 7ab8c0d..12b997d 100644 --- a/core/src/interface.ts +++ b/core/src/interface.ts @@ -23,6 +23,8 @@ export interface CoolConfig { sms?: CoolSmsConfig, /** 是否自动导入数据库 */ initDB?: boolean; + /** Eps */ + eps?: boolean; /** 是否自动导入模块菜单 */ initMenu?: boolean; // 实体配置 diff --git a/core/src/rest/eps.ts b/core/src/rest/eps.ts index 623b733..7f6663a 100644 --- a/core/src/rest/eps.ts +++ b/core/src/rest/eps.ts @@ -7,8 +7,10 @@ import { ScopeEnum, } from "@midwayjs/decorator"; import * as _ from "lodash"; -import { Inject, MidwayWebRouterService } from "@midwayjs/core"; +import { Config, Inject, MidwayWebRouterService } from "@midwayjs/core"; import { TypeORMDataSourceManager } from "@midwayjs/typeorm"; +import { CoolUrlTagData } from "../tag/data"; +import { TagTypes } from "../decorator/tag"; /** * 实体路径 @@ -26,18 +28,38 @@ export class CoolEps { @Inject() typeORMDataSourceManager: TypeORMDataSourceManager; + @Config('cool.eps') + epsConfig: boolean; + + @Config('module') + moduleConfig: any; + + @Inject() + coolUrlTagData: CoolUrlTagData; + // @Init() async init() { + if(!this.epsConfig) return; const entitys = await this.entity(); const controllers = await this.controller(); const routers = await this.router(); const adminArr = []; const appArr = []; for (const controller of controllers) { - const { prefix, module, curdOption } = controller; + const { prefix, module, moduleConfig, curdOption, routerOptions } = controller; const name = curdOption?.entity?.name; (_.startsWith(prefix, "/admin/") ? adminArr : appArr).push({ module, + info: { + module: { + name: moduleConfig?.name, + description: moduleConfig?.description, + }, + type: { + name: prefix.split("/").pop(), + description: routerOptions?.description || "" , + }, + }, api: routers[prefix], name, columns: entitys[name] || [], @@ -56,9 +78,10 @@ export class CoolEps { const result = []; const controllers = listModule(CONTROLLER_KEY); for (const controller of controllers) { - result.push(getClassMetadata(CONTROLLER_KEY, controller)); + const data = getClassMetadata(CONTROLLER_KEY, controller); + data.moduleConfig = this.moduleConfig[data.module]; + result.push(data); } - return result; } @@ -67,6 +90,10 @@ export class CoolEps { * @returns */ async router() { + let ignoreUrls: string[] = this.coolUrlTagData.byKey(TagTypes.IGNORE_TOKEN); + if(_.isEmpty(ignoreUrls)){ + ignoreUrls = []; + } return _.groupBy( (await await this.midwayWebRouterService.getFlattenRouterTable()).map( (item) => { @@ -77,6 +104,7 @@ export class CoolEps { dts: {}, tag: "", prefix: item.prefix, + ignoreToken: ignoreUrls.includes(item.prefix+item.url), }; } ), diff --git a/core/src/tag/data.ts b/core/src/tag/data.ts index d01b32f..eb57354 100644 --- a/core/src/tag/data.ts +++ b/core/src/tag/data.ts @@ -55,9 +55,12 @@ export class CoolUrlTagData { /** * 根据键获得 * @param key + * @param type * @returns */ - byKey(key: string): string[] { - return this.data[key]; + byKey(key: string, type?: 'app' | 'admin'): string[] { + return this.data[key].filter(e => { + return type? _.startsWith(e, `/${type}/`): true; + }); } }