完善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]);
// 装饰器
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

View File

@ -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) => {

View File

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

View File

@ -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),
};
}
),

View File

@ -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;
});
}
}