Нет описания
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

app.component.ts 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import { Component, OnInit, HostListener, OnDestroy } from '@angular/core';
  2. import { CommonModule } from '@angular/common';
  3. import { Router, RouterOutlet, ActivatedRoute, NavigationEnd } from '@angular/router';
  4. import { TreeNavComponent } from 'base-core';
  5. import { MatIcon } from '@angular/material/icon';
  6. import { MatButtonModule } from '@angular/material/button';
  7. import { MatMenuModule } from '@angular/material/menu';
  8. import { MatTooltipModule } from '@angular/material/tooltip';
  9. import { DragDropModule } from '@angular/cdk/drag-drop';
  10. import { AuthService } from './services/auth.service';
  11. import { TreeService } from './services/tree.service';
  12. import { TreeNode } from './models/tree-node.model';
  13. import { Subscription, filter } from 'rxjs';
  14. import { ConfigService } from 'base-core';
  15. @Component({
  16. selector: 'app-root',
  17. imports: [CommonModule, RouterOutlet, TreeNavComponent, MatIcon, MatButtonModule, MatMenuModule, DragDropModule, MatTooltipModule],
  18. templateUrl: './app.component.html',
  19. styleUrl: './app.component.scss'
  20. })
  21. export class AppComponent implements OnInit, OnDestroy {
  22. sidebarWidth = 320; // 默认宽度
  23. isDragging = false;
  24. minSidebarWidth = 200;
  25. maxSidebarWidth = 600;
  26. breadcrumbPath: string[] = [];
  27. treeData: TreeNode[] = [];
  28. private subscriptions: Subscription = new Subscription();
  29. constructor(
  30. private router: Router,
  31. public authService: AuthService,
  32. private treeService: TreeService,
  33. private activatedRoute: ActivatedRoute,
  34. private config: ConfigService
  35. ) {}
  36. ngOnInit() {
  37. console.log('AppComponent初始化');
  38. console.log('当前路由:', this.router.url);
  39. // 启用真实API调用
  40. this.config.useMockData = false;
  41. console.log('使用真实API数据,配置:', {
  42. useMockData: this.config.useMockData,
  43. apiBaseUrl: this.config.apiBaseUrl
  44. });
  45. // 从本地存储恢复宽度
  46. const savedWidth = localStorage.getItem('sidebarWidth');
  47. if (savedWidth) {
  48. this.sidebarWidth = parseInt(savedWidth, 10);
  49. }
  50. // 加载树数据
  51. this.treeService.getTree().subscribe({
  52. next: (data) => {
  53. this.treeData = data;
  54. this.updateBreadcrumb();
  55. },
  56. error: (error) => {
  57. console.error('加载树数据失败:', error);
  58. // 确保设置 treeData 为空数组,避免绑定失效
  59. this.treeData = [];
  60. this.updateBreadcrumb();
  61. }
  62. });
  63. // 订阅路由变化
  64. this.subscriptions.add(
  65. this.router.events.pipe(
  66. filter(event => event instanceof NavigationEnd)
  67. ).subscribe(() => {
  68. this.updateBreadcrumb();
  69. })
  70. );
  71. }
  72. get isLoginPage(): boolean {
  73. return this.router.url.includes('/login');
  74. }
  75. startDrag(event: MouseEvent) {
  76. event.preventDefault();
  77. this.isDragging = true;
  78. document.body.style.cursor = 'col-resize';
  79. document.body.style.userSelect = 'none';
  80. }
  81. @HostListener('document:mousemove', ['$event'])
  82. onDrag(event: MouseEvent) {
  83. if (!this.isDragging) return;
  84. const newWidth = event.clientX;
  85. if (newWidth >= this.minSidebarWidth && newWidth <= this.maxSidebarWidth) {
  86. this.sidebarWidth = newWidth;
  87. }
  88. }
  89. @HostListener('document:mouseup')
  90. stopDrag() {
  91. if (!this.isDragging) return;
  92. this.isDragging = false;
  93. document.body.style.cursor = '';
  94. document.body.style.userSelect = '';
  95. localStorage.setItem('sidebarWidth', this.sidebarWidth.toString());
  96. }
  97. logout() {
  98. this.authService.logout();
  99. }
  100. get username(): string {
  101. return this.authService.getCurrentUser()?.username || '用户';
  102. }
  103. onTreeRefresh(): void {
  104. console.log('树刷新事件触发');
  105. this.treeService.getTree().subscribe({
  106. next: (data) => {
  107. this.treeData = data;
  108. this.updateBreadcrumb();
  109. },
  110. error: (error) => {
  111. console.error('刷新树数据失败:', error);
  112. }
  113. });
  114. }
  115. ngOnDestroy() {
  116. this.subscriptions.unsubscribe();
  117. }
  118. private updateBreadcrumb() {
  119. const currentRoute = this.router.url;
  120. console.log('更新面包屑,当前路由:', currentRoute);
  121. if (!currentRoute || currentRoute === '/') {
  122. this.breadcrumbPath = ['首页'];
  123. return;
  124. }
  125. const path = this.findPathInTree(this.treeData, currentRoute);
  126. if (path.length > 0) {
  127. this.breadcrumbPath = path;
  128. } else {
  129. // 如果树中没有找到,使用路由路径
  130. //const segments = currentRoute.split('/').filter(segment => segment.length > 0);
  131. // this.breadcrumbPath = segments.map(segment => this.formatRouteSegment(segment));
  132. }
  133. }
  134. private findPathInTree(nodes: TreeNode[], targetRoute: string): string[] {
  135. for (const node of nodes) {
  136. if (node.route === targetRoute) {
  137. return [node.name];
  138. }
  139. if (node.children && node.children.length > 0) {
  140. const childPath = this.findPathInTree(node.children, targetRoute);
  141. if (childPath.length > 0) {
  142. return [node.name, ...childPath];
  143. }
  144. }
  145. }
  146. return [];
  147. }
  148. }