优化菜单导入

This commit is contained in:
cool 2024-04-24 09:11:44 +08:00
parent 8deaa90fd4
commit d117b84dd1
3 changed files with 71 additions and 62 deletions

View File

@ -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",

View File

@ -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", {});
}
}
}
/**
*
* @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}]菜单失败,请检查对应的数据结构是否正确`
);
}
}
}
}

View File

@ -1,6 +1,6 @@
{
"name": "@cool-midway/core",
"version": "7.1.17",
"version": "7.1.18",
"description": "",
"main": "index.js",
"typings": "index.d.ts",