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): Promise { return await this.coreMenuService.createMenu(data); } /** * 更新菜单 * @param appType 应用类型 * @param menuKey 菜单键 * @param data 更新数据 * @returns 是否成功 */ async edit( appType: string, menuKey: string, data: Partial, ): Promise { return await this.coreMenuService.updateMenu(appType, menuKey, data); } /** * 获取菜单信息 * @param appType 应用类型 * @param menuKey 菜单键 * @returns 菜单信息 */ async get(appType: string, menuKey: string): Promise { return await this.coreMenuService.findByMenuKey(menuKey, appType); } /** * 查找菜单 * @param menuKey 菜单键 * @param appType 应用类型 * @returns 菜单实体 */ async find(menuKey: string, appType?: string): Promise { return await this.coreMenuService.findByMenuKey(menuKey, appType); } /** * 删除菜单 * @param appType 应用类型 * @param menuKey 菜单键 * @returns 是否成功 */ async del(appType: string, menuKey: string): Promise { 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 { // 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 { 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 { 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 { 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 { 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 { 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, ); } }