detail.vue 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  1. <template>
  2. <view class="page">
  3. <!-- 顶部导航栏 -->
  4. <view class="nav-bar">
  5. <view class="nav-content">
  6. <view class="nav-left" @click="goBack">
  7. <text class="back-icon">←</text>
  8. </view>
  9. <text class="page-title">{{ currentBook?.title || '书籍详情' }}</text>
  10. <view class="nav-right"></view>
  11. </view>
  12. </view>
  13. <!-- 主内容区 -->
  14. <view class="main-content">
  15. <!-- 书籍信息 -->
  16. <view class="card book-info">
  17. <view class="book-title-row">
  18. <text class="book-title-large">{{ currentBook?.title }}</text>
  19. <view :class="['status-badge', currentBook?.status]">
  20. {{ getStatusText(currentBook?.status || 'draft') }}
  21. </view>
  22. </view>
  23. <text v-if="currentBook?.subtitle" class="book-subtitle">{{ currentBook.subtitle }}</text>
  24. <text class="book-desc">{{ currentBook?.description }}</text>
  25. <view class="book-meta-row">
  26. <text>章节:{{ currentBook?.totalChapters }} 章</text>
  27. <text>预估:{{ currentBook?.estimatedWords || 0 }} 字</text>
  28. </view>
  29. </view>
  30. <!-- 进度显示 -->
  31. <view v-if="currentBook && currentBook.progress > 0" class="card progress-card">
  32. <text class="card-title">📊 生成进度</text>
  33. <view class="progress-display">
  34. <text class="progress-text">{{ currentBook.progress }}%</text>
  35. <text class="progress-detail">
  36. {{ completedChapters }}/{{ currentBook.totalChapters }} 章
  37. </text>
  38. </view>
  39. <view class="progress-bar-large">
  40. <view class="progress-fill" :style="{ width: currentBook.progress + '%' }"></view>
  41. </view>
  42. <!-- 实时生成状态面板 -->
  43. <view v-if="currentBook.status === 'generating'" class="generation-status">
  44. <view class="status-header">
  45. <text class="status-icon">⚡</text>
  46. <text class="status-title">正在生成中...</text>
  47. </view>
  48. <view class="status-stage">
  49. <text class="stage-label">当前阶段:</text>
  50. <text class="stage-value">{{ getCurrentStage() }}</text>
  51. </view>
  52. <view v-if="currentBook.error" class="status-error">
  53. <text class="error-icon">⚠️</text>
  54. <text class="error-text">{{ currentBook.error }}</text>
  55. </view>
  56. <view v-if="currentBook.error && currentBook.error.includes('自动恢复')" class="status-recovery">
  57. <text class="recovery-icon">🔄</text>
  58. <text class="recovery-text">系统正在尝试自动恢复...</text>
  59. </view>
  60. <view class="status-tip">
  61. <text class="tip-text">💡 生成过程可能需要几分钟,请耐心等待</text>
  62. </view>
  63. </view>
  64. <!-- 实时配额显示 -->
  65. <view v-if="currentBook.status === 'generating'" class="quota-monitor">
  66. <text class="quota-monitor-title">🎧 额度监控</text>
  67. <view class="quota-monitor-row">
  68. <text>已用:{{ usedAudioMinutes }}分钟</text>
  69. <text>剩余:{{ remainingAudioMinutes }}分钟</text>
  70. </view>
  71. <view v-if="overageMinutes > 0" class="quota-overage-warning">
  72. <text>⚠️ 已超出配额 {{ overageMinutes }} 分钟</text>
  73. </view>
  74. </view>
  75. <!-- 中断提示 -->
  76. <view v-if="currentBook.status === 'failed' && currentBook?.error?.includes('额度')" class="interrupted-tip">
  77. <text class="interrupted-icon">⚠️</text>
  78. <text class="interrupted-text">生成已中断:额度不足,已保存当前进度</text>
  79. <text class="interrupted-hint">可升级套餐后继续生成</text>
  80. </view>
  81. <!-- 失败提示 -->
  82. <view v-if="currentBook.status === 'failed' && !currentBook?.error?.includes('额度')" class="failed-tip">
  83. <text class="failed-icon">❌</text>
  84. <text class="failed-text">生成失败</text>
  85. <text v-if="currentBook.error" class="failed-error">{{ currentBook.error }}</text>
  86. <text class="failed-hint">请检查网络或稍后重试</text>
  87. <button class="retry-btn" @click="handleRetryGenerate">🔄 重新生成</button>
  88. </view>
  89. </view>
  90. <!-- 完整内容区 -->
  91. <view v-if="currentBook?.chapters && currentBook.chapters.length > 0" class="card content-card">
  92. <view class="card-header-row">
  93. <text class="card-title">📖 完整内容</text>
  94. <view class="header-actions">
  95. <text v-if="currentBook.outline?.mainTheme" class="outline-theme">{{ currentBook.outline.mainTheme }}</text>
  96. <view v-if="!isBatchMode" class="edit-btn" @click="enterBatchMode">
  97. <text>批量</text>
  98. </view>
  99. </view>
  100. </view>
  101. <!-- 批量操作栏 -->
  102. <view v-if="isBatchMode" class="batch-actions-bar">
  103. <view class="batch-btn" @click="toggleSelectAll">
  104. <text>{{ isAllSelected ? '取消全选' : '全选' }}</text>
  105. </view>
  106. <view class="batch-btn regenerate" @click="handleBatchRegenerate" :class="{ disabled: selectedChapterIds.length === 0 }">
  107. <text>重新生成 ({{ selectedChapterIds.length }})</text>
  108. </view>
  109. <view class="batch-btn delete" @click="handleBatchDelete" :class="{ disabled: selectedChapterIds.length === 0 }">
  110. <text>删除 ({{ selectedChapterIds.length }})</text>
  111. </view>
  112. <view class="batch-btn cancel" @click="exitBatchMode">
  113. <text>取消</text>
  114. </view>
  115. </view>
  116. <!-- 前言 -->
  117. <view v-if="currentBook?.metadata?.foreword" class="content-item foreword" @click="viewForeword">
  118. <view class="content-item-left">
  119. <text class="content-label">前言</text>
  120. <text class="content-word-count">{{ currentBook?.metadata?.foreword?.length || 0 }}字</text>
  121. </view>
  122. <view class="content-item-right">
  123. <text class="expand-icon">→</text>
  124. </view>
  125. </view>
  126. <!-- 章节列表 -->
  127. <view class="content-tree-list">
  128. <view
  129. v-for="chapter in chaptersLevel1"
  130. :key="chapter.id"
  131. class="content-tree-item content-chapter"
  132. >
  133. <view class="content-tree-row chapter-row">
  134. <view class="chapter-num-badge">{{ chapter.number }}</view>
  135. <view class="chapter-info">
  136. <text class="chapter-title">{{ chapter.title }}</text>
  137. <view v-if="chapter.content && chapter.content.length > 100" class="chapter-content-hint">
  138. <text class="content-hint-tag">📄 正文 {{ chapter.wordCount || 0 }}字</text>
  139. </view>
  140. <text v-else-if="getChapterOutline(chapter.number)?.summary" class="chapter-outline-summary">
  141. {{ getChapterOutline(chapter.number)?.summary }}
  142. </text>
  143. <view v-if="!chapter.content || chapter.content.length <= 100" class="chapter-key-points">
  144. <text
  145. v-for="(point, idx) in getChapterOutline(chapter.number)?.keyPoints?.slice(0, 3) || []"
  146. :key="idx"
  147. class="point-tag"
  148. >
  149. {{ point }}
  150. </text>
  151. </view>
  152. <view class="chapter-media-badges">
  153. <text v-if="chapterHasAudio(chapter)" class="media-badge">🎵 音频</text>
  154. <text v-if="chapter.videoUrl" class="media-badge">🎬 视频</text>
  155. </view>
  156. </view>
  157. <view :class="['chapter-status', getNodeStatusClass(chapter)]">
  158. <text class="status-icon">{{ getNodeStatusIcon(chapter) }}</text>
  159. <text class="status-text">{{ getNodeStatusText(chapter) }}</text>
  160. </view>
  161. </view>
  162. <!-- 节 -->
  163. <view v-if="getChapterSectionsFromDB(chapter.id) && getChapterSectionsFromDB(chapter.id).length > 0" class="content-sections-list">
  164. <view
  165. v-for="section in getChapterSectionsFromDB(chapter.id)"
  166. :key="'sec-' + section.id"
  167. class="content-tree-item content-section"
  168. >
  169. <view class="content-tree-row section-row">
  170. <view class="section-num-badge">{{ section.number }}</view>
  171. <view class="section-info">
  172. <text class="section-title">{{ section.title }}</text>
  173. <text v-if="section.summary" class="section-summary">{{ section.summary }}</text>
  174. </view>
  175. <view :class="['section-status', getNodeStatusClass(section)]">
  176. <text class="status-icon">{{ getNodeStatusIcon(section) }}</text>
  177. <text class="status-text">{{ getNodeStatusText(section) }}</text>
  178. </view>
  179. </view>
  180. <!-- 小节 -->
  181. <view v-if="getSectionSubsectionsFromDB(section.id) && getSectionSubsectionsFromDB(section.id).length > 0" class="content-subsections-list">
  182. <view
  183. v-for="subsection in getSectionSubsectionsFromDB(section.id)"
  184. :key="'sub-' + subsection.id"
  185. class="content-tree-item content-subsection"
  186. @click="goToChapterDetail(subsection)"
  187. >
  188. <view class="content-tree-row subsection-row">
  189. <view class="subsection-num-badge">{{ subsection.number }}</view>
  190. <view class="subsection-info">
  191. <text class="subsection-title">{{ subsection.title }}</text>
  192. <view class="subsection-media-badges">
  193. <text v-if="subsection.audioUrl" class="media-badge-small">🎵 音频</text>
  194. <text v-if="subsection.videoUrl" class="media-badge-small">🎬 视频</text>
  195. </view>
  196. <text v-if="subsection.content" class="subsection-content-preview">
  197. {{ subsection.content.substring(0, 50) }}...
  198. </text>
  199. </view>
  200. <view :class="['subsection-status', getNodeStatusClass(subsection)]">
  201. <text class="status-icon">{{ getNodeStatusIcon(subsection) }}</text>
  202. <text class="status-text">{{ getNodeStatusText(subsection) }}</text>
  203. </view>
  204. </view>
  205. </view>
  206. </view>
  207. </view>
  208. </view>
  209. <!-- 章没有节时章本身就是叶子节点 -->
  210. <view v-else class="content-tree-item content-subsection" @click="goToChapterDetail(chapter)">
  211. <view class="content-tree-row subsection-row">
  212. <view class="subsection-info" style="margin-left: 56rpx;">
  213. <text class="subsection-title">{{ chapter.title }}</text>
  214. </view>
  215. <view class="content-item-right">
  216. <text class="expand-icon">→</text>
  217. </view>
  218. </view>
  219. </view>
  220. </view>
  221. </view>
  222. <!-- 后记 -->
  223. <view v-if="currentBook?.metadata?.afterword" class="content-item afterword" @click="viewAfterword">
  224. <view class="content-item-left">
  225. <text class="content-label">后记</text>
  226. <text class="content-word-count">{{ currentBook?.metadata?.afterword?.length || 0 }}字</text>
  227. </view>
  228. <view class="content-item-right">
  229. <text class="expand-icon">→</text>
  230. </view>
  231. </view>
  232. </view>
  233. <!-- 操作按钮 -->
  234. <view class="card action-card">
  235. <view class="btn-col">
  236. <view class="btn-row">
  237. <button
  238. class="action-btn langgraph-btn"
  239. :disabled="generating"
  240. @click="handleLangGraphGenerate"
  241. >
  242. {{ generating ? '生成中...' : '🤖 开始生成' }}
  243. </button>
  244. </view>
  245. </view>
  246. <view v-if="generating" class="generating-tip">
  247. <text>正在生成中,请稍候...</text>
  248. </view>
  249. <view v-if="currentBook?.status === 'completed' && (!currentBook.chapters || currentBook.chapters.length === 0)" class="warning-tip">
  250. <text class="warning-icon">⚠️</text>
  251. <text class="warning-text">该书籍状态标记为已完成,但没有任何章节内容。</text>
  252. <text class="warning-hint">请尝试点击上方按钮重新生成。</text>
  253. </view>
  254. <button class="action-btn full-width" @click="goBack">
  255. 返回书籍列表
  256. </button>
  257. </view>
  258. </view>
  259. </view>
  260. </template>
  261. <script setup lang="ts">
  262. import { ref, computed, onMounted, onUnmounted } from 'vue';
  263. import { onLoad, onShow } from '@dcloudio/uni-app';
  264. import * as api from '../../utils/book-generator-api';
  265. import { post } from '../../utils/request';
  266. import type { Book, Chapter } from '../../utils/book-generator-api';
  267. import { wsService } from '../../utils/websocket';
  268. // 批量操作状态
  269. const isBatchMode = ref(false);
  270. const selectedChapterIds = ref<string[]>([]);
  271. const isAllSelected = ref(false);
  272. // 当前书籍
  273. const currentBook = ref<Book | null>(null);
  274. // 生成状态
  275. const generating = ref(false);
  276. // 轮询定时器
  277. let pollTimer: ReturnType<typeof setInterval> | null = null;
  278. // 计算属性
  279. const completedChapters = computed(() => {
  280. if (!currentBook.value) return 0;
  281. return currentBook.value.chapters.filter((c) => c.status === 'completed').length;
  282. });
  283. const chaptersLevel1 = computed(() => {
  284. if (!currentBook.value?.chapters) return [];
  285. return currentBook.value.chapters.filter((c) => c.level === 1);
  286. });
  287. const usedAudioMinutes = computed(() => {
  288. if (!currentBook.value) return 0;
  289. const totalWords = currentBook.value.chapters
  290. .filter((c) => c.status === 'completed')
  291. .reduce((sum, c) => sum + (c.wordCount || 0), 0);
  292. return Math.ceil(totalWords / 150);
  293. });
  294. const remainingAudioMinutes = computed(() => {
  295. if (!quotaInfo.value) return 0;
  296. return Math.max(0, quotaInfo.value.totalMinutes - quotaInfo.value.usedMinutes);
  297. });
  298. const overageMinutes = computed(() => {
  299. if (!currentBook.value) return 0;
  300. const used = usedAudioMinutes.value;
  301. if (!quotaInfo.value) return 0;
  302. return Math.max(0, used - quotaInfo.value.totalMinutes);
  303. });
  304. const quotaInfo = ref<{
  305. totalMinutes: number;
  306. usedMinutes: number;
  307. remainingMinutes: number;
  308. overageEnabled: boolean;
  309. } | null>(null);
  310. function getStatusText(status: string): string {
  311. const map: Record<string, string> = {
  312. draft: '草稿',
  313. planning: '规划中',
  314. generating: '生成中',
  315. completed: '已完成',
  316. failed: '失败',
  317. };
  318. return map[status] || status;
  319. }
  320. function getCurrentStage(): string {
  321. if (!currentBook.value) return '未知';
  322. const progress = currentBook.value.progress || 0;
  323. const error = currentBook.value.error || '';
  324. if (error) {
  325. if (error.includes('AI调用')) return 'AI调用失败,正在重试...';
  326. if (error.includes('超时')) return '节点执行超时,正在恢复...';
  327. if (error.includes('自动恢复')) return '系统正在自动恢复...';
  328. }
  329. if (progress === 0) return '准备中...';
  330. if (progress <= 10) return '正在生成章大纲...';
  331. if (progress <= 20) return '正在生成节大纲...';
  332. if (progress <= 30) return '正在生成小节大纲...';
  333. if (progress <= 90) return '正在生成章节内容...';
  334. if (progress <= 95) return '正在生成前言...';
  335. if (progress < 100) return '正在生成后记...';
  336. if (progress === 100) return '生成完成!';
  337. return '生成中...';
  338. }
  339. function getNodeStatusClass(node: any): string {
  340. if (node.contentStatus === 'completed') return 'status-completed';
  341. if (node.contentStatus === 'generating') return 'status-generating';
  342. if (node.contentStatus === 'failed') return 'status-failed';
  343. if (node.status === 'completed') return 'status-completed';
  344. if (node.status === 'pending') return 'status-pending';
  345. return 'status-pending';
  346. }
  347. function getNodeStatusIcon(node: any): string {
  348. if (node.contentStatus === 'completed') return '✓';
  349. if (node.contentStatus === 'generating') return '⚡';
  350. if (node.contentStatus === 'failed') return '✗';
  351. if (node.contentError) return '⚠';
  352. if (node.status === 'completed') return '✓';
  353. if (node.status === 'pending') return '○';
  354. return '○';
  355. }
  356. function getNodeStatusText(node: any): string {
  357. if (node.contentStatus === 'completed') return '已完成';
  358. if (node.contentStatus === 'generating') return '生成中';
  359. if (node.contentStatus === 'failed') return '失败';
  360. if (node.contentError) return '错误';
  361. if (node.status === 'completed') return '大纲完成';
  362. if (node.status === 'pending') return '待生成';
  363. return '待生成';
  364. }
  365. function getChapterSectionsFromDB(chapterId: string | number) {
  366. if (!currentBook.value?.chapters) return [];
  367. const chapterIdNum = typeof chapterId === 'string' ? parseInt(chapterId) : chapterId;
  368. return currentBook.value.chapters.filter((c: any) => {
  369. const cParentId = typeof c.parentId === 'string' ? parseInt(c.parentId) : c.parentId;
  370. return cParentId === chapterIdNum && c.level === 2;
  371. });
  372. }
  373. function getSectionSubsectionsFromDB(sectionId: string | number) {
  374. if (!currentBook.value?.chapters) return [];
  375. const sectionIdNum = typeof sectionId === 'string' ? parseInt(sectionId) : sectionId;
  376. return currentBook.value.chapters.filter((c: any) => {
  377. const cParentId = typeof c.parentId === 'string' ? parseInt(c.parentId) : cParentId;
  378. return cParentId === sectionIdNum && c.level === 3;
  379. });
  380. }
  381. function chapterHasAudio(chapter: any) {
  382. const leafLevel = getBookLeafLevel({ chapters: [chapter] } as Book);
  383. if (leafLevel === 1) {
  384. return !!chapter.audioUrl;
  385. }
  386. if (chapter.level === leafLevel) {
  387. return !!chapter.audioUrl;
  388. }
  389. const sections = getChapterSectionsFromDB(chapter.id);
  390. for (const section of sections) {
  391. if (section.level === leafLevel) {
  392. if (section.audioUrl) return true;
  393. } else {
  394. const subsections = getSectionSubsectionsFromDB(section.id);
  395. if (subsections.some((s: any) => s.audioUrl)) return true;
  396. }
  397. }
  398. return false;
  399. }
  400. function getBookLeafLevel(book: Book): number {
  401. const chapters = book.chapters || [];
  402. if (chapters.length === 0) return 0;
  403. const maxLevel = Math.max(...chapters.map(c => c.level || 0));
  404. return maxLevel > 0 ? maxLevel : 1;
  405. }
  406. function getBookLeafNodes(book: Book, leafLevel: number): any[] {
  407. const chapters = book.chapters || [];
  408. return chapters.filter(c => c.level === leafLevel);
  409. }
  410. function getChapterOutline(chapterNumber: number) {
  411. if (!currentBook.value?.outline?.chapters) return null;
  412. return currentBook.value.outline.chapters.find(c => c.number === chapterNumber) || null;
  413. }
  414. function goBack() {
  415. uni.navigateBack();
  416. }
  417. async function loadBook(id: string) {
  418. try {
  419. currentBook.value = await api.getBook(id);
  420. // 同步 generating 状态
  421. if (currentBook.value.status === 'generating') {
  422. generating.value = true;
  423. }
  424. startPollingProgress(id);
  425. } catch (e) {
  426. console.error('加载书籍失败:', e);
  427. uni.showToast({ title: '加载失败', icon: 'none' });
  428. }
  429. }
  430. function startPollingProgress(bookId: string) {
  431. stopPollingProgress();
  432. pollTimer = setInterval(async () => {
  433. try {
  434. const progress = await api.getProgress(bookId);
  435. if (!progress) return;
  436. if (currentBook.value) {
  437. currentBook.value.progress = progress.progress;
  438. currentBook.value.status = progress.status as any;
  439. }
  440. if (progress.status === 'completed' || progress.status === 'failed') {
  441. stopPollingProgress();
  442. generating.value = false;
  443. currentBook.value = await api.getBook(bookId);
  444. if (progress.status === 'completed') {
  445. uni.showToast({ title: '生成完成!', icon: 'success' });
  446. }
  447. }
  448. } catch (e) {
  449. console.error('轮询进度失败:', e);
  450. }
  451. }, 3000);
  452. }
  453. function stopPollingProgress() {
  454. if (pollTimer) {
  455. clearInterval(pollTimer);
  456. pollTimer = null;
  457. }
  458. }
  459. async function handleRetryGenerate() {
  460. if (!currentBook.value) return;
  461. uni.showModal({
  462. title: '确认重新生成',
  463. content: '将重新开始生成,是否继续?',
  464. success: async (res) => {
  465. if (res.confirm) {
  466. await handleLangGraphGenerate();
  467. }
  468. },
  469. });
  470. }
  471. function enterBatchMode() {
  472. isBatchMode.value = true;
  473. selectedChapterIds.value = [];
  474. isAllSelected.value = false;
  475. }
  476. function exitBatchMode() {
  477. isBatchMode.value = false;
  478. selectedChapterIds.value = [];
  479. isAllSelected.value = false;
  480. }
  481. function toggleSelectAll() {
  482. if (isAllSelected.value) {
  483. selectedChapterIds.value = [];
  484. isAllSelected.value = false;
  485. } else {
  486. const allSubsections = currentBook.value?.chapters?.filter(c => c.level === 3) || [];
  487. selectedChapterIds.value = allSubsections.map((ch: Chapter) => ch.id);
  488. isAllSelected.value = true;
  489. }
  490. }
  491. async function handleBatchRegenerate() {
  492. if (selectedChapterIds.value.length === 0 || !currentBook.value) return;
  493. const bookId = currentBook.value.id;
  494. uni.showModal({
  495. title: '确认重新生成',
  496. content: `将重新生成选中的 ${selectedChapterIds.value.length} 个章节,是否继续?`,
  497. success: async (res) => {
  498. if (res.confirm) {
  499. try {
  500. uni.showLoading({ title: '提交中...', mask: true });
  501. await post(`/book-generator/langgraph/books/${bookId}/chapters/batch-regenerate`, {
  502. chapterIds: selectedChapterIds.value,
  503. });
  504. uni.hideLoading();
  505. uni.showToast({ title: '已提交批量生成任务', icon: 'success' });
  506. exitBatchMode();
  507. currentBook.value = await api.getBook(bookId);
  508. } catch (error) {
  509. uni.hideLoading();
  510. console.error('[BatchRegenerate] 失败:', error);
  511. uni.showToast({ title: '批量重新生成失败', icon: 'none' });
  512. }
  513. }
  514. },
  515. });
  516. }
  517. async function handleBatchDelete() {
  518. if (selectedChapterIds.value.length === 0 || !currentBook.value) return;
  519. const bookId = currentBook.value.id;
  520. uni.showModal({
  521. title: '确认删除',
  522. content: `将删除选中的 ${selectedChapterIds.value.length} 个章节,是否继续?`,
  523. success: async (res) => {
  524. if (res.confirm) {
  525. try {
  526. uni.showLoading({ title: '删除中...', mask: true });
  527. await post(`/book-generator/langgraph/books/${bookId}/chapters/batch-delete`, {
  528. chapterIds: selectedChapterIds.value,
  529. });
  530. uni.hideLoading();
  531. uni.showToast({ title: '删除成功', icon: 'success' });
  532. exitBatchMode();
  533. currentBook.value = await api.getBook(bookId);
  534. } catch (error) {
  535. uni.hideLoading();
  536. console.error('[BatchDelete] 失败:', error);
  537. uni.showToast({ title: '批量删除失败', icon: 'none' });
  538. }
  539. }
  540. },
  541. });
  542. }
  543. async function handleLangGraphGenerate() {
  544. if (!currentBook.value) return;
  545. generating.value = true;
  546. currentBook.value.status = 'generating';
  547. currentBook.value.progress = 0;
  548. try {
  549. await api.generateWithLangGraph(currentBook.value.id);
  550. uni.showToast({ title: 'LangGraph 生成已启动', icon: 'none', duration: 2000 });
  551. startPollingProgress(currentBook.value.id);
  552. } catch (e: any) {
  553. generating.value = false;
  554. currentBook.value.status = 'failed';
  555. uni.showToast({ title: e.message || '启动失败', icon: 'none' });
  556. }
  557. }
  558. function goToChapterDetail(chapter: Chapter) {
  559. if (!currentBook.value) return;
  560. (globalThis as any).__navBookData = currentBook.value;
  561. uni.navigateTo({
  562. url: `/pages/book-generator/chapter-detail?bookId=${encodeURIComponent(currentBook.value.id)}&chapter=${chapter.id}`
  563. });
  564. }
  565. function viewForeword() {
  566. uni.showModal({
  567. title: '前言',
  568. content: currentBook.value?.metadata?.foreword || '',
  569. showCancel: true,
  570. cancelText: '关闭',
  571. confirmText: '复制',
  572. success: (res) => {
  573. if (res.confirm) {
  574. uni.setClipboardData({
  575. data: currentBook.value?.metadata?.foreword || '',
  576. success: () => uni.showToast({ title: '已复制', icon: 'success' }),
  577. });
  578. }
  579. }
  580. });
  581. }
  582. function viewAfterword() {
  583. uni.showModal({
  584. title: '后记',
  585. content: currentBook.value?.metadata?.afterword || '',
  586. showCancel: true,
  587. cancelText: '关闭',
  588. confirmText: '复制',
  589. success: (res) => {
  590. if (res.confirm) {
  591. uni.setClipboardData({
  592. data: currentBook.value?.metadata?.afterword || '',
  593. success: () => uni.showToast({ title: '已复制', icon: 'success' }),
  594. });
  595. }
  596. }
  597. });
  598. }
  599. onLoad((query: any) => {
  600. if (query?.id) {
  601. loadBook(query.id);
  602. }
  603. });
  604. onMounted(() => {
  605. // H5 环境:从 URL hash 获取参数
  606. // #ifdef H5
  607. if (typeof window !== 'undefined') {
  608. const hash = window.location.hash;
  609. const match = hash.match(/detail\?id=([^&]+)/);
  610. if (match && !currentBook.value) {
  611. loadBook(decodeURIComponent(match[1]));
  612. }
  613. }
  614. // #endif
  615. try {
  616. const token = uni.getStorageSync('token');
  617. if (token) {
  618. wsService.connect(token);
  619. wsService.on('book_generation_notification', (data: any) => {
  620. if (currentBook.value && data.bookId === currentBook.value.id.toString()) {
  621. if (data.message) {
  622. uni.showToast({ title: data.message, icon: 'none', duration: 3000 });
  623. }
  624. loadBook(currentBook.value.id);
  625. }
  626. });
  627. }
  628. } catch (error) {
  629. console.error('[WebSocket] 初始化失败:', error);
  630. }
  631. });
  632. onShow(() => {
  633. // onShow 时刷新书籍数据(H5 环境下 onMounted 时 hash 可能未更新)
  634. if (currentBook.value?.id) {
  635. loadBook(currentBook.value.id);
  636. }
  637. });
  638. onUnmounted(() => {
  639. stopPollingProgress();
  640. });
  641. </script>
  642. <style scoped>
  643. .page {
  644. min-height: 100vh;
  645. background: #f9fafb;
  646. }
  647. .nav-bar {
  648. position: fixed;
  649. top: 0;
  650. left: 0;
  651. right: 0;
  652. z-index: 100;
  653. background: #ffffff;
  654. box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  655. }
  656. .nav-content {
  657. display: flex;
  658. align-items: center;
  659. justify-content: space-between;
  660. height: 88rpx;
  661. padding: 0 32rpx;
  662. padding-top: env(safe-area-inset-top);
  663. }
  664. .nav-left, .nav-right { width: 80rpx; }
  665. .back-icon { font-size: 40rpx; color: #1f2937; }
  666. .page-title {
  667. font-size: 32rpx;
  668. font-weight: 600;
  669. color: #1f2937;
  670. max-width: 400rpx;
  671. overflow: hidden;
  672. text-overflow: ellipsis;
  673. white-space: nowrap;
  674. }
  675. .main-content {
  676. padding-top: calc(88rpx + env(safe-area-inset-top));
  677. padding: 120rpx 32rpx 32rpx;
  678. }
  679. .card {
  680. background: #ffffff;
  681. border-radius: 24rpx;
  682. padding: 32rpx;
  683. margin-bottom: 24rpx;
  684. box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
  685. }
  686. .card-header-row { display: flex; align-items: center; justify-content: space-between; margin-bottom: 24rpx; }
  687. .header-actions { display: flex; align-items: center; gap: 16rpx; }
  688. .edit-btn { padding: 12rpx 24rpx; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; border-radius: 8rpx; font-size: 28rpx; }
  689. .batch-actions-bar { display: flex; gap: 16rpx; padding: 24rpx; background: #f9fafb; border-radius: 16rpx; margin-bottom: 24rpx; flex-wrap: wrap; }
  690. .batch-btn { padding: 16rpx 32rpx; background: white; border: 2rpx solid #e5e7eb; border-radius: 12rpx; font-size: 28rpx; color: #374151; }
  691. .batch-btn.regenerate { background: linear-gradient(135deg, #10b981 0%, #059669 100%); color: white; border: none; }
  692. .batch-btn.regenerate.disabled { opacity: 0.5; }
  693. .batch-btn.delete { background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%); color: white; border: none; }
  694. .batch-btn.delete.disabled { opacity: 0.5; }
  695. .batch-btn.cancel { background: #f3f4f6; color: #6b7280; border: none; }
  696. .card-title { font-size: 32rpx; font-weight: 600; color: #1f2937; }
  697. .book-info { margin-bottom: 24rpx; }
  698. .book-title-row { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8rpx; }
  699. .book-title-large { font-size: 36rpx; font-weight: 700; color: #1f2937; flex: 1; }
  700. .book-subtitle { font-size: 28rpx; color: #6b7280; margin-bottom: 16rpx; display: block; }
  701. .book-meta-row { display: flex; gap: 24rpx; font-size: 26rpx; color: #9ca3af; margin-top: 16rpx; }
  702. .status-badge { font-size: 22rpx; padding: 6rpx 16rpx; border-radius: 20rpx; background: #e5e7eb; color: #6b7280; }
  703. .status-badge.planning, .status-badge.generating { background: #fef3c7; color: #d97706; }
  704. .status-badge.completed { background: #d1fae5; color: #059669; }
  705. .status-badge.failed { background: #fee2e2; color: #dc2626; }
  706. .progress-card { margin-bottom: 24rpx; }
  707. .progress-display { display: flex; align-items: baseline; gap: 16rpx; margin: 16rpx 0; }
  708. .progress-text { font-size: 48rpx; font-weight: 700; color: #4f46e5; }
  709. .progress-detail { font-size: 28rpx; color: #6b7280; }
  710. .progress-bar-large { height: 12rpx; background: #e5e7eb; border-radius: 6rpx; }
  711. .generation-status { margin-top: 24rpx; padding: 24rpx; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); border-radius: 16rpx; color: white; }
  712. .status-header { display: flex; align-items: center; margin-bottom: 16rpx; }
  713. .status-icon { font-size: 36rpx; margin-right: 12rpx; animation: pulse 2s ease-in-out infinite; }
  714. @keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.5; } }
  715. .status-title { font-size: 32rpx; font-weight: 600; }
  716. .status-stage { display: flex; align-items: center; padding: 16rpx; background: rgba(255, 255, 255, 0.2); border-radius: 12rpx; margin-bottom: 16rpx; }
  717. .stage-label { font-size: 26rpx; opacity: 0.9; margin-right: 8rpx; }
  718. .stage-value { font-size: 28rpx; font-weight: 600; }
  719. .status-error { display: flex; align-items: flex-start; padding: 16rpx; background: rgba(239, 68, 68, 0.3); border-radius: 12rpx; margin-bottom: 16rpx; }
  720. .error-icon { font-size: 28rpx; margin-right: 12rpx; flex-shrink: 0; }
  721. .error-text { font-size: 24rpx; line-height: 1.6; flex: 1; }
  722. .status-recovery { display: flex; align-items: center; padding: 16rpx; background: rgba(251, 191, 36, 0.3); border-radius: 12rpx; margin-bottom: 16rpx; }
  723. .recovery-icon { font-size: 32rpx; margin-right: 12rpx; animation: spin 1s linear infinite; }
  724. @keyframes spin { from { transform: rotate(0deg); } to { transform: rotate(360deg); } }
  725. .recovery-text { font-size: 26rpx; font-weight: 500; }
  726. .status-tip { padding: 12rpx; background: rgba(255, 255, 255, 0.1); border-radius: 8rpx; }
  727. .tip-text { font-size: 24rpx; opacity: 0.9; }
  728. .failed-tip { margin-top: 24rpx; padding: 24rpx; background: linear-gradient(135deg, #fee2e2 0%, #fecaca 100%); border-radius: 16rpx; border: 2rpx solid #fca5a5; }
  729. .failed-icon { font-size: 40rpx; display: block; text-align: center; margin-bottom: 12rpx; }
  730. .failed-text { font-size: 32rpx; font-weight: 600; color: #dc2626; display: block; text-align: center; margin-bottom: 12rpx; }
  731. .failed-error { font-size: 26rpx; color: #991b1b; display: block; padding: 16rpx; background: rgba(255, 255, 255, 0.5); border-radius: 8rpx; margin-bottom: 12rpx; line-height: 1.6; }
  732. .failed-hint { font-size: 24rpx; color: #6b7280; display: block; text-align: center; margin-bottom: 16rpx; }
  733. .retry-btn { width: 100%; height: 72rpx; background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%); color: white; border: none; border-radius: 12rpx; font-size: 28rpx; font-weight: 600; }
  734. .outline-theme { font-size: 24rpx; color: #6b7280; }
  735. .content-item { display: flex; align-items: center; justify-content: space-between; padding: 24rpx; background: #f9fafb; border-radius: 16rpx; margin-top: 16rpx; }
  736. .content-item.foreword, .content-item.afterword { background: linear-gradient(135deg, #fef3c7 0%, #fde68a 100%); cursor: pointer; }
  737. .content-item-left { display: flex; align-items: center; gap: 16rpx; }
  738. .content-label { font-size: 28rpx; font-weight: 600; color: #1f2937; }
  739. .content-word-count { font-size: 24rpx; color: #9ca3af; }
  740. .content-item-right { display: flex; align-items: center; }
  741. .expand-icon { font-size: 28rpx; color: #9ca3af; }
  742. .content-card { margin-top: 0; }
  743. .content-tree-list { margin-top: 20rpx; }
  744. .content-tree-item { margin-bottom: 12rpx; }
  745. .content-tree-row { display: flex; align-items: center; gap: 16rpx; padding: 16rpx; background: #f9fafb; border-radius: 12rpx; }
  746. .chapter-num-badge { width: 48rpx; height: 48rpx; background: #4f46e5; color: #ffffff; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 24rpx; font-weight: 600; flex-shrink: 0; }
  747. .chapter-info { flex: 1; }
  748. .chapter-title { font-size: 28rpx; font-weight: 600; color: #1f2937; display: block; margin-bottom: 6rpx; }
  749. .chapter-content-hint { margin-bottom: 6rpx; }
  750. .content-hint-tag { font-size: 22rpx; padding: 4rpx 12rpx; background: #e0f2fe; color: #0369a1; border-radius: 6rpx; }
  751. .chapter-outline-summary { font-size: 24rpx; color: #6b7280; display: block; margin-bottom: 8rpx; }
  752. .chapter-key-points { display: flex; flex-wrap: wrap; gap: 8rpx; margin-bottom: 8rpx; }
  753. .point-tag { font-size: 20rpx; padding: 4rpx 12rpx; background: #f3f4f6; color: #6b7280; border-radius: 6rpx; }
  754. .chapter-media-badges { display: flex; gap: 8rpx; }
  755. .media-badge { font-size: 20rpx; padding: 4rpx 12rpx; background: rgba(16, 185, 129, 0.1); color: #059669; border-radius: 6rpx; }
  756. .chapter-status { display: flex; flex-direction: column; align-items: center; justify-content: center; min-width: 80rpx; padding: 8rpx 16rpx; border-radius: 8rpx; flex-shrink: 0; }
  757. .chapter-status.status-completed { background: #d1fae5; color: #059669; }
  758. .chapter-status.status-pending { background: #e5e7eb; color: #6b7280; }
  759. .chapter-status.status-generating { background: #fef3c7; color: #d97706; animation: pulse-bg 2s ease-in-out infinite; }
  760. .chapter-status.status-failed { background: #fee2e2; color: #dc2626; }
  761. @keyframes pulse-bg { 0%, 100% { opacity: 1; } 50% { opacity: 0.7; } }
  762. .status-icon { font-size: 28rpx; font-weight: bold; line-height: 1; }
  763. .status-text { font-size: 20rpx; margin-top: 4rpx; white-space: nowrap; }
  764. .section-num-badge { width: 40rpx; height: 40rpx; background: #8b5cf6; color: #ffffff; border-radius: 8rpx; display: flex; align-items: center; justify-content: center; font-size: 22rpx; font-weight: 600; flex-shrink: 0; }
  765. .section-info { flex: 1; }
  766. .section-title { font-size: 26rpx; font-weight: 500; color: #374151; display: block; margin-bottom: 4rpx; }
  767. .section-summary { font-size: 22rpx; color: #9ca3af; display: block; }
  768. .section-status { display: flex; flex-direction: column; align-items: center; justify-content: center; min-width: 70rpx; padding: 6rpx 12rpx; border-radius: 6rpx; flex-shrink: 0; }
  769. .section-status.status-completed { background: #d1fae5; color: #059669; }
  770. .section-status.status-pending { background: #f3f4f6; color: #9ca3af; }
  771. .section-status.status-generating { background: #fef3c7; color: #d97706; }
  772. .section-status.status-failed { background: #fee2e2; color: #dc2626; }
  773. .subsection-num-badge { width: 36rpx; height: 36rpx; background: #06b6d4; color: #ffffff; border-radius: 6rpx; display: flex; align-items: center; justify-content: center; font-size: 20rpx; font-weight: 500; flex-shrink: 0; }
  774. .subsection-info { flex: 1; }
  775. .subsection-title { font-size: 24rpx; color: #6b7280; display: block; }
  776. .subsection-media-badges { display: flex; gap: 8rpx; margin-top: 4rpx; }
  777. .media-badge-small { font-size: 18rpx; padding: 2rpx 8rpx; background: rgba(16, 185, 129, 0.1); color: #059669; border-radius: 4rpx; }
  778. .subsection-content-preview { font-size: 22rpx; color: #6b7280; margin-top: 4rpx; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
  779. .subsection-status { display: flex; flex-direction: column; align-items: center; justify-content: center; min-width: 70rpx; padding: 6rpx 12rpx; border-radius: 6rpx; flex-shrink: 0; }
  780. .subsection-status.status-completed { background: #d1fae5; color: #059669; }
  781. .subsection-status.status-pending { background: #f3f4f6; color: #9ca3af; }
  782. .subsection-status.status-generating { background: #fef3c7; color: #d97706; }
  783. .subsection-status.status-failed { background: #fee2e2; color: #dc2626; }
  784. .action-card { margin-bottom: 24rpx; }
  785. .btn-col { margin-bottom: 20rpx; }
  786. .btn-row { display: flex; gap: 16rpx; margin-bottom: 20rpx; }
  787. .action-btn { flex: 1; height: 88rpx; background: #f3f4f6; border-radius: 16rpx; font-size: 28rpx; color: #374151; display: flex; align-items: center; justify-content: center; border: none; }
  788. .action-btn.primary { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: #ffffff; }
  789. .action-btn.full-width { width: 100%; flex: none; margin-bottom: 16rpx; }
  790. .action-btn[disabled] { opacity: 0.6; }
  791. .action-btn.langgraph-btn { background: linear-gradient(135deg, #10b981 0%, #059669 100%); color: #ffffff; }
  792. .generating-tip { text-align: center; padding: 20rpx; color: #6b7280; font-size: 26rpx; }
  793. .warning-tip { padding: 24rpx; background: #fef3c7; border-radius: 12rpx; margin-top: 16rpx; }
  794. .warning-icon { font-size: 32rpx; display: block; margin-bottom: 8rpx; }
  795. .warning-text { font-size: 26rpx; color: #92400e; display: block; margin-bottom: 8rpx; }
  796. .warning-hint { font-size: 24rpx; color: #b45309; display: block; }
  797. </style>