edit
This commit is contained in:
		| @ -7,6 +7,7 @@ import { NzSafeAny } from 'ng-zorro-antd/core/types'; | ||||
| import { NzModalService } from 'ng-zorro-antd/modal'; | ||||
| import { SettingRoleEditComponent } from 'src/app/routes/sys-setting/components/role-management/edit/edit.component'; | ||||
| import { MenuManagerService } from './../../services/menu-manager.service'; | ||||
| import { MenuModalComponent } from './menu-modal/menu-modal.component'; | ||||
|  | ||||
| @Component({ | ||||
|   selector: 'app-menu-manager-components-index', | ||||
| @ -15,18 +16,11 @@ import { MenuManagerService } from './../../services/menu-manager.service'; | ||||
| }) | ||||
| export class MenuManagerComponentsIndexComponent implements OnInit { | ||||
|   selectedPlatform!: { name: string; appId: string; enName: string }; | ||||
|   menus: Array<any> = []; | ||||
|   platforms: Array<any> = []; | ||||
|   currentSelectedNode: any; | ||||
|   transferData!: string; | ||||
|   dropType = { | ||||
|     dropPrev: true, | ||||
|     dropNext: true, | ||||
|     dropInner: true | ||||
|   }; | ||||
|   platforms: Array<any> = [ | ||||
|     { name: '货主PC', appId: 'A48F72F0A304427F921794BAD86B3522', enName: 'tms-smc-web' }, | ||||
|     { name: '运营后台', appId: this.envSrv.env.appId, enName: 'tms-obc-web' } | ||||
|   ]; | ||||
|  | ||||
|   @ViewChild('st', { static: true }) | ||||
|   st!: STComponent; | ||||
|   @ViewChild('sf', { static: false }) | ||||
|   sf!: SFComponent; | ||||
|  | ||||
| @ -34,14 +28,12 @@ export class MenuManagerComponentsIndexComponent implements OnInit { | ||||
|     properties: { | ||||
|       roleName: { | ||||
|         type: 'string', | ||||
|         title: '角色名称', | ||||
|         title: '菜单名称', | ||||
|         ui: { placeholder: '请输入' } | ||||
|       } | ||||
|     } | ||||
|   }; | ||||
|  | ||||
|   selectedRows: any[] = []; | ||||
|  | ||||
|   mapOfExpandedData: { [key: string]: any[] } = {}; | ||||
|   listOfMapData: any[] = []; | ||||
|   constructor(private envSrv: EAEnvironmentService, public service: MenuManagerService, private modal: NzModalService) { | ||||
| @ -51,10 +43,6 @@ export class MenuManagerComponentsIndexComponent implements OnInit { | ||||
|   ngOnInit(): void {} | ||||
|  | ||||
|   initData(): void { | ||||
|     this.platforms = [ | ||||
|       { name: '货主PC', appId: 'A48F72F0A304427F921794BAD86B3522', enName: 'tms-smc-web' }, | ||||
|       { name: '运营后台', appId: this.envSrv.env.appId, enName: 'tms-obc-web' } | ||||
|     ]; | ||||
|     this.selectedPlatform = this.platforms[0]; | ||||
|     this.loadMemu(this.selectedPlatform.appId); | ||||
|   } | ||||
| @ -64,115 +52,32 @@ export class MenuManagerComponentsIndexComponent implements OnInit { | ||||
|       if (res) { | ||||
|         this.listOfMapData = res; | ||||
|         this.listOfMapData.forEach(item => { | ||||
|           this.mapOfExpandedData[item.key] = this.convertTreeToList(item); | ||||
|           this.mapOfExpandedData[item.key] = this.service.convertTreeToList(item); | ||||
|         }); | ||||
|         console.log(this.listOfMapData, this.mapOfExpandedData); | ||||
|       } | ||||
|     }); | ||||
|   } | ||||
|  | ||||
|   platformChange(e: { name: string; appId: string }) { | ||||
|     if (e) { | ||||
|       this.loadMenus(e.appId); | ||||
|     } else { | ||||
|       this.menus = []; | ||||
|       this.currentSelectedNode = null; | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   loadMenus(appId: string) { | ||||
|     this.service.request(this.service.$api_get_one, { appId }, 'POST', false).subscribe(res => { | ||||
|       this.menus = res; | ||||
|     }); | ||||
|   } | ||||
|  | ||||
|   editValueChange(event: any) {} | ||||
|  | ||||
|   menuImport(index: number) { | ||||
|     this.selectedPlatform = this.platforms[index]; | ||||
|     if (!this.selectedPlatform) { | ||||
|       return; | ||||
|     } | ||||
|  | ||||
|     this.service.http.request('GET', `assets/mocks/platform/${this.selectedPlatform.enName}.json`).subscribe((res: any) => { | ||||
|       this.addMenu(res.menu); | ||||
|     }); | ||||
|   } | ||||
|  | ||||
|   addMenu(menus: Array<Menu>, parentId: string = '') { | ||||
|     menus.forEach(r => { | ||||
|       if (parentId !== '') { | ||||
|         r.parentId = parentId; | ||||
|       } | ||||
|       this.service.request(this.service.$api_get_one, { appId: this.selectedPlatform.appId }, 'POST', false).subscribe(res => { | ||||
|         // 如果res.data存在,则更新菜单 | ||||
|         if (res.data) { | ||||
|           r.id = res.data.id; | ||||
|         } | ||||
|         this.service | ||||
|           .addOne({ appId: this.selectedPlatform.appId, ...r, isLeaf: !(r.children && r.children.length > 0) }) | ||||
|           .subscribe(result => { | ||||
|             if (result) { | ||||
|               if (r.children && r.children.length > 0) { | ||||
|                 this.addMenu(r.children, result.id); | ||||
|               } | ||||
|             } | ||||
|           }); | ||||
|       }); | ||||
|     }); | ||||
|  | ||||
|     // this.loadMenus(this.selectedPlatform.appId); | ||||
|   } | ||||
|  | ||||
|   addMenuRecursion() {} | ||||
|  | ||||
|   delMenu(type: number) { | ||||
|     this.modal.confirm({ | ||||
|       nzTitle: '<i>删除确认</i>', | ||||
|       nzContent: `是否确认删除?`, | ||||
|       nzOnOk: () => { | ||||
|         this.getMenuByAppID(type === 0 ? 'A48F72F0A304427F921794BAD86B3522' : this.envSrv.env.appId); | ||||
|       } | ||||
|     }); | ||||
|   } | ||||
|  | ||||
|   getMenuByAppID(appId: string) { | ||||
|     this.service.request(this.service.$api_get_one, { appId }, 'POST', false).subscribe(res => { | ||||
|       if (res) { | ||||
|         const menus = res.data; | ||||
|         if (res.data?.length > 0) { | ||||
|           this.deleteMenuByAppID(res.data); | ||||
|         } else { | ||||
|           this.service.msgSrv.success('菜单已清空'); | ||||
|         } | ||||
|       } | ||||
|     }); | ||||
|   } | ||||
|  | ||||
|   deleteMenuByAppID(arr: Array<any>) { | ||||
|     let ids: any[] = arr?.map(item => item.id) || []; | ||||
|     arr.forEach(item => { | ||||
|       if (item.children?.length > 0) { | ||||
|         this.deleteMenuByAppID(item.children); | ||||
|       } | ||||
|     }); | ||||
|     this.service.request(this.service.$api_del_many, ids).subscribe(res => {}); | ||||
|   } | ||||
|  | ||||
|   changeMemu(key: number) { | ||||
|     this.selectedPlatform = this.platforms[key]; | ||||
|     this.loadMemu(this.selectedPlatform.appId); | ||||
|   } | ||||
|  | ||||
|   roleAction(item?: any) { | ||||
|   menuAction(nzTitle: string, item?: any, parentId?: string, isDisabled = false) { | ||||
|     const modal = this.modal.create({ | ||||
|       nzContent: SettingRoleEditComponent, | ||||
|       nzTitle, | ||||
|       nzContent: MenuModalComponent, | ||||
|       nzWidth: 900, | ||||
|       nzComponentParams: item ? { i: { ...item } } : { i: { id: 0 } }, | ||||
|       nzComponentParams: item | ||||
|         ? { formData: { ...item }, isDisabled, params: { parentId, appId: this.selectedPlatform.appId } } | ||||
|         : { formData: { id: null }, params: { parentId, appId: this.selectedPlatform.appId } }, | ||||
|       nzFooter: null | ||||
|     }); | ||||
|     modal.afterClose.subscribe(res => { | ||||
|       this.st.load(); | ||||
|       if (res) { | ||||
|         this.loadMemu(this.selectedPlatform.appId); | ||||
|       } | ||||
|     }); | ||||
|   } | ||||
|  | ||||
| @ -182,12 +87,12 @@ export class MenuManagerComponentsIndexComponent implements OnInit { | ||||
|       nzClosable: false, | ||||
|       nzCancelText: '取消', | ||||
|       nzOnOk: () => { | ||||
|         // this.service.request(this.service.$api_dalete_role, [item.id]).subscribe(res => { | ||||
|         //   if (res) { | ||||
|         //     this.service.msgSrv.success('删除角色成功'); | ||||
|         //     this.st.load(); | ||||
|         //   } | ||||
|         // }); | ||||
|         this.service.request(this.service.$api_del_many, [item.id]).subscribe(res => { | ||||
|           if (res) { | ||||
|             this.service.msgSrv.success('删除菜单成功'); | ||||
|             this.loadMemu(this.selectedPlatform.appId); | ||||
|           } | ||||
|         }); | ||||
|       } | ||||
|     }); | ||||
|   } | ||||
| @ -199,63 +104,25 @@ export class MenuManagerComponentsIndexComponent implements OnInit { | ||||
|     this.sf.reset(); | ||||
|   } | ||||
|  | ||||
|   collapse(array: TreeNodeInterface[], data: TreeNodeInterface, $event: boolean): void { | ||||
|     if (!$event) { | ||||
|       if (data.children) { | ||||
|         data.children.forEach(d => { | ||||
|           const target = array.find(a => a.key === d.key)!; | ||||
|           target.expand = false; | ||||
|           this.collapse(array, target, false); | ||||
|         }); | ||||
|       } else { | ||||
|         return; | ||||
|   menuImport(index: number) { | ||||
|     if (this.listOfMapData?.length > 0) { | ||||
|       this.service.msgSrv.warning('请先清空菜单'); | ||||
|       return; | ||||
|     } | ||||
|     if (!this.selectedPlatform) { | ||||
|       return; | ||||
|     } | ||||
|  | ||||
|     this.service.menuImport(this.selectedPlatform.enName, this.selectedPlatform.appId); | ||||
|   } | ||||
|  | ||||
|   delMenu(type: number) { | ||||
|     this.modal.confirm({ | ||||
|       nzTitle: '<i>删除确认</i>', | ||||
|       nzContent: `是否确认删除?`, | ||||
|       nzOnOk: () => { | ||||
|         this.service.getMenuByAppID(type === 0 ? 'A48F72F0A304427F921794BAD86B3522' : this.envSrv.env.appId); | ||||
|       } | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   convertTreeToList(root: TreeNodeInterface): TreeNodeInterface[] { | ||||
|     const stack: TreeNodeInterface[] = []; | ||||
|     const array: TreeNodeInterface[] = []; | ||||
|     const hashMap = {}; | ||||
|     stack.push({ ...root, level: 0, expand: true }); | ||||
|  | ||||
|     while (stack.length !== 0) { | ||||
|       const node = stack.pop()!; | ||||
|       this.visitNode(node, hashMap, array); | ||||
|       if (node.children) { | ||||
|         for (let i = node.children.length - 1; i >= 0; i--) { | ||||
|           stack.push({ ...node.children[i], level: node.level! + 1, expand: false, parent: node, iconType: this.formatIcon(node.children[i].icon) }); | ||||
|         } | ||||
|       } | ||||
|     } | ||||
|  | ||||
|     return array; | ||||
|   } | ||||
|  | ||||
|   visitNode(node: TreeNodeInterface, hashMap: { [key: string]: boolean }, array: TreeNodeInterface[]): void { | ||||
|     if (!hashMap[node.key]) { | ||||
|       hashMap[node.key] = true; | ||||
|       array.push(node); | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   private formatIcon(icon: any) { | ||||
|     let value = icon; | ||||
|     // compatible `anticon anticon-user` | ||||
|     if (~icon.indexOf(`anticon-`)) { | ||||
|       value = value.split('-').slice(1).join('-'); | ||||
|     } | ||||
|     return value; | ||||
|     }); | ||||
|   } | ||||
| } | ||||
| export interface TreeNodeInterface { | ||||
|   key: string; | ||||
|   name: string; | ||||
|   age?: number; | ||||
|   level?: number; | ||||
|   expand?: boolean; | ||||
|   address?: string; | ||||
|   children?: TreeNodeInterface[]; | ||||
|   parent?: TreeNodeInterface; | ||||
|   [key: string]: any; | ||||
| } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user