Files
wwjcloud-nest-v1/wwjcloud/src/common/sys/services/admin/MenuService.ts

271 lines
6.5 KiB
TypeScript
Raw Normal View History

import { Injectable } from '@nestjs/common';
import { CoreMenuService } from '../core/CoreMenuService';
import { SysMenu } from '../../../rbac/entities/SysMenu';
/**
* - Admin层
* 对应PHP: app\service\admin\sys\MenuService
*/
@Injectable()
export class MenuService {
constructor(private readonly coreMenuService: CoreMenuService) {}
/**
*
* @param data
* @returns
*/
async add(data: Partial<SysMenu>): Promise<SysMenu> {
return await this.coreMenuService.createMenu(data);
}
/**
*
* @param appType
* @param menuKey
* @param data
* @returns
*/
async edit(
appType: string,
menuKey: string,
data: Partial<SysMenu>,
): Promise<boolean> {
return await this.coreMenuService.updateMenu(appType, menuKey, data);
}
/**
*
* @param appType
* @param menuKey
* @returns
*/
async get(appType: string, menuKey: string): Promise<SysMenu | null> {
return await this.coreMenuService.findByMenuKey(menuKey, appType);
}
/**
*
* @param menuKey
* @param appType
* @returns
*/
async find(menuKey: string, appType?: string): Promise<SysMenu | null> {
return await this.coreMenuService.findByMenuKey(menuKey, appType);
}
/**
*
* @param appType
* @param menuKey
* @returns
*/
async del(appType: string, menuKey: string): Promise<boolean> {
return await this.coreMenuService.deleteMenu(appType, menuKey);
}
/**
*
* @param siteId ID
* @param menuKeys
* @param appType
* @param isTree
* @param addon
* @param isButton
* @returns
*/
async getMenuListByMenuKeys(
siteId: number,
menuKeys: string[],
appType: string,
isTree: number = 0,
addon: string = 'all',
isButton: number = 1,
): Promise<any[]> {
// TODO: 这里需要实现插件服务来获取站点的插件列表
// 暂时使用空数组,后续完善插件模块时补充
const addons: string[] = [''];
const menuList = await this.coreMenuService.getMenusByKeys(
siteId,
menuKeys,
appType,
addon,
addons,
);
// 处理多语言 - 暂时跳过,后续完善多语言模块时补充
// TODO: 实现多语言处理逻辑
if (isTree) {
return this.coreMenuService.buildMenuTree(
menuList,
'menu_key',
'parent_key',
'children',
'',
isButton,
);
}
return menuList;
}
/**
* API菜单
* @param appType
* @param addon
* @returns API菜单列表
*/
async getAllApiMenus(
appType: string = 'admin',
addon: string = '',
): Promise<SysMenu[]> {
return await this.coreMenuService.getAllApiMenus(appType, addon);
}
/**
*
* @param status
* @param isTree
* @param isButton
* @returns
*/
async getSystemMenu(
status: string | number = 'all',
isTree: number = 0,
isButton: number = 0,
): Promise<any[]> {
const menuList = await this.coreMenuService.getSystemMenus(
status,
isButton,
);
// 处理多语言 - 暂时跳过,后续完善多语言模块时补充
// TODO: 实现多语言处理逻辑
if (isTree) {
const treeMenus = this.coreMenuService.buildMenuTree(
menuList,
'menu_key',
'parent_key',
'children',
'',
isButton,
);
return this.moveChildrenToParent(treeMenus);
}
return menuList;
}
/**
*
* @param appKey
* @param status
* @param isTree
* @param isButton
* @returns
*/
async getAddonMenu(
appKey: string,
status: string | number = 'all',
isTree: number = 0,
isButton: number = 0,
): Promise<any[]> {
const menuList = await this.coreMenuService.getAddonMenus(
appKey,
status,
isButton,
);
if (isTree) {
return this.coreMenuService.buildMenuTree(
menuList,
'menu_key',
'parent_key',
'children',
'',
isButton,
);
}
return menuList;
}
/**
*
* @param addon
* @returns
*/
async getMenuByTypeDir(addon: string = 'system'): Promise<any[]> {
const menuList = await this.coreMenuService.getMenusByTypeDir(addon);
// 处理多语言 - 暂时跳过,后续完善多语言模块时补充
// TODO: 实现多语言处理逻辑
return this.coreMenuService.buildMenuTree(
menuList,
'menu_key',
'parent_key',
'children',
'',
0,
);
}
/**
*
* @param appType
* @param addons
* @returns
*/
async getMenuKeysBySystem(
appType: string,
addons: string[],
): Promise<string[]> {
return await this.coreMenuService.getMenuKeysBySystem(appType, addons);
}
/**
* -
* @param menuList
* @returns
*/
private moveChildrenToParent(menuList: any[]): any[] {
// 这个方法的具体实现需要根据PHP代码的逻辑来完善
// 暂时返回原始数据,后续根据需求完善
return menuList;
}
/**
* -
* @param menus
* @param keyField
* @param parentKeyField
* @param childrenField
* @param authField
* @param parentKey
* @param isButton
* @returns
*/
menuToTree(
menus: any[],
keyField: string = 'menu_key',
parentKeyField: string = 'parent_key',
childrenField: string = 'children',
authField: string = 'auth',
parentKey: string = '',
isButton: number = 1,
): any[] {
return this.coreMenuService.buildMenuTree(
menus,
keyField,
parentKeyField,
childrenField,
parentKey,
isButton,
);
}
}