import { RhAppConfigService, noop, FormHelper, MsgHelper, RhTreeHelper, RhApiUrlsService, FileHelper } from 'rh-base/core'; import { RhInterfaceLocationDto } from 'rh-base/model'; import { RhMenusDto, OpMode, RhMenuQueryDto, RhSafeAny, UploadFileOption, OpResult } from 'rh-base/model'; import { Component, OnInit, ViewChild } from '@angular/core'; import { Validators, UntypedFormBuilder, UntypedFormGroup, AbstractControl } from '@angular/forms'; import { RhAacService } from './../../aac.service'; import { Observable } from 'rxjs'; import { NzContextMenuService, NzDropdownMenuComponent } from 'ng-zorro-antd/dropdown'; import { NzTreeNode, NzTreeComponent, NzTreeService, NzFormatEmitEvent } from 'ng-zorro-antd/tree'; /** 操作模式:增加兄弟菜单|增加子菜单|编辑 */ export type OpModeType = 'brother' | 'child' | 'edit'; @Component({ selector: 'rh-menu-manage', templateUrl: './menu-manage.component.html', styleUrls: ['./menu-manage.component.less'] }) export class MenuManageComponent implements OnInit { menusForm!: UntypedFormGroup; menuDto: RhMenusDto | null = null; submitBusy = false; treeLoading = false; treeNodes: NzTreeNode[] = []; selectedTreeNode: NzTreeNode | null = null; // selectedMenu: RhMenusDto = null; // 只是当前是增加菜单,还是增加子菜单 // opMode: OpModeType = 'brother'; @ViewChild('menuTree', { static: false }) menuTree!: NzTreeComponent; /** 是否在编辑功能菜单 */ isEditFeature = false; addedTreeType!: 'brother' | 'child' | 'edit'; importMenuDataInterface!: RhInterfaceLocationDto; constructor( private fb: UntypedFormBuilder, private operator: RhAacService, public treeSer: NzTreeService, private apiUrls: RhApiUrlsService, private contextMenuSer: NzContextMenuService, public appconfigSer: RhAppConfigService ) {} ngOnInit() { this.importMenuDataInterface = this.initImportMenuDataInterface(); this.menuDto = this.initMenusDto(); this.createMenusForm(this.menuDto); this.initTreeNodes(); } /** 折叠所有树节点 */ collapseAll() { if (this.treeNodes?.length) { if (this.treeNodes?.length === 1) { this.treeNodes[0]?.children?.length ? RhTreeHelper.collapseTree(this.treeNodes[0].children) : noop(); } else { RhTreeHelper.collapseTree(this.treeNodes); } this.treeNodes = [...this.treeNodes]; } } initImportMenuDataInterface() { const temp = RhInterfaceLocationDto.create(); temp.interfaceType = 'post'; temp.port = 8018; temp.controllerName = 'YGAacApi'; temp.interfaceName = 'ImportMenuJsonFile'; return temp; } initTreeNodes(): void { const queryDto = RhMenuQueryDto.create('MES'); this.treeLoading = true; this.operator.GetMenuTreeNodes(queryDto).subscribe( (dr) => { if (dr.Success) { if (this.appconfigSer.projectConfig.MenuTreeVisibility !== 'All') { dr.Attach.children = dr.Attach.children.filter((ele) => ele?.key === this.appconfigSer.projectConfig.RootMenu); } this.treeNodes = RhTreeHelper.ToNzTreeNodes(dr.Attach, this.treeSer); } 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], 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 new RhMenusDto(null, null, null, null, null, null, null, null, null, 1, 1, false, null, 1, null, null); } submitForm($event: Event): void { $event.preventDefault(); FormHelper.YGSubmitForm(this.menuDto as RhMenusDto, this.menusForm, (dto) => { this.submitBusy = true; (dto as RhMenusDto).IsEnable = (dto as RhMenusDto).IsEnable ? 1 : 0; (dto as RhMenusDto).IsVisible = (dto as RhMenusDto).IsVisible ? 1 : 0; this.operator.StoreMenus(dto as RhMenusDto).subscribe((result) => { if (result && result.Success) { const menu = result.Attach as RhMenusDto; if (this.addedTreeType === 'brother') { this.addTreeNode(this.selectedTreeNode?.parentNode as NzTreeNode, menu); } else if (this.addedTreeType === 'child') { this.addTreeNode(this.selectedTreeNode as NzTreeNode, menu); } else if (this.addedTreeType === 'edit') { (this.selectedTreeNode as NzTreeNode).title = (dto as RhMenusDto).DisplayName as string; (this.selectedTreeNode as NzTreeNode).origin['customData'] = menu; } this.resetForm($event); // 取消树节点的选中 if (this.selectedTreeNode) { this.selectedTreeNode.isSelected = false; this.selectedTreeNode = null; } MsgHelper.ShowSuccessMessage('保存数据成功!'); this.treeNodes = [...this.treeNodes]; } else { MsgHelper.ShowWarningModal(`数据保存失败!${result.Message}`); } this.submitBusy = false; }); }); } resetForm($event: Event): void { $event.preventDefault(); this.submitBusy = false; (this.selectedTreeNode as NzTreeNode).isSelected = false; this.selectedTreeNode = null; this.menuDto = null; this.menuDto = this.initMenusDto(); this.menusForm.reset(this.menuDto); } addMenuNode(): void { this.isEditFeature = false; 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; this.menuDto = menu; FormHelper.setValueToForm(this.menuDto, this.menusForm); } addChildMenuNode(): void { this.isEditFeature = false; 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?.level as number) + 1; this.menuDto = menu; FormHelper.setValueToForm(this.menuDto, this.menusForm); } removeMenuNode(): void { if (!this.checkIsSelectedTreeNode()) { return; } MsgHelper.ShowDeleteConfirm( `删除${this.menuDto?.DisplayName}菜单,则该菜单所含有子菜单将一并删除,同时系统的此功能将停止使用,您确定要删除吗?`, () => { (this.menuDto as RhMenusDto).IsEnable = (this.menuDto as RhMenusDto).IsEnable ? 1 : 0; (this.menuDto as RhMenusDto).IsVisible = (this.menuDto as RhMenusDto).IsVisible ? 1 : 0; this.operator.RemoveMenu(this.menuDto as RhMenusDto).subscribe((result) => { if (result && result.Success) { const parentNode = this.selectedTreeNode?.parentNode as NzTreeNode; RhTreeHelper.RemoveTreeNode(this.selectedTreeNode as NzTreeNode); if (parentNode.children.length > 0) { parentNode.isExpanded = true; } else { parentNode.isLeaf = true; } this.menuDto = this.initMenusDto(); this.menusForm.reset(this.menuDto); MsgHelper.ShowSuccessMessage(`数据删除成功!`); this.treeNodes = [...this.treeNodes]; } else { MsgHelper.ShowSuccessMessage(`数据删除失败!${result.Message}`); } }); }, () => { return true; } ); // this.opMode = -1; } checkIsSelectedTreeNode(): boolean { if (this.selectedTreeNode === null) { MsgHelper.ShowWarningModal('请先选中菜单节点!'); return false; } return true; } contextMenu(event: MouseEvent, comp: NzDropdownMenuComponent) { this.contextMenuSer.create(event, comp); } exportMenuData(node: NzTreeNode) { const menuDataset = RhTreeHelper.getMenuDatasetViaParentNode(node); MsgHelper.ShowGlobalLoadingMessage(`正在导出菜单数据。。。`, 1000); FileHelper.downloadByContent(JSON.stringify(menuDataset), Date.now().toString(), 'application/json'); } onSelectTreeNode($event: NzFormatEmitEvent): void { this.isEditFeature = false; // this.opMode = -1; const treeNode = $event.node as NzTreeNode; if (treeNode?.isSelected) { // 清空原先节点的选中状态 if (this.selectedTreeNode) { this.selectedTreeNode.isSelected = false; } this.addedTreeType = 'edit'; const control = this.menusForm.get('IsUseFeaturePermission') as AbstractControl; if (treeNode.isLeaf) { control.enable(); } else { control.disable(); } this.selectedTreeNode = treeNode; const menu = treeNode.origin['customData'] as RhMenusDto; menu.OpSign = OpMode.OpEdit; // menu.Enabled = menu.IsEnable === 1 ? true : false; // menu.Visibled = menu.IsVisible === 1 ? true : false; this.menuDto = menu; // 处理checkbox menu.IsEnable = !!menu.IsEnable as RhSafeAny; menu.IsVisible = !!menu.IsVisible as RhSafeAny; this.menusForm.patchValue(menu); } else { this.selectedTreeNode = null; this.menuDto = null; this.menuDto = this.initMenusDto(); this.menusForm.reset(this.menuDto); } } 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.menuTree.nzTreeService ); node.isExpanded = true; treeNode.isLeaf = false; treeNode.addChildren([node]); } // 编辑功能菜单 editFeatureMenu() { this.isEditFeature = true; } closeFeaturePage() { this.isEditFeature = false; } validateMenuName(menuName: string) { if (!menuName || !menuName.trim()) { return; } // 菜单节点有效性判定 // 菜单名称形式为xxx_xxxx; const patt = /^[A-Z]+_[a-zA-Z0-9]+$/g; if (!patt.test(menuName)) { this.menusForm.get('Name')?.setErrors({ error: true, invalid: true }); return; } // 子菜单名称形式必须与父菜单以一致的开头命名,比如:RP_; if (this.selectedTreeNode && (this.menuDto?.LevelLayer as number) >= 2) { if (this.addedTreeType === 'child') { if (this.getMenuNameStartStr(this.selectedTreeNode.origin['customData'].Name) !== this.getMenuNameStartStr(menuName)) { this.menusForm.get('Name')?.setErrors({ error: true, discord: true }); return; } } if (this.addedTreeType === 'brother') { if (this.getMenuNameStartStr(this.selectedTreeNode.parentNode?.origin['customData'].Name) !== this.getMenuNameStartStr(menuName)) { this.menusForm.get('Name')?.setErrors({ error: true, discord: true }); return; } } } this.operator.GetMenuBy(menuName.trim()).subscribe((result) => { if (result && result.Success) { this.menusForm.get('Name')?.setErrors({ error: true, duplicated: true }); } this.menusForm.updateValueAndValidity(); }); } private getMenuNameStartStr(menuName: string) { const tempDatas = menuName.split('_'); return tempDatas[0]; } //#region api区域 /** * 导入菜单Json文件 */ ImportMenuJsonFile(option: UploadFileOption): Observable { return this.apiUrls.PostPp('YGAacApi', 'ImportMenuJsonFile', option); } //#endregion api区域结束 }