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

session-list.component.ts 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import { Component, OnInit, OnDestroy, Input, OnChanges, SimpleChanges } from '@angular/core';
  2. import { CommonModule } from '@angular/common';
  3. import { MatListModule } from '@angular/material/list';
  4. import { MatButtonModule } from '@angular/material/button';
  5. import { MatIconModule } from '@angular/material/icon';
  6. import { MatInputModule } from '@angular/material/input';
  7. import { MatFormFieldModule } from '@angular/material/form-field';
  8. import { FormsModule } from '@angular/forms';
  9. import { Subscription } from 'rxjs';
  10. import { SessionService } from '../services/session.service';
  11. import { AuthService } from '../services/auth.service';
  12. import { MenuService } from '../services/menu.service';
  13. import { InstanceService } from '../services/instance.service';
  14. import { Session } from '../models/session.model';
  15. @Component({
  16. selector: 'app-session-list',
  17. standalone: true,
  18. imports: [
  19. CommonModule,
  20. MatListModule,
  21. MatButtonModule,
  22. MatIconModule,
  23. MatInputModule,
  24. MatFormFieldModule,
  25. FormsModule
  26. ],
  27. templateUrl: './session-list.component.html',
  28. styleUrl: './session-list.component.scss'
  29. })
  30. export class SessionListComponent implements OnInit, OnDestroy {
  31. sessions: Session[] = [];
  32. activeSessionId: string | null = null;
  33. showCreateForm = false;
  34. newSessionTitle = '';
  35. @Input() instanceId: string | undefined;
  36. menuItemId: string | null = null;
  37. private authSubscription?: Subscription;
  38. constructor(
  39. private sessionService: SessionService,
  40. private authService: AuthService,
  41. private menuService: MenuService,
  42. private instanceService: InstanceService
  43. ) {}
  44. ngOnInit() {
  45. // 初始检查认证状态
  46. if (this.authService.isAuthenticated()) {
  47. this.loadMenuAndSessions();
  48. } else {
  49. // 订阅认证状态变化,登录后加载会话
  50. this.authSubscription = this.authService.authState$.subscribe(authState => {
  51. if (authState.isAuthenticated) {
  52. console.log('用户已认证,加载会话列表');
  53. this.loadMenuAndSessions();
  54. }
  55. });
  56. }
  57. }
  58. ngOnChanges(changes: SimpleChanges) {
  59. // 如果instanceId变化,重新加载会话
  60. if (changes['instanceId'] && !changes['instanceId'].firstChange) {
  61. console.log('实例ID变化,重新加载会话:', this.instanceId);
  62. this.loadMenuAndSessions();
  63. }
  64. }
  65. ngOnDestroy() {
  66. if (this.authSubscription) {
  67. this.authSubscription.unsubscribe();
  68. }
  69. }
  70. loadMenuAndSessions() {
  71. if (this.instanceId) {
  72. // 通过实例ID加载
  73. const instance = this.instanceService.getInstanceById(this.instanceId);
  74. if (instance) {
  75. this.menuItemId = instance.menuItemId;
  76. console.log('使用实例菜单项ID:', this.menuItemId);
  77. this.loadSessionsForInstance();
  78. } else {
  79. console.error('实例不存在:', this.instanceId);
  80. this.loadSessions(); // 回退到普通加载
  81. }
  82. } else {
  83. // 没有实例ID,使用旧逻辑:加载菜单,获取第一个菜单项ID
  84. this.menuService.getTopMenu().subscribe({
  85. next: (menuItems) => {
  86. if (menuItems.length > 0) {
  87. this.menuItemId = menuItems[0].id;
  88. console.log('使用第一个菜单项ID:', this.menuItemId);
  89. this.loadSessions();
  90. } else {
  91. console.error('没有可用的菜单项');
  92. this.loadSessions(); // 仍然尝试加载会话,但创建会话时会失败
  93. }
  94. },
  95. error: (error) => {
  96. console.error('加载菜单失败:', error);
  97. // 仍然尝试加载会话
  98. this.loadSessions();
  99. }
  100. });
  101. }
  102. }
  103. loadSessions() {
  104. this.sessionService.getSessions().subscribe({
  105. next: (sessions) => {
  106. // 如果没有实例ID,显示所有会话
  107. this.sessions = sessions;
  108. if (sessions.length > 0 && !this.activeSessionId) {
  109. this.selectSession(sessions[0]);
  110. }
  111. },
  112. error: (error) => {
  113. console.error('加载会话列表失败:', error);
  114. }
  115. });
  116. }
  117. loadSessionsForInstance() {
  118. if (!this.instanceId) {
  119. this.loadSessions();
  120. return;
  121. }
  122. this.instanceService.getInstanceSessions(this.instanceId).subscribe({
  123. next: (sessions) => {
  124. this.sessions = sessions;
  125. if (sessions.length > 0 && !this.activeSessionId) {
  126. this.selectSession(sessions[0]);
  127. }
  128. },
  129. error: (error) => {
  130. console.error('加载实例会话失败:', error);
  131. }
  132. });
  133. }
  134. createNewSession() {
  135. this.showCreateForm = true;
  136. }
  137. cancelCreate() {
  138. this.showCreateForm = false;
  139. this.newSessionTitle = '';
  140. }
  141. confirmCreate() {
  142. if (!this.newSessionTitle.trim()) return;
  143. if (!this.menuItemId) {
  144. console.error('无法创建会话:未找到菜单项ID');
  145. // 尝试重新加载菜单
  146. this.loadMenuAndSessions();
  147. return;
  148. }
  149. this.sessionService.createSession(this.newSessionTitle, this.menuItemId!).subscribe({
  150. next: (session) => {
  151. this.sessions.unshift(session);
  152. this.selectSession(session);
  153. this.showCreateForm = false;
  154. this.newSessionTitle = '';
  155. },
  156. error: (error) => {
  157. console.error('创建会话失败:', error);
  158. }
  159. });
  160. }
  161. selectSession(session: Session) {
  162. this.activeSessionId = session.id;
  163. // 触发会话切换事件(通过服务)
  164. this.sessionService.setActiveSession(session);
  165. }
  166. }