浏览代码

优化一键更新描述词

黎海 2 月之前
父节点
当前提交
3611fadf7e
共有 1 个文件被更改,包括 75 次插入20 次删除
  1. 75 20
      src/pages/projectDetail/projectDetail.js

+ 75 - 20
src/pages/projectDetail/projectDetail.js

@@ -481,6 +481,15 @@ const ProjectDetail = () => {
481
               description: description
481
               description: description
482
             });
482
             });
483
 
483
 
484
+            // 直接更新状态而不是重新加载
485
+            setSegments(prevSegments =>
486
+              prevSegments.map(seg =>
487
+                seg.id === segment.id
488
+                  ? { ...seg, description: description }
489
+                  : seg
490
+              )
491
+            );
492
+
484
             console.log(`分镜ID ${segment.id} 的描述词生成成功: ${description.slice(0, 30)}...`);
493
             console.log(`分镜ID ${segment.id} 的描述词生成成功: ${description.slice(0, 30)}...`);
485
             successCount++;
494
             successCount++;
486
           } catch (error) {
495
           } catch (error) {
@@ -505,6 +514,15 @@ const ProjectDetail = () => {
505
                 await bookInfoService.updateBookInfo(segment.id, {
514
                 await bookInfoService.updateBookInfo(segment.id, {
506
                   description: `[生成失败] ${errorMsg}`
515
                   description: `[生成失败] ${errorMsg}`
507
                 });
516
                 });
517
+
518
+                // 直接更新状态而不是重新加载
519
+                setSegments(prevSegments =>
520
+                  prevSegments.map(seg =>
521
+                    seg.id === segment.id
522
+                      ? { ...seg, description: `[生成失败] ${errorMsg}` }
523
+                      : seg
524
+                  )
525
+                );
508
               }
526
               }
509
             } catch (saveError) {
527
             } catch (saveError) {
510
               console.error('保存错误信息失败:', saveError);
528
               console.error('保存错误信息失败:', saveError);
@@ -513,9 +531,6 @@ const ProjectDetail = () => {
513
         }
531
         }
514
       }
532
       }
515
 
533
 
516
-      // 刷新数据
517
-      await loadProjectDetail();
518
-
519
       // 显示结果统计
534
       // 显示结果统计
520
       if (errorCount > 0 && successCount > 0) {
535
       if (errorCount > 0 && successCount > 0) {
521
         toast.warning(`描述词生成完成,成功: ${successCount}个,失败: ${errorCount}个`);
536
         toast.warning(`描述词生成完成,成功: ${successCount}个,失败: ${errorCount}个`);
@@ -878,6 +893,15 @@ const ProjectDetail = () => {
878
       for (let i = 0; i < segmentsToProcess.length; i++) {
893
       for (let i = 0; i < segmentsToProcess.length; i++) {
879
         const segment = segmentsToProcess[i];
894
         const segment = segmentsToProcess[i];
880
 
895
 
896
+        // 每次处理前检查分镜的当前状态
897
+        const currentSegment = await getSegmentById(segment.id);
898
+        
899
+        // 如果分镜状态不是排队状态(0),或者已经有图片,跳过处理
900
+        if (currentSegment.draw_status !== 0 || currentSegment.image_path) {
901
+          console.log(`分镜ID ${segment.id} 当前状态不是排队状态或已有图片,跳过处理`);
902
+          continue;
903
+        }
904
+
881
         // 每次处理前检查是否有分镜处于暂停状态(-1)
905
         // 每次处理前检查是否有分镜处于暂停状态(-1)
882
         // 获取最新的分镜状态
906
         // 获取最新的分镜状态
883
         const latestSegments = await bookInfoService.getBookInfoByBookId(parseInt(projectId));
907
         const latestSegments = await bookInfoService.getBookInfoByBookId(parseInt(projectId));
@@ -906,6 +930,13 @@ const ProjectDetail = () => {
906
         progress.current = currentCompletedCount;
930
         progress.current = currentCompletedCount;
907
         setImageGenerationProgress({ ...progress });
931
         setImageGenerationProgress({ ...progress });
908
 
932
 
933
+        // 再次检查分镜状态,确保没有被其他进程处理
934
+        const segmentBeforeUpdate = await getSegmentById(segment.id);
935
+        if (segmentBeforeUpdate.draw_status !== 0 || segmentBeforeUpdate.image_path) {
936
+          console.log(`分镜ID ${segment.id} 已被其他进程处理,跳过`);
937
+          continue;
938
+        }
939
+
909
         // 更新状态为绘画中(1)
940
         // 更新状态为绘画中(1)
910
         await bookInfoService.updateBookInfo(segment.id, {
941
         await bookInfoService.updateBookInfo(segment.id, {
911
           draw_status: 1 // 绘画中状态
942
           draw_status: 1 // 绘画中状态
@@ -920,8 +951,8 @@ const ProjectDetail = () => {
920
 
951
 
921
             // 再次检查当前分镜状态,确保没有被暂停
952
             // 再次检查当前分镜状态,确保没有被暂停
922
             const currentSegment = await getSegmentById(segment.id);
953
             const currentSegment = await getSegmentById(segment.id);
923
-            if (currentSegment.draw_status === -1) {
924
-              console.log('检测到当前分镜已被设置为暂停状态,跳过处理');
954
+            if (currentSegment.draw_status === -1 || currentSegment.image_path) {
955
+              console.log('检测到当前分镜已被设置为暂停状态或已有图片,跳过处理');
925
               return null;
956
               return null;
926
             }
957
             }
927
 
958
 
@@ -953,10 +984,10 @@ const ProjectDetail = () => {
953
             continue;
984
             continue;
954
           }
985
           }
955
 
986
 
956
-          // 检查分镜当前状态,如果已设置为暂停状态,则不保存结果
987
+          // 检查分镜当前状态,如果已设置为暂停状态或已有图片,则不保存结果
957
           const segmentAfterApiCall = await getSegmentById(segment.id);
988
           const segmentAfterApiCall = await getSegmentById(segment.id);
958
-          if (segmentAfterApiCall.draw_status === -1) {
959
-            console.log('API请求完成后检测到分镜已被设置为暂停状态,不保存结果');
989
+          if (segmentAfterApiCall.draw_status === -1 || segmentAfterApiCall.image_path) {
990
+            console.log('API请求完成后检测到分镜已被设置为暂停状态或已有图片,不保存结果');
960
             setGeneratingImages(false);
991
             setGeneratingImages(false);
961
             // 重置进度状态
992
             // 重置进度状态
962
             setImageGenerationProgress({ current: 0, total: 0 });
993
             setImageGenerationProgress({ current: 0, total: 0 });
@@ -994,9 +1025,13 @@ const ProjectDetail = () => {
994
             });
1025
             });
995
 
1026
 
996
             console.log(`分镜ID ${segment.id} 的图片生成成功: ${imagePath}`);
1027
             console.log(`分镜ID ${segment.id} 的图片生成成功: ${imagePath}`);
1028
+            successCount++; // 增加成功计数
997
 
1029
 
998
-            // 更新进度为1/1,表示图片生成100%完成
999
-            setImageGenerationProgress({ current: 1, total: 1 });
1030
+            // 更新进度
1031
+            const currentCompletedCount = (await bookInfoService.getBookInfoByBookId(parseInt(projectId)))
1032
+              .filter(segment => segment.draw_status === 2 && segment.image_path).length;
1033
+            progress.current = currentCompletedCount;
1034
+            setImageGenerationProgress({ ...progress });
1000
 
1035
 
1001
             // 实时更新图片显示
1036
             // 实时更新图片显示
1002
             setSegments(prevSegments =>
1037
             setSegments(prevSegments =>
@@ -1130,6 +1165,15 @@ const ProjectDetail = () => {
1130
       for (let i = 0; i < segmentsToProcess.length; i++) {
1165
       for (let i = 0; i < segmentsToProcess.length; i++) {
1131
         const segment = segmentsToProcess[i];
1166
         const segment = segmentsToProcess[i];
1132
 
1167
 
1168
+        // 每次处理前检查分镜的当前状态
1169
+        const currentSegment = await getSegmentById(segment.id);
1170
+        
1171
+        // 如果分镜状态不是排队状态(0),或者已经有图片,跳过处理
1172
+        if (currentSegment.draw_status !== 0 || currentSegment.image_path) {
1173
+          console.log(`分镜ID ${segment.id} 当前状态不是排队状态或已有图片,跳过处理`);
1174
+          continue;
1175
+        }
1176
+
1133
         // 每次处理前检查是否有分镜处于暂停状态(-1)
1177
         // 每次处理前检查是否有分镜处于暂停状态(-1)
1134
         // 获取最新的分镜状态
1178
         // 获取最新的分镜状态
1135
         const latestSegments = await bookInfoService.getBookInfoByBookId(parseInt(projectId));
1179
         const latestSegments = await bookInfoService.getBookInfoByBookId(parseInt(projectId));
@@ -1158,6 +1202,13 @@ const ProjectDetail = () => {
1158
         progress.current = currentCompletedCount;
1202
         progress.current = currentCompletedCount;
1159
         setImageGenerationProgress({ ...progress });
1203
         setImageGenerationProgress({ ...progress });
1160
 
1204
 
1205
+        // 再次检查分镜状态,确保没有被其他进程处理
1206
+        const segmentBeforeUpdate = await getSegmentById(segment.id);
1207
+        if (segmentBeforeUpdate.draw_status !== 0 || segmentBeforeUpdate.image_path) {
1208
+          console.log(`分镜ID ${segment.id} 已被其他进程处理,跳过`);
1209
+          continue;
1210
+        }
1211
+
1161
         // 更新状态为绘画中(1)
1212
         // 更新状态为绘画中(1)
1162
         await bookInfoService.updateBookInfo(segment.id, {
1213
         await bookInfoService.updateBookInfo(segment.id, {
1163
           draw_status: 1 // 绘画中状态
1214
           draw_status: 1 // 绘画中状态
@@ -1172,8 +1223,8 @@ const ProjectDetail = () => {
1172
 
1223
 
1173
             // 再次检查当前分镜状态,确保没有被暂停
1224
             // 再次检查当前分镜状态,确保没有被暂停
1174
             const currentSegment = await getSegmentById(segment.id);
1225
             const currentSegment = await getSegmentById(segment.id);
1175
-            if (currentSegment.draw_status === -1) {
1176
-              console.log('检测到当前分镜已被设置为暂停状态,跳过处理');
1226
+            if (currentSegment.draw_status === -1 || currentSegment.image_path) {
1227
+              console.log('检测到当前分镜已被设置为暂停状态或已有图片,跳过处理');
1177
               return null;
1228
               return null;
1178
             }
1229
             }
1179
 
1230
 
@@ -1205,10 +1256,10 @@ const ProjectDetail = () => {
1205
             continue;
1256
             continue;
1206
           }
1257
           }
1207
 
1258
 
1208
-          // 检查分镜当前状态,如果已设置为暂停状态,则不保存结果
1259
+          // 检查分镜当前状态,如果已设置为暂停状态或已有图片,则不保存结果
1209
           const segmentAfterApiCall = await getSegmentById(segment.id);
1260
           const segmentAfterApiCall = await getSegmentById(segment.id);
1210
-          if (segmentAfterApiCall.draw_status === -1) {
1211
-            console.log('API请求完成后检测到分镜已被设置为暂停状态,不保存结果');
1261
+          if (segmentAfterApiCall.draw_status === -1 || segmentAfterApiCall.image_path) {
1262
+            console.log('API请求完成后检测到分镜已被设置为暂停状态或已有图片,不保存结果');
1212
             setGeneratingImages(false);
1263
             setGeneratingImages(false);
1213
             // 重置进度状态
1264
             // 重置进度状态
1214
             setImageGenerationProgress({ current: 0, total: 0 });
1265
             setImageGenerationProgress({ current: 0, total: 0 });
@@ -1244,9 +1295,13 @@ const ProjectDetail = () => {
1244
             });
1295
             });
1245
 
1296
 
1246
             console.log(`分镜ID ${segment.id} 的图片生成成功: ${imagePath}`);
1297
             console.log(`分镜ID ${segment.id} 的图片生成成功: ${imagePath}`);
1298
+            successCount++; // 增加成功计数
1247
 
1299
 
1248
-            // 更新进度为1/1,表示图片生成100%完成
1249
-            setImageGenerationProgress({ current: 1, total: 1 });
1300
+            // 更新进度
1301
+            const currentCompletedCount = (await bookInfoService.getBookInfoByBookId(parseInt(projectId)))
1302
+              .filter(segment => segment.draw_status === 2 && segment.image_path).length;
1303
+            progress.current = currentCompletedCount;
1304
+            setImageGenerationProgress({ ...progress });
1250
 
1305
 
1251
             // 实时更新图片显示
1306
             // 实时更新图片显示
1252
             setSegments(prevSegments =>
1307
             setSegments(prevSegments =>
@@ -2306,7 +2361,7 @@ const ProjectDetail = () => {
2306
                     className="me-3"
2361
                     className="me-3"
2307
                   >
2362
                   >
2308
                     {generatingDescriptions
2363
                     {generatingDescriptions
2309
-                      ? <span>生成中 <span className="generation-progress">{generationProgress.current}/{generationProgress.total}</span></span>
2364
+                      ? `生成中 ${generationProgress.current}/${generationProgress.total}`
2310
                       : (hasDescriptions ? '一键更新描述词' : '一键生成描述词')}
2365
                       : (hasDescriptions ? '一键更新描述词' : '一键生成描述词')}
2311
                   </Button>
2366
                   </Button>
2312
                   {isPaused ? (
2367
                   {isPaused ? (
@@ -2336,7 +2391,7 @@ const ProjectDetail = () => {
2336
                             size="sm"
2391
                             size="sm"
2337
                             onClick={pauseImageGeneration}
2392
                             onClick={pauseImageGeneration}
2338
                           >
2393
                           >
2339
-                            暂停绘画 <span className="generation-progress">{imageGenerationProgress.current}/{imageGenerationProgress.total}</span>
2394
+                            暂停绘画 {imageGenerationProgress.current}/{imageGenerationProgress.total}
2340
                           </Button>
2395
                           </Button>
2341
                         </div>
2396
                         </div>
2342
                       ) : (
2397
                       ) : (
@@ -2353,7 +2408,7 @@ const ProjectDetail = () => {
2353
                               onClick={() => generateAllImages(false)}
2408
                               onClick={() => generateAllImages(false)}
2354
                               disabled={!selectedStyle}
2409
                               disabled={!selectedStyle}
2355
                             >
2410
                             >
2356
-                              继续绘画
2411
+                              开始绘画
2357
                             </Dropdown.Item>
2412
                             </Dropdown.Item>
2358
                             <Dropdown.Item
2413
                             <Dropdown.Item
2359
                               onClick={() => generateAllImages(true)}
2414
                               onClick={() => generateAllImages(true)}