AnthologyReadController.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. namespace App\Http\Controllers\Library;
  3. use App\Http\Controllers\Controller;
  4. use App\Services\CollectionService;
  5. use App\Services\ArticleService;
  6. use Illuminate\Http\Request;
  7. class AnthologyReadController extends Controller
  8. {
  9. public function __construct(
  10. private CollectionService $collectionService,
  11. private ArticleService $articleService,
  12. ) {}
  13. // =========================================================================
  14. // read
  15. // GET /library/anthology/{anthology}/read/{article}
  16. // =========================================================================
  17. public function read(string $anthologyId, string $articleId)
  18. {
  19. // ── 1. 获取文集信息 ───────────────────────────────────────────────────
  20. $colResult = $this->collectionService->getCollection($anthologyId);
  21. if (isset($colResult['error'])) {
  22. abort($colResult['code'] ?? 404, $colResult['error']);
  23. }
  24. $col = is_array($colResult['data'])
  25. ? $colResult['data']
  26. : $colResult['data']->toArray(request());
  27. // ── 2. 构建目录(方案A:展开祖先链,其余折叠) ───────────────────────
  28. $fullArticleList = collect($col['article_list'] ?? []);
  29. $toc = $this->buildCollapsedToc($fullArticleList->toArray(), $articleId);
  30. // ── 3. 获取当前文章内容 ───────────────────────────────────────────────
  31. $artResult = $this->articleService->getArticle($articleId);
  32. if (isset($artResult['error'])) {
  33. abort($artResult['code'] ?? 404, $artResult['error']);
  34. }
  35. // ArticleResource 需要 format=html
  36. $fakeRequest = Request::create('', 'GET', ['format' => 'html']);
  37. $artResource = $artResult['data'];
  38. $artArray = $artResource->toArray($fakeRequest);
  39. // content 统一包装成 book.read 期望的格式:
  40. // [ ['id'=>..., 'level'=>1, 'text'=>[[ html_string ]]], ... ]
  41. $content = [[
  42. 'id' => $articleId,
  43. 'level' => 100,
  44. 'text' => [[$artArray['html'] ?? $artArray['content'] ?? 'null']],
  45. ]];
  46. // ── 4. 计算翻页(pagination) ─────────────────────────────────────────
  47. // 只在 level=1 的节点间翻页(与目录一致)
  48. $level1 = $fullArticleList
  49. ->filter(fn($a) => ($a['level'] ?? 1) === 1)
  50. ->values();
  51. $currentIndex = $level1->search(
  52. fn($a) => $a['article_id'] === $articleId
  53. );
  54. $prev = null;
  55. $next = null;
  56. if ($currentIndex !== false) {
  57. if ($currentIndex > 0) {
  58. $prevItem = $level1[$currentIndex - 1];
  59. $prev = [
  60. 'id' => $prevItem['article_id'],
  61. 'title' => $prevItem['title'],
  62. ];
  63. }
  64. if ($currentIndex < $level1->count() - 1) {
  65. $nextItem = $level1[$currentIndex + 1];
  66. $next = [
  67. 'id' => $nextItem['article_id'],
  68. 'title' => $nextItem['title'],
  69. ];
  70. }
  71. }
  72. $pagination = [
  73. 'start' => 0,
  74. 'end' => 0,
  75. 'prev' => $prev,
  76. 'next' => $next,
  77. ];
  78. // ── 5. 组装 $book(对齐 book.read 的数据结构) ───────────────────────
  79. $studio = $col['studio'] ?? [];
  80. // blade 用 $book['publisher']->username / ->nickname,必须是对象
  81. $publisher = (object) [
  82. 'username' => $studio['studioName'] ?? '',
  83. 'nickname' => $studio['nickName'] ?? $studio['studioName'] ?? '',
  84. ];
  85. $book = [
  86. 'id' => $articleId,
  87. 'title' => $artArray['title'] ?? ($col['title'] ?? ''),
  88. 'author' => $studio['nickName'] ?? $studio['studioName'] ?? '',
  89. 'publisher' => $publisher,
  90. 'type' => '',
  91. 'category_id' => null,
  92. 'cover' => null,
  93. 'description' => $col['summary'] ?? '',
  94. 'language' => $col['lang'] ?? '',
  95. 'anthology' => [
  96. 'id' => $anthologyId,
  97. 'title' => $col['title'] ?? '',
  98. ],
  99. 'categories' => [],
  100. 'tags' => [],
  101. 'downloads' => [],
  102. 'toc' => $toc,
  103. 'pagination' => $pagination,
  104. 'content' => $content,
  105. ];
  106. // blade 里有 $relatedBooks,传空数组防止 undefined variable
  107. $relatedBooks = [];
  108. // 翻页路由需要 anthologyId,传给 blade 供覆盖路由使用
  109. return view('library.book.read', compact('book', 'relatedBooks', 'anthologyId'));
  110. }
  111. // =========================================================================
  112. // buildCollapsedToc
  113. //
  114. // 规则:
  115. // 1. 所有 level=1 节点始终显示
  116. // 2. 找出当前节点的「祖先链」(从 root 到当前节点经过的每个节点)
  117. // 3. 祖先链上每个节点的「直接子节点」都展开显示
  118. // 4. 当前节点本身若有子节点,同样展开一级
  119. // 5. 其余节点隐藏
  120. // =========================================================================
  121. private function buildCollapsedToc(array $list, string $currentId): array
  122. {
  123. // ── Step 1:为每个节点推导父节点 id ──────────────────────────────────
  124. // article_list 是有序的,父节点 = 当前节点之前最近的 level 更小的节点
  125. $parents = []; // article_id => parent_article_id | null
  126. $stack = []; // 维护祖先栈 [ ['id'=>..., 'level'=>...], ... ]
  127. foreach ($list as $item) {
  128. $id = $item['article_id'];
  129. $level = (int) ($item['level'] ?? 1);
  130. // 弹出所有 level >= 当前 level 的栈顶
  131. while (!empty($stack) && $stack[count($stack) - 1]['level'] >= $level) {
  132. array_pop($stack);
  133. }
  134. $parents[$id] = empty($stack) ? null : $stack[count($stack) - 1]['id'];
  135. $stack[] = ['id' => $id, 'level' => $level];
  136. }
  137. // ── Step 2:找出当前节点的祖先链 ─────────────────────────────────────
  138. $ancestorIds = [];
  139. $cursor = $currentId;
  140. while (isset($parents[$cursor]) && $parents[$cursor] !== null) {
  141. $cursor = $parents[$cursor];
  142. $ancestorIds[] = $cursor;
  143. }
  144. $ancestorSet = array_flip($ancestorIds);
  145. // ── Step 3:需要展开子节点的集合 = 祖先链 + 当前节点 ─────────────────
  146. $expandParentSet = $ancestorSet;
  147. $expandParentSet[$currentId] = true;
  148. // ── Step 4:过滤构建 toc ──────────────────────────────────────────────
  149. $toc = [];
  150. foreach ($list as $item) {
  151. $id = $item['article_id'];
  152. $level = (int) ($item['level'] ?? 1);
  153. $parentId = $parents[$id];
  154. $isActive = $id === $currentId;
  155. // 显示条件:
  156. // a) level=1(始终显示)
  157. // b) 父节点在 expandParentSet 中(祖先链或当前节点的直接子节点)
  158. $visible = $level === 1
  159. || ($parentId !== null && isset($expandParentSet[$parentId]));
  160. if (!$visible) {
  161. continue;
  162. }
  163. $toc[] = [
  164. 'id' => $id,
  165. 'title' => $item['title'],
  166. 'summary' => '',
  167. 'progress' => 0,
  168. 'level' => $level,
  169. 'disabled' => $isActive,
  170. 'active' => $isActive,
  171. ];
  172. }
  173. return $toc;
  174. }
  175. }