From d117b84dd1056e673513f7bf749c2e28076cf4e8 Mon Sep 17 00:00:00 2001 From: cool Date: Wed, 24 Apr 2024 09:11:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=8F=9C=E5=8D=95=E5=AF=BC?= =?UTF-8?q?=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/package.json | 2 +- core/src/module/menu.ts | 129 +++++++++++++++++++++------------------- core/src/package.json | 2 +- 3 files changed, 71 insertions(+), 62 deletions(-) diff --git a/core/package.json b/core/package.json index 9fe3c62..575f067 100644 --- a/core/package.json +++ b/core/package.json @@ -1,6 +1,6 @@ { "name": "@cool-midway/core", - "version": "7.1.17", + "version": "7.1.18", "description": "", "main": "dist/index.js", "typings": "index.d.ts", diff --git a/core/src/module/menu.ts b/core/src/module/menu.ts index e9ad20c..4903454 100644 --- a/core/src/module/menu.ts +++ b/core/src/module/menu.ts @@ -1,4 +1,14 @@ -import { App, Config, ILogger, IMidwayApplication, Inject, Logger, Provide, Scope, ScopeEnum } from "@midwayjs/core"; +import { + App, + Config, + ILogger, + IMidwayApplication, + Inject, + Logger, + Provide, + Scope, + ScopeEnum, +} from "@midwayjs/core"; import * as fs from "fs"; import { CoolModuleConfig } from "./config"; import * as path from "path"; @@ -11,71 +21,70 @@ import { CoolEventManager } from "../event"; @Provide() @Scope(ScopeEnum.Singleton) export class CoolModuleMenu { + @Inject() + coolModuleConfig: CoolModuleConfig; - @Inject() - coolModuleConfig: CoolModuleConfig; + @Config("cool") + coolConfig: CoolConfig; - @Config("cool") - coolConfig: CoolConfig; + @App() + app: IMidwayApplication; - @App() - app: IMidwayApplication; + @Logger() + coreLogger: ILogger; - @Logger() - coreLogger: ILogger; + @Inject() + coolEventManager: CoolEventManager; - @Inject() - coolEventManager: CoolEventManager; + datas = {}; - datas = {}; - - async init() { - // 是否需要导入 - if (this.coolConfig.initMenu) { - const modules = this.coolModuleConfig.modules; - const importLockPath = path.join( - `${this.app.getBaseDir()}`, - "..", - "lock", - 'menu' - ); - if (!fs.existsSync(importLockPath)) { - fs.mkdirSync(importLockPath, { recursive: true }); - } - for (const module of modules) { - const lockPath = path.join(importLockPath, module + ".menu.lock"); - if (!fs.existsSync(lockPath)) { - await this.importMenu(module, lockPath); - } - } - this.coolEventManager.emit("onMenuImport", this.datas); - this.coolEventManager.emit("onMenuInit", {}); + async init() { + // 是否需要导入 + if (this.coolConfig.initMenu) { + const modules = this.coolModuleConfig.modules; + const importLockPath = path.join( + `${this.app.getBaseDir()}`, + "..", + "lock", + "menu" + ); + if (!fs.existsSync(importLockPath)) { + fs.mkdirSync(importLockPath, { recursive: true }); + } + for (const module of modules) { + const lockPath = path.join(importLockPath, module + ".menu.lock"); + if (!fs.existsSync(lockPath)) { + await this.importMenu(module, lockPath); } } - - /** - * 导入菜单 - * @param module - * @param lockPath - */ - async importMenu(module: string, lockPath: string){ - // 模块路径 - const modulePath = `${this.app.getBaseDir()}/modules/${module}`; - // json 路径 - const menuPath = `${modulePath}/menu.json`; - // 导入 - if (fs.existsSync(menuPath)) { - const data = fs.readFileSync(menuPath); - try { - // this.coolEventManager.emit("onMenuImport", module, JSON.parse(data.toString())); - this.datas[module] = JSON.parse(data.toString()); - fs.writeFileSync(lockPath, data); - } catch (error) { - this.coreLogger.error(error); - this.coreLogger.error( - `自动初始化模块[${module}]菜单失败,请检查对应的数据结构是否正确` - ); - } - } + this.coolEventManager.emit("onMenuImport", this.datas); + // this.coolEventManager.emit("onMenuInit", {}); } -} \ No newline at end of file + } + + /** + * 导入菜单 + * @param module + * @param lockPath + */ + async importMenu(module: string, lockPath: string) { + // 模块路径 + const modulePath = `${this.app.getBaseDir()}/modules/${module}`; + // json 路径 + const menuPath = `${modulePath}/menu.json`; + // 导入 + if (fs.existsSync(menuPath)) { + const data = fs.readFileSync(menuPath); + try { + // this.coolEventManager.emit("onMenuImport", module, JSON.parse(data.toString())); + this.datas[module] = JSON.parse(data.toString()); + fs.writeFileSync(lockPath, data); + } catch (error) { + this.coreLogger.error(error); + this.coreLogger.error( + `自动初始化模块[${module}]菜单失败,请检查对应的数据结构是否正确` + ); + } + } + } +} diff --git a/core/src/package.json b/core/src/package.json index 450d52d..36dba78 100644 --- a/core/src/package.json +++ b/core/src/package.json @@ -1,6 +1,6 @@ { "name": "@cool-midway/core", - "version": "7.1.17", + "version": "7.1.18", "description": "", "main": "index.js", "typings": "index.d.ts",