Brak opisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

app.component.ts 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import { Component, OnInit, OnDestroy } from '@angular/core';
  2. import { CommonModule } from '@angular/common';
  3. import { Router, RouterLink, RouterOutlet } from '@angular/router';
  4. import { Subscription } from 'rxjs';
  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 { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
  10. import { AuthService } from './services/auth.service';
  11. import { EventService } from './services/event.service';
  12. import { SessionService } from './services/session.service';
  13. import { WindowService } from './services/window.service';
  14. import { ConfigService } from 'base-core';
  15. import { NewProjectModalComponent } from './components/new-project-modal.component';
  16. @Component({
  17. selector: 'app-root',
  18. imports: [CommonModule, RouterOutlet, RouterLink, MatIcon, MatButtonModule, MatMenuModule, MatTooltipModule, MatProgressSpinnerModule, NewProjectModalComponent],
  19. templateUrl: './app.component.html',
  20. styleUrl: './app.component.scss'
  21. })
  22. export class AppComponent implements OnInit, OnDestroy {
  23. showNewProjectModal = false;
  24. private subscriptions: Subscription = new Subscription();
  25. isEventStreamConnected = false;
  26. constructor(
  27. private router: Router,
  28. public authService: AuthService,
  29. private config: ConfigService,
  30. private eventService: EventService,
  31. private sessionService: SessionService,
  32. private windowService: WindowService
  33. ) {}
  34. ngOnInit() {
  35. console.log('AppComponent初始化');
  36. // 禁用模拟数据,使用真实API
  37. this.config.useMockData = false;
  38. this.config.apiBaseUrl = '/api';
  39. console.log('使用真实API,配置:', {
  40. useMockData: this.config.useMockData,
  41. apiBaseUrl: this.config.apiBaseUrl
  42. });
  43. // 订阅事件流连接状态
  44. this.subscriptions.add(
  45. this.eventService.connectionStatus$.subscribe(connected => {
  46. this.isEventStreamConnected = connected;
  47. console.log('事件流连接状态:', connected ? '已连接' : '已断开');
  48. })
  49. );
  50. // 如果已登录,加载项目
  51. if (this.authService.isAuthenticated()) {
  52. this.loadProjects();
  53. } else {
  54. // 订阅认证状态变化
  55. this.subscriptions.add(
  56. this.authService.authState$.subscribe(authState => {
  57. if (authState.isAuthenticated) {
  58. console.log('用户已认证,加载项目');
  59. this.loadProjects();
  60. }
  61. })
  62. );
  63. }
  64. }
  65. // 加载项目列表
  66. loadProjects() {
  67. this.sessionService.getSessions().subscribe({
  68. next: (sessions: any[]) => {
  69. // 从会话中提取项目信息
  70. const projects = sessions
  71. .filter(session => session.project_id)
  72. .map(session => ({
  73. id: session.project_id!,
  74. title: session.title,
  75. description: session.description,
  76. agent: session.agent_name,
  77. created_at: session.created_at,
  78. updated_at: session.updated_at
  79. }));
  80. // 去重:相同project_id只保留最新
  81. const uniqueProjects = Array.from(
  82. new Map(projects.map(p => [p.id, p])).values()
  83. );
  84. console.log('加载项目列表:', uniqueProjects.length);
  85. // 可以在这里更新标签页服务中的项目数据(如果需要)
  86. // this.tabService.updateProjects(uniqueProjects);
  87. },
  88. error: (error) => {
  89. console.error('加载项目失败:', error);
  90. }
  91. });
  92. }
  93. get isLoginPage(): boolean {
  94. return this.router.url.includes('/login') || this.router.url.includes('/project/');
  95. }
  96. // 导航到主页
  97. goHome() {
  98. this.router.navigate(['/home']);
  99. }
  100. // 打开新建项目模态框
  101. openNewProjectModal() {
  102. console.log('🔍 [AppComponent] 打开新建项目模态框,当前状态:', this.showNewProjectModal);
  103. this.showNewProjectModal = true;
  104. console.log('🔍 [AppComponent] 设置后状态:', this.showNewProjectModal);
  105. }
  106. // 处理项目创建
  107. onProjectCreated(project: any) {
  108. // 在新浏览器标签页中打开项目
  109. const projectId = project.project_id || project.id;
  110. console.log('🔍 [AppComponent] 新项目创建,打开标签页:', projectId);
  111. const success = this.windowService.openProjectTab(projectId);
  112. if (!success) {
  113. console.error('🔍 [AppComponent] 打开项目标签页失败');
  114. }
  115. }
  116. logout() {
  117. this.authService.logout();
  118. }
  119. get username(): string {
  120. return this.authService.getCurrentUser()?.username || '用户';
  121. }
  122. ngOnDestroy() {
  123. this.subscriptions.unsubscribe();
  124. }
  125. }