workspace-flow.component.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import { Component } from '@angular/core';
  2. import { CommonModule } from '@angular/common';
  3. import { BriefUploadComponent } from '../brief-upload/brief-upload.component';
  4. import { TaskProgressComponent } from '../task-progress/task-progress.component';
  5. import { ExportPanelComponent } from '../export-panel/export-panel.component';
  6. import { WorkspaceApiService } from '../../services/workspace-api.service';
  7. import { TaskStep, TaskStatus } from '../../models/task.model';
  8. @Component({
  9. selector: 'app-workspace-flow',
  10. standalone: true,
  11. imports: [CommonModule, BriefUploadComponent, TaskProgressComponent, ExportPanelComponent],
  12. templateUrl: './workspace-flow.component.html',
  13. styleUrl: './workspace-flow.component.scss',
  14. })
  15. export class WorkspaceFlowComponent {
  16. currentTaskId: string | null = null;
  17. taskStatus: TaskStatus = 'pending';
  18. steps: TaskStep[] = [];
  19. candidateCount = 0;
  20. exporting = false;
  21. errorMessage = '';
  22. libraryUploading = false;
  23. libraryMessage = '';
  24. stopPolling: (() => void) | null = null;
  25. constructor(private api: WorkspaceApiService) {}
  26. get isProcessing(): boolean {
  27. return ['uploading', 'analyzing', 'searching', 'processing'].includes(this.taskStatus);
  28. }
  29. get isCompleted(): boolean {
  30. return this.taskStatus === 'completed';
  31. }
  32. get showProgress(): boolean {
  33. return this.isProcessing || this.isCompleted || this.taskStatus === 'failed';
  34. }
  35. async onFileSelected(file: File): Promise<void> {
  36. this.errorMessage = '';
  37. this.taskStatus = 'uploading';
  38. this.steps = [];
  39. this.candidateCount = 0;
  40. try {
  41. const response = await this.api.uploadBrief(file);
  42. this.currentTaskId = response.taskId;
  43. this.taskStatus = 'analyzing';
  44. // Start the task processing
  45. await this.api.startTask(response.taskId);
  46. // Poll for progress
  47. this.startPolling(response.taskId);
  48. } catch (error: unknown) {
  49. this.taskStatus = 'failed';
  50. this.errorMessage = error instanceof Error ? error.message : '上传失败';
  51. }
  52. }
  53. async onCreatorLibraryInput(event: Event): Promise<void> {
  54. const input = event.target as HTMLInputElement;
  55. const file = input.files?.[0];
  56. if (!file) return;
  57. this.libraryUploading = true;
  58. this.libraryMessage = '';
  59. this.errorMessage = '';
  60. try {
  61. const result = await this.api.uploadCreatorLibrary(file);
  62. this.libraryMessage = `已入库 ${result.localCreatorCount} 位达人:${result.fileName}`;
  63. } catch (error: unknown) {
  64. this.errorMessage = error instanceof Error ? error.message : '达人资料库上传失败';
  65. } finally {
  66. this.libraryUploading = false;
  67. input.value = '';
  68. }
  69. }
  70. private startPolling(taskId: string): void {
  71. if (this.stopPolling) {
  72. this.stopPolling();
  73. }
  74. this.stopPolling = this.api.pollTaskProgress(taskId, (event) => {
  75. const stepIndex = this.steps.findIndex((s) => s.id === event.step);
  76. const stepData: TaskStep = {
  77. id: event.step,
  78. label: this.getStepLabel(event.step),
  79. status: event.status,
  80. progress: event.progress,
  81. detail: event.detail,
  82. };
  83. if (stepIndex >= 0) {
  84. this.steps[stepIndex] = stepData;
  85. } else {
  86. this.steps.push(stepData);
  87. }
  88. // Update overall status
  89. if (event.data && typeof event.data === 'object') {
  90. const data = event.data as Record<string, unknown>;
  91. if (data['candidates'] && Array.isArray(data['candidates'])) {
  92. this.candidateCount = data['candidates'].length;
  93. }
  94. if (data['totalPool']) {
  95. this.candidateCount = data['totalPool'] as number;
  96. }
  97. }
  98. const allDone = this.steps.length > 0 && this.steps.every((s) => s.status === 'done');
  99. const anyError = this.steps.some((s) => s.status === 'error');
  100. if (allDone) {
  101. this.taskStatus = 'completed';
  102. } else if (anyError) {
  103. this.taskStatus = 'failed';
  104. this.errorMessage = this.steps.find((s) => s.status === 'error')?.detail || '处理失败';
  105. } else {
  106. this.taskStatus = 'processing';
  107. }
  108. });
  109. }
  110. async onExport(): Promise<void> {
  111. if (!this.currentTaskId) return;
  112. this.exporting = true;
  113. try {
  114. const blob = await this.api.exportRecommendation(this.currentTaskId);
  115. const url = URL.createObjectURL(blob);
  116. const anchor = document.createElement('a');
  117. anchor.href = url;
  118. anchor.download = `推荐表_${new Date().toISOString().slice(0, 10)}.xlsx`;
  119. anchor.click();
  120. URL.revokeObjectURL(url);
  121. } catch (error: unknown) {
  122. this.errorMessage = error instanceof Error ? error.message : '导出失败';
  123. } finally {
  124. this.exporting = false;
  125. }
  126. }
  127. private getStepLabel(stepId: string): string {
  128. const labels: Record<string, string> = {
  129. upload: '上传文件',
  130. analyze: 'AI 解析 Brief 需求',
  131. rules: '确定筛选规则',
  132. search: '搜索达人资源',
  133. recommend: '生成推荐名单',
  134. export: '准备导出',
  135. };
  136. return labels[stepId] || stepId;
  137. }
  138. ngOnDestroy(): void {
  139. if (this.stopPolling) {
  140. this.stopPolling();
  141. }
  142. }
  143. }