完善Eps内容

This commit is contained in:
cool 2023-11-18 15:37:54 +08:00
parent 161432bb99
commit fb67148f43
5 changed files with 64 additions and 24 deletions

View File

@ -55,12 +55,9 @@ export class CoolConfiguration implements ILifeCycle {
this.app.useFilter([CoolExceptionFilter]); this.app.useFilter([CoolExceptionFilter]);
// 装饰器 // 装饰器
await container.getAsync(CoolDecorator); 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); global["COOL-CACHE"] = await container.getAsync(CacheManager);
// 清除 location // 清除 location

View File

@ -97,25 +97,35 @@ export interface ControllerOption {
}; };
} }
// 路由配置
export interface RouterOptions {
sensitive?: boolean;
middleware?: MiddlewareParamArray;
description?: string;
tagName?: string;
ignoreGlobalPrefix?: boolean;
}
// COOL的装饰器 // COOL的装饰器
export function CoolController( export function CoolController(
curdOption?: CurdOption | string, curdOption?: CurdOption | string | RouterOptions,
routerOptions: { routerOptions: RouterOptions = { middleware: [], sensitive: true }
sensitive?: boolean;
middleware?: MiddlewareParamArray;
description?: string;
tagName?: string;
ignoreGlobalPrefix?: boolean;
} = { middleware: [], sensitive: true }
): ClassDecorator { ): ClassDecorator {
return (target: any) => { return (target: any) => {
// 将装饰的类,绑定到该装饰器,用于后续能获取到 class // 将装饰的类,绑定到该装饰器,用于后续能获取到 class
saveModule(CONTROLLER_KEY, target); saveModule(CONTROLLER_KEY, target);
let prefix; let prefix;
if (typeof curdOption === "string") { if(curdOption){
prefix = curdOption; // 判断 curdOption 的类型
} else { if (typeof curdOption === "string") {
prefix = curdOption?.prefix || ""; 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) => { location.scriptPath(target).then(async (res: any) => {

View File

@ -23,6 +23,8 @@ export interface CoolConfig {
sms?: CoolSmsConfig, sms?: CoolSmsConfig,
/** 是否自动导入数据库 */ /** 是否自动导入数据库 */
initDB?: boolean; initDB?: boolean;
/** Eps */
eps?: boolean;
/** 是否自动导入模块菜单 */ /** 是否自动导入模块菜单 */
initMenu?: boolean; initMenu?: boolean;
// 实体配置 // 实体配置

View File

@ -7,8 +7,10 @@ import {
ScopeEnum, ScopeEnum,
} from "@midwayjs/decorator"; } from "@midwayjs/decorator";
import * as _ from "lodash"; import * as _ from "lodash";
import { Inject, MidwayWebRouterService } from "@midwayjs/core"; import { Config, Inject, MidwayWebRouterService } from "@midwayjs/core";
import { TypeORMDataSourceManager } from "@midwayjs/typeorm"; import { TypeORMDataSourceManager } from "@midwayjs/typeorm";
import { CoolUrlTagData } from "../tag/data";
import { TagTypes } from "../decorator/tag";
/** /**
* *
@ -26,18 +28,38 @@ export class CoolEps {
@Inject() @Inject()
typeORMDataSourceManager: TypeORMDataSourceManager; typeORMDataSourceManager: TypeORMDataSourceManager;
@Config('cool.eps')
epsConfig: boolean;
@Config('module')
moduleConfig: any;
@Inject()
coolUrlTagData: CoolUrlTagData;
// @Init() // @Init()
async init() { async init() {
if(!this.epsConfig) return;
const entitys = await this.entity(); const entitys = await this.entity();
const controllers = await this.controller(); const controllers = await this.controller();
const routers = await this.router(); const routers = await this.router();
const adminArr = []; const adminArr = [];
const appArr = []; const appArr = [];
for (const controller of controllers) { for (const controller of controllers) {
const { prefix, module, curdOption } = controller; const { prefix, module, moduleConfig, curdOption, routerOptions } = controller;
const name = curdOption?.entity?.name; const name = curdOption?.entity?.name;
(_.startsWith(prefix, "/admin/") ? adminArr : appArr).push({ (_.startsWith(prefix, "/admin/") ? adminArr : appArr).push({
module, module,
info: {
module: {
name: moduleConfig?.name,
description: moduleConfig?.description,
},
type: {
name: prefix.split("/").pop(),
description: routerOptions?.description || "" ,
},
},
api: routers[prefix], api: routers[prefix],
name, name,
columns: entitys[name] || [], columns: entitys[name] || [],
@ -56,9 +78,10 @@ export class CoolEps {
const result = []; const result = [];
const controllers = listModule(CONTROLLER_KEY); const controllers = listModule(CONTROLLER_KEY);
for (const controller of controllers) { 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; return result;
} }
@ -67,6 +90,10 @@ export class CoolEps {
* @returns * @returns
*/ */
async router() { async router() {
let ignoreUrls: string[] = this.coolUrlTagData.byKey(TagTypes.IGNORE_TOKEN);
if(_.isEmpty(ignoreUrls)){
ignoreUrls = [];
}
return _.groupBy( return _.groupBy(
(await await this.midwayWebRouterService.getFlattenRouterTable()).map( (await await this.midwayWebRouterService.getFlattenRouterTable()).map(
(item) => { (item) => {
@ -77,6 +104,7 @@ export class CoolEps {
dts: {}, dts: {},
tag: "", tag: "",
prefix: item.prefix, prefix: item.prefix,
ignoreToken: ignoreUrls.includes(item.prefix+item.url),
}; };
} }
), ),

View File

@ -55,9 +55,12 @@ export class CoolUrlTagData {
/** /**
* *
* @param key * @param key
* @param type
* @returns * @returns
*/ */
byKey(key: string): string[] { byKey(key: string, type?: 'app' | 'admin'): string[] {
return this.data[key]; return this.data[key].filter(e => {
return type? _.startsWith(e, `/${type}/`): true;
});
} }
} }