import { RhTreeNode, RhOrganizationUnitDto, RhOrganizationUnitQueryDto } from 'rh-base/model'; import { Component, OnInit, Input, Output, EventEmitter, ViewChild } from '@angular/core'; import { ObjectHelper, RhTreeHelper, MsgHelper } from 'rh-base/core'; import { RhAacService } from './../../../../aac.service'; import { NzFormatEmitEvent, NzTreeComponent, NzTreeNode, NzTreeService } from 'ng-zorro-antd/tree'; @Component({ selector: 'rh-group-role-select-snippet', templateUrl: './group-role-select-snippet.component.html', styleUrls: ['./group-role-select-snippet.component.less'] }) export class GroupRoleSelectSnippetComponent implements OnInit { @Input() rootNode!: RhOrganizationUnitDto; @Output() selectedTreeNodeChange = new EventEmitter(); @Input() selectedTreeNode: NzTreeNode | null = null; @ViewChild('organizationUnitTree', { static: false }) organizationUnitTree!: NzTreeComponent; treeNodes: NzTreeNode[] = []; treeLoading = false; constructor(public operator: RhAacService, public treeSer: NzTreeService) {} ngOnInit() { this.initTreeNodes(); } // ************************用户组************************ initTreeNodes(): void { this.treeLoading = true; this.operator.GetOrganizationUnitTreeNodes(RhOrganizationUnitQueryDto.create()).subscribe( (result) => { if (result.Success) { const node = new RhTreeNode('', '', false, '', [], ''); // 将所有的用户组的isLeaf设置为false this.setLeafToFalse(result.Attach); ObjectHelper.MapT(result.Attach, node); this.treeNodes = RhTreeHelper.ToNzTreeNodes(node, this.treeSer); this.rootNode = node.nodeData as RhOrganizationUnitDto; } else if (!result.Success || !result.Attach) { MsgHelper.ShowInfoMessage('查询无用户组数据'); } this.treeLoading = false; }, () => { this.treeLoading = false; } ); } onSelectTreeNode($event: NzFormatEmitEvent) { const treeNode = $event.node; if (treeNode?.isSelected || treeNode?.isExpanded) { this.selectedTreeNode = treeNode; } else { this.selectedTreeNode = null; } this.selectedTreeNodeChange.emit(this.selectedTreeNode as NzTreeNode); if (!this.selectedTreeNode) { return; } // 如果选择的节点是用户组节点,就拉取对应角色列表 const customData = treeNode?.origin?.['customData']; if (treeNode?.isExpanded && !customData.isRole && !customData.loaded) { this.operator.GetOrganizationUnitMatchRoles(customData as RhOrganizationUnitDto).subscribe((result) => { if (result && result.Success && result.Attach.Roles?.length) { const roles = result.Attach.Roles; // 添加节点 const childrenNode: NzTreeNode[] = []; roles.forEach((ele) => { if (ele) { ele.isRole = true; } const childNode = new NzTreeNode({ key: ele?.Name || '', title: ele?.DisplayName || '', isLeaf: true, customData: ele }); childrenNode.push(childNode); }); treeNode.addChildren(childrenNode); // 防止数据的多次加载 customData.loaded = true; } else { MsgHelper.ShowWarningMessage('查询无角色数据!'); } }); } } resetSelectedTreeNode() { if (this.selectedTreeNode) { this.selectedTreeNode.isSelected = false; this.selectedTreeNode = null; } } private setLeafToFalse(node: NzTreeNode) { node.isLeaf = false; if (node.children && node.children.length) { node.children.forEach((ele) => { this.setLeafToFalse(ele); }); } } }