import { RhAacService } from './../../aac.service'; import { RhAppConfigService } from 'rh-base/core'; import { RhAppConfigDto, RhProjectConfigDto } from 'rh-base/model'; import { UntypedFormBuilder, Validators } from '@angular/forms'; import { UntypedFormGroup } from '@angular/forms'; import { Component, OnInit } from '@angular/core'; import { DevProfileSettingDto, RhMenuQueryDto, RhMenusDto, RhSafeAny, // RhDynamicReportConfigInfoDto, RhRolesDto, MenuBindedRoles } from 'rh-base/model'; import { RhTreeHelper, FormHelper, MsgHelper, getMenuFullUrl } from 'rh-base/core'; import { NzTreeNode, NzTreeService } from 'ng-zorro-antd/tree'; import { RhBaseDynamicService, RhSharedService } from 'rh-base/shared'; /** * 页面开发功能 * @deprecated 已弃用 */ @Component({ selector: 'rh-menu-develop', templateUrl: './menu-develop.component.html', styleUrls: ['./menu-develop.component.less'] }) export class MenuDevelopComponent implements OnInit { isEditPage = false; treeLoading = false; treeNodes: NzTreeNode[] = []; selectedTreeNode: NzTreeNode | null = null; menusForm!: UntypedFormGroup; menuDto: RhMenusDto | null = null; submitBusy = false; addedTreeType!: 'brother' | 'child' | 'edit'; isShowAssignRoleModal = false; boundRoleDto: MenuBindedRoles | null = null; /** 拼接之后的url,包含父节点路由 */ url!: string; get appconfig(): RhAppConfigDto { return this.appconfigSer.appConfig; } get projectConfig(): RhProjectConfigDto { return this.appconfigSer.projectConfig; } constructor( private operator: RhAacService, public treeSer: NzTreeService, private fb: UntypedFormBuilder, private sharedSer: RhSharedService, private baseDynamicSer: RhBaseDynamicService, private appconfigSer: RhAppConfigService ) {} ngOnInit() { this.menuDto = this.initMenusDto(); this.createMenusForm(this.menuDto); this.initTreeNodes(); } initTreeNodes(): void { const queryDto = RhMenuQueryDto.create('MES'); this.treeLoading = true; this.selectedTreeNode = null; this.resetForm(null); this.operator.GetMenuTreeNodes(queryDto).subscribe( (dr) => { if (dr?.Success && dr?.Attach?.children) { if (this.projectConfig.MenuTreeVisibility !== 'All') { dr.Attach.children = dr.Attach.children.filter((ele) => ele?.key === this.projectConfig.RootMenu); } this.treeNodes = RhTreeHelper.ToNzTreeNodes(dr.Attach, this.treeSer); } else { MsgHelper.ShowWarningModal(`拉取菜单信息失败!${dr.Message}`); } this.treeLoading = false; }, () => (this.treeLoading = false) ); } createMenusForm(dto: RhMenusDto): void { this.menusForm = this.fb.group({ Name: [dto.Name, [Validators.required]], DisplayName: [dto.DisplayName, [Validators.required]], ParentId: [dto.ParentId, [Validators.required]], Icon: [dto.Icon], Url: [dto.Url, [Validators.required]], LevelLayer: [{ value: dto.LevelLayer, disabled: true }, [Validators.required]], SortOrder: [dto.SortOrder, [Validators.required]], TargetView: [dto.TargetView], CustomData: [dto.CustomData], IsEnable: [!!dto.IsEnable, [Validators.required]], IsVisible: [!!dto.IsVisible, [Validators.required]], IsUseFeaturePermission: [{ value: dto.IsUseFeaturePermission, disabled: true }, [Validators.required]], ProductName: [dto.ProductName, [Validators.required]] }); } initMenusDto(): RhMenusDto { return RhMenusDto.create(); } submitForm($event: Event | null): void { if ($event) { $event.preventDefault(); } FormHelper.YGSubmitForm(this.menuDto as RhMenusDto, this.menusForm, (dto) => { this.operator.menuDtoEnableHandler(dto as RhMenusDto); // this.createReport(this.menuDto); }); } resetForm($event: Event | null): void { if ($event) { $event.preventDefault(); } this.submitBusy = false; if (this.selectedTreeNode) { this.selectedTreeNode.isSelected = false; this.selectedTreeNode = null; } this.menuDto = null; this.menuDto = this.initMenusDto(); this.menusForm.reset(this.menuDto); } addMenuNode(): void { if (!this.checkIsSelectedTreeNode()) { return; } this.addedTreeType = 'brother'; const menu = this.initMenusDto(); menu.ParentId = this.menuDto?.ParentId; menu.ProductName = this.menuDto?.ProductName; menu.LevelLayer = this.selectedTreeNode?.level; FormHelper.SetTValueToForm(menu, this.menusForm); } addChildMenuNode(): void { if (!this.checkIsSelectedTreeNode()) { return; } this.addedTreeType = 'child'; const idKey = this.menuDto?.IdKey; const productName = this.menuDto?.ProductName; const menu = this.initMenusDto(); menu.ParentId = idKey; menu.ProductName = productName; menu.LevelLayer = (this.selectedTreeNode as NzTreeNode).level + 1; this.menuDto = menu; FormHelper.SetTValueToForm(this.menuDto, this.menusForm); } removeMenuNode(): void { if (!this.checkIsSelectedTreeNode()) { return; } MsgHelper.ShowDeleteConfirm( '删除菜单,则该菜单所含有子菜单将一并删除,同时系统的此功能将停止使用,您确定要删除吗?', () => { this.operator.menuDtoEnableHandler(this.menuDto as RhMenusDto); this.operator.RemoveMenu(this.menuDto as RhMenusDto).subscribe((result) => { if (result?.Success) { // 删除菜单的配置数据 this.removePageInfo(); const parentNode = this.selectedTreeNode?.parentNode; RhTreeHelper.RemoveTreeNode(this.selectedTreeNode as NzTreeNode); if (parentNode?.children?.length) { parentNode.isExpanded = true; } else { (parentNode as NzTreeNode).isLeaf = true; } this.menuDto = this.initMenusDto(); this.menusForm.reset(this.menuDto); MsgHelper.ShowSuccessMessage(`菜单删除成功!`); this.selectedTreeNode = null; this.treeNodes = [...this.treeNodes]; } else { MsgHelper.ShowErrorModal(`删除菜单失败!${result.Message}`); } }); }, () => { return true; } ); // this.opMode = -1; } checkIsSelectedTreeNode(): boolean { if (this.selectedTreeNode === null) { MsgHelper.ShowWarningModal('请先选中菜单节点!'); return false; } return true; } addTreeNode(treeNode: NzTreeNode, menu: RhMenusDto): void { const node = new NzTreeNode( { title: menu.DisplayName as string, key: menu.Name as string, isLeaf: true, customData: menu }, null, this.treeSer ); node.isExpanded = true; if (this.addedTreeType === 'child') { treeNode.isLeaf = false; treeNode.isExpanded = true; } treeNode.addChildren([node]); this.resetForm(null); this.treeNodes = [...this.treeNodes]; // 获取新增的节点对应的路由地址 this.url = getMenuFullUrl(node); } onSelectTreeNode(node: NzTreeNode) { this.closeEditPage(); this.addedTreeType = 'edit'; this.selectedTreeNode = node; const data = node.origin['customData']; // 根据选中节点拼接url this.url = getMenuFullUrl(node); // 处理checkbox data.IsEnable = !!data.IsEnable as RhSafeAny; data.IsVisible = !!data.IsVisible as RhSafeAny; this.menuDto = data; this.menusForm.reset(this.menuDto); } openEditPage() { this.isEditPage = true; } closeEditPage() { this.isEditPage = false; } openAssignModal() { // 获取该角色已经绑定的菜单列表 if (this.selectedTreeNode) { const menuDto: RhMenusDto = this.selectedTreeNode.origin['customData']; this.operator.menuDtoEnableHandler(menuDto); MsgHelper.ShowGlobalLoadingMessage(`正在拉取当前菜单的绑定用户信息,请稍候...`); this.operator.GetMenuBindedRoles(menuDto).subscribe( (result) => { if (result?.Success) { this.boundRoleDto = result.Attach; } else { this.boundRoleDto = null; } this.isShowAssignRoleModal = true; MsgHelper.CloseGlobalLoadingMessage(); }, () => { this.isShowAssignRoleModal = true; MsgHelper.CloseGlobalLoadingMessage(); } ); } } closeAssignModal() { this.isShowAssignRoleModal = false; this.boundRoleDto = null; } /** 验证菜单名称是否存在 */ validateMenuName(menuName: string) { if (!menuName || !menuName.trim()) { return; } this.operator.GetMenuBy(menuName.trim()).subscribe((result) => { if (result && result.Success) { this.menusForm?.get('Name')?.setErrors({ error: true, duplicated: true }); } this.menusForm.updateValueAndValidity(); }); } assignRole(dataset: RhRolesDto[]) { // 分配角色 if (this.selectedTreeNode) { const menuDto = this.selectedTreeNode.origin['customData']; this.operator.menuDtoEnableHandler(menuDto); const dto = MenuBindedRoles.create(); dto.Menu = menuDto; dto.Roles = dataset; this.operator.StoreMenuRoles(dto).subscribe((result) => { if (result?.Success) { MsgHelper.ShowSuccessMessage('分配角色成功!'); this.closeAssignModal(); } else { MsgHelper.ShowWarningModal(`分配角色失败!${result.Message}`); } }); } } returnMenuPage() { this.isEditPage = false; } // deletePageInfo() { // // 删除菜单页面信息 // if (!this.menuDto) { // MsgHelper.ShowWarningModal('请先选择菜单!'); // return; // } // MsgHelper.ShowConfirmModal('是否删除', '是否删除配置页面信息?', () => { // const dto = DevProfileSettingDto.create(); // dto.MenuCode = this.url; // this.sharedSer.RemoveDevProfileSettingData(dto).subscribe((result) => { // if (!result || !result.Success) { // MsgHelper.ShowWarningModal(`菜单对应的页面配置删除失败,请通知管理员处理`); // } else { // MsgHelper.ShowSuccessMessage('页面配置信息删除成功!'); // } // }); // }); // } private removePageInfo() { const dto = DevProfileSettingDto.create(); dto.MenuCode = this.url; this.baseDynamicSer.RemoveDevProfileSettingData(dto).subscribe((result) => { if (!result || !result.Success) { MsgHelper.ShowWarningModal(`菜单对应的页面配置删除失败,请通知管理员处理`); } }); } // private createReport(dto: RhMenusDto) { // const data = RhDynamicReportConfigInfoDto.create(); // // 根据是生成子节点还是兄弟节点设置url // if (this.addedTreeType === 'brother') { // const tempDatas = this.url.split('/'); // tempDatas.splice(-1, 1, dto.Url); // this.url = tempDatas.join('/'); // } else if (this.addedTreeType === 'child') { // this.url = this.url + `/${dto.Url}`; // } // data.ParentId = dto.ParentId; // data.ReportModuleCode = this.url; // data.ReportModuleName = dto.Url; // data.ReportMenuCode = dto.Name; // data.ReportMenuName = dto.DisplayName; // this.submitBusy = true; // this.operator.CreateReport(data).subscribe((result) => { // if (result?.Success) { // MsgHelper.ShowSuccessMessage(`初始化报表数据成功!`); // if (this.selectedTreeNode) { // // 获取新生成菜单的IdKey // // dto.IdKey = result.Attach.IdKey; // // dto.Id = result.Attach.Id; // dto = result.Attach; // if (this.addedTreeType === 'brother') { // this.addTreeNode(this.selectedTreeNode.parentNode, dto); // } else if (this.addedTreeType === 'child') { // this.addTreeNode(this.selectedTreeNode, dto); // } // this.treeNodes = [...this.treeNodes]; // } // } else { // MsgHelper.ShowWarningModal(`初始化报表数据失败!${result.Message}`); // } // this.submitBusy = false; // }); // } }