Sfoglia il codice sorgente

feat: 创建前AI预览检测书籍规模 + 描述失焦自动触发

MyFramework User 3 mesi fa
parent
commit
139054c1c5

+ 28 - 0
my-uniapp-vue3/src/pages/book-generator/create.vue

@@ -42,6 +42,7 @@
             class="form-textarea"
             class="form-textarea"
             placeholder="描述这本书的内容、主题、写作目的..."
             placeholder="描述这本书的内容、主题、写作目的..."
             :maxlength="500"
             :maxlength="500"
+            @blur="detectScaleFromDesc"
           />
           />
         </view>
         </view>
 
 
@@ -658,6 +659,33 @@ function switchToInteractive() {
   uni.redirectTo({ url: '/pages/book-generator/interactive' });
   uni.redirectTo({ url: '/pages/book-generator/interactive' });
 }
 }
 
 
+// AI 预览检测规模(用户未手动选时,描述失焦自动触发)
+let detectTimer: any = null;
+async function detectScaleFromDesc() {
+  const desc = newBook.value.description?.trim();
+  if (!desc || newBook.value.bookScale) return; // 已手动选过,不覆盖
+  clearTimeout(detectTimer);
+  detectTimer = setTimeout(async () => {
+    try {
+      const response = await uni.request({
+        url: `${BASE_URL}/book-generator/langgraph/detect-scale`,
+        method: 'POST',
+        data: { description: desc }
+      });
+      const res = response.data as any;
+      if (res.code === 0 && res.data) {
+        newBook.value.bookScale = res.data.bookScale;
+        bookEstimate.value = {
+          scale: res.data.bookScale,
+          words: { min: res.data.totalWords, max: res.data.totalWords, avg: res.data.totalWords },
+          audioMinutes: { min: 0, max: 0, avg: 0 },
+          estimatedChapters: res.data.chapters,
+        };
+      }
+    } catch { /* AI 调用失败不影响用户操作 */ }
+  }, 800);
+}
+
 function goBack() {
 function goBack() {
   const pages = getCurrentPages();
   const pages = getCurrentPages();
   if (pages.length > 1) {
   if (pages.length > 1) {

+ 23 - 0
server/src/modules/book-generator/langgraph-controller.ts

@@ -89,6 +89,29 @@ router.get('/estimate', async (ctx: Context) => {
   };
   };
 });
 });
 
 
+/**
+ * POST /api/book-generator/langgraph/detect-scale
+ * AI 预览检测书籍规模(创建前调用,让用户看到检测结果)
+ */
+router.post('/detect-scale', async (ctx) => {
+  const { description } = ctx.request.body as { description?: string };
+  if (!description) {
+    ctx.body = { code: 1, message: '缺少 description' };
+    return;
+  }
+  const scale = await autoDetectBookType('', description);
+  const config = getScaleConfig(scale);
+  ctx.body = {
+    code: 0,
+    data: {
+      bookScale: scale,
+      totalWords: config.totalWords,
+      chapters: config.chapters,
+      isShortArticle: config.isShortArticle,
+    },
+  };
+});
+
 /**
 /**
  * GET /api/book-generator/langgraph/book-types
  * GET /api/book-generator/langgraph/book-types
  * 获取所有书籍类型配置(供前端显示)
  * 获取所有书籍类型配置(供前端显示)