| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- import { Component, OnInit, OnDestroy, Input, OnChanges, SimpleChanges } from '@angular/core';
- import { CommonModule } from '@angular/common';
- import { MatListModule } from '@angular/material/list';
- import { MatButtonModule } from '@angular/material/button';
- import { MatIconModule } from '@angular/material/icon';
- import { MatInputModule } from '@angular/material/input';
- import { MatFormFieldModule } from '@angular/material/form-field';
- import { FormsModule } from '@angular/forms';
- import { Subscription } from 'rxjs';
- import { SessionService } from '../services/session.service';
- import { AuthService } from '../services/auth.service';
- import { MenuService } from '../services/menu.service';
- import { InstanceService } from '../services/instance.service';
- import { Session } from '../models/session.model';
-
- @Component({
- selector: 'app-session-list',
- standalone: true,
- imports: [
- CommonModule,
- MatListModule,
- MatButtonModule,
- MatIconModule,
- MatInputModule,
- MatFormFieldModule,
- FormsModule
- ],
- templateUrl: './session-list.component.html',
- styleUrl: './session-list.component.scss'
- })
- export class SessionListComponent implements OnInit, OnDestroy {
- sessions: Session[] = [];
- activeSessionId: string | null = null;
- showCreateForm = false;
- newSessionTitle = '';
- @Input() instanceId: string | undefined;
- menuItemId: string | null = null;
-
- private authSubscription?: Subscription;
-
- constructor(
- private sessionService: SessionService,
- private authService: AuthService,
- private menuService: MenuService,
- private instanceService: InstanceService
- ) {}
-
- ngOnInit() {
- // 初始检查认证状态
- if (this.authService.isAuthenticated()) {
- this.loadMenuAndSessions();
- } else {
- // 订阅认证状态变化,登录后加载会话
- this.authSubscription = this.authService.authState$.subscribe(authState => {
- if (authState.isAuthenticated) {
- console.log('用户已认证,加载会话列表');
- this.loadMenuAndSessions();
- }
- });
- }
- }
-
- ngOnChanges(changes: SimpleChanges) {
- // 如果instanceId变化,重新加载会话
- if (changes['instanceId'] && !changes['instanceId'].firstChange) {
- console.log('实例ID变化,重新加载会话:', this.instanceId);
- this.loadMenuAndSessions();
- }
- }
-
- ngOnDestroy() {
- if (this.authSubscription) {
- this.authSubscription.unsubscribe();
- }
- }
-
- loadMenuAndSessions() {
- if (this.instanceId) {
- // 通过实例ID加载
- const instance = this.instanceService.getInstanceById(this.instanceId);
- if (instance) {
- this.menuItemId = instance.menuItemId;
- console.log('使用实例菜单项ID:', this.menuItemId);
- this.loadSessionsForInstance();
- } else {
- console.error('实例不存在:', this.instanceId);
- this.loadSessions(); // 回退到普通加载
- }
- } else {
- // 没有实例ID,使用旧逻辑:加载菜单,获取第一个菜单项ID
- this.menuService.getTopMenu().subscribe({
- next: (menuItems) => {
- if (menuItems.length > 0) {
- this.menuItemId = menuItems[0].id;
- console.log('使用第一个菜单项ID:', this.menuItemId);
- this.loadSessions();
- } else {
- console.error('没有可用的菜单项');
- this.loadSessions(); // 仍然尝试加载会话,但创建会话时会失败
- }
- },
- error: (error) => {
- console.error('加载菜单失败:', error);
- // 仍然尝试加载会话
- this.loadSessions();
- }
- });
- }
- }
-
- loadSessions() {
- this.sessionService.getSessions().subscribe({
- next: (sessions) => {
- // 如果没有实例ID,显示所有会话
- this.sessions = sessions;
- if (sessions.length > 0 && !this.activeSessionId) {
- this.selectSession(sessions[0]);
- }
- },
- error: (error) => {
- console.error('加载会话列表失败:', error);
- }
- });
- }
-
- loadSessionsForInstance() {
- if (!this.instanceId) {
- this.loadSessions();
- return;
- }
-
- this.instanceService.getInstanceSessions(this.instanceId).subscribe({
- next: (sessions) => {
- this.sessions = sessions;
- if (sessions.length > 0 && !this.activeSessionId) {
- this.selectSession(sessions[0]);
- }
- },
- error: (error) => {
- console.error('加载实例会话失败:', error);
- }
- });
- }
-
- createNewSession() {
- this.showCreateForm = true;
- }
-
- cancelCreate() {
- this.showCreateForm = false;
- this.newSessionTitle = '';
- }
-
- confirmCreate() {
- if (!this.newSessionTitle.trim()) return;
-
- if (!this.menuItemId) {
- console.error('无法创建会话:未找到菜单项ID');
- // 尝试重新加载菜单
- this.loadMenuAndSessions();
- return;
- }
-
- this.sessionService.createSession(this.newSessionTitle, this.menuItemId!).subscribe({
- next: (session) => {
- this.sessions.unshift(session);
- this.selectSession(session);
- this.showCreateForm = false;
- this.newSessionTitle = '';
- },
- error: (error) => {
- console.error('创建会话失败:', error);
- }
- });
- }
-
- selectSession(session: Session) {
- this.activeSessionId = session.id;
- // 触发会话切换事件(通过服务)
- this.sessionService.setActiveSession(session);
- }
- }
|