AnthologyReadController.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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(Request $request, 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. $urlParam = [
  37. 'mode' => 'read',
  38. 'format' => 'html',
  39. 'anthology' => $anthologyId,
  40. 'channel' => $request->input('channel', null),
  41. 'origin' => 'true',
  42. 'paragraph' => true,
  43. ];
  44. $fakeRequest = Request::create('', 'GET', $urlParam);
  45. $artResource = $artResult['data'];
  46. $artArray = $artResource->toArray($fakeRequest);
  47. // content 统一包装成 book.read 期望的格式:
  48. // [ ['id'=>..., 'level'=>1, 'text'=>[[ html_string ]]], ... ]
  49. $content = [[
  50. 'id' => $articleId,
  51. 'level' => 100,
  52. 'text' => [[$artArray['html'] ?? $artArray['content'] ?? 'null']],
  53. ]];
  54. // ── 4. 计算翻页(pagination) ─────────────────────────────────────────
  55. $currentIndex = $fullArticleList->search(
  56. fn($a) => $a['article_id'] === $articleId
  57. );
  58. $prev = null;
  59. $next = null;
  60. if ($currentIndex !== false) {
  61. if ($currentIndex > 0) {
  62. $prevItem = $fullArticleList[$currentIndex - 1];
  63. $prev = [
  64. 'id' => $prevItem['article_id'],
  65. 'title' => $prevItem['title'],
  66. ];
  67. }
  68. if ($currentIndex < $fullArticleList->count() - 1) {
  69. $nextItem = $fullArticleList[$currentIndex + 1];
  70. $next = [
  71. 'id' => $nextItem['article_id'],
  72. 'title' => $nextItem['title'],
  73. ];
  74. }
  75. }
  76. $pagination = [
  77. 'start' => 0,
  78. 'end' => 0,
  79. 'prev' => $prev,
  80. 'next' => $next,
  81. ];
  82. // ── 5. 组装 $book(对齐 book.read 的数据结构) ───────────────────────
  83. $studio = $col['studio'] ?? [];
  84. // blade 用 $book['publisher']->username / ->nickname,必须是对象
  85. $publisher = (object) [
  86. 'username' => $studio['studioName'] ?? '',
  87. 'nickname' => $studio['nickName'] ?? $studio['studioName'] ?? '',
  88. ];
  89. $book = [
  90. 'id' => $articleId,
  91. 'title' => $artArray['title'] ?? ($col['title'] ?? ''),
  92. 'author' => $studio['nickName'] ?? $studio['studioName'] ?? '',
  93. 'publisher' => $publisher,
  94. 'type' => '',
  95. 'category_id' => null,
  96. 'cover' => null,
  97. 'description' => $col['summary'] ?? '',
  98. 'language' => $col['lang'] ?? '',
  99. 'anthology' => [
  100. 'id' => $anthologyId,
  101. 'title' => $col['title'] ?? '',
  102. ],
  103. 'categories' => [],
  104. 'tags' => [],
  105. 'downloads' => [],
  106. 'toc' => $toc,
  107. 'pagination' => $pagination,
  108. 'content' => $content,
  109. ];
  110. $channels = $this->articleService->articleChannels($articleId);
  111. // blade 里有 $relatedBooks,传空数组防止 undefined variable
  112. $relatedBooks = [];
  113. $editor_link = config('mint.server.dashboard_base_path') . "/workspace/anthology/{$anthologyId}/{$book['id']}";
  114. // 翻页路由需要 anthologyId,传给 blade 供覆盖路由使用
  115. return view('library.book.read', compact('book', 'relatedBooks', 'anthologyId', 'channels', 'editor_link'));
  116. }
  117. // =========================================================================
  118. // buildCollapsedToc
  119. //
  120. // 规则:
  121. // 1. 所有 level=1 节点始终显示
  122. // 2. 找出当前节点的「祖先链」(从 root 到当前节点经过的每个节点)
  123. // 3. 祖先链上每个节点的「直接子节点」都展开显示
  124. // 4. 当前节点本身若有子节点,同样展开一级
  125. // 5. 其余节点隐藏
  126. // =========================================================================
  127. private function buildCollapsedToc(array $list, string $currentId): array
  128. {
  129. // ── Step 1:为每个节点推导父节点 id ──────────────────────────────────
  130. // article_list 是有序的,父节点 = 当前节点之前最近的 level 更小的节点
  131. $parents = []; // article_id => parent_article_id | null
  132. $stack = []; // 维护祖先栈 [ ['id'=>..., 'level'=>...], ... ]
  133. foreach ($list as $item) {
  134. $id = $item['article_id'];
  135. $level = (int) ($item['level'] ?? 1);
  136. // 弹出所有 level >= 当前 level 的栈顶
  137. while (!empty($stack) && $stack[count($stack) - 1]['level'] >= $level) {
  138. array_pop($stack);
  139. }
  140. $parents[$id] = empty($stack) ? null : $stack[count($stack) - 1]['id'];
  141. $stack[] = ['id' => $id, 'level' => $level];
  142. }
  143. // ── Step 2:找出当前节点的祖先链 ─────────────────────────────────────
  144. $ancestorIds = [];
  145. $cursor = $currentId;
  146. while (isset($parents[$cursor]) && $parents[$cursor] !== null) {
  147. $cursor = $parents[$cursor];
  148. $ancestorIds[] = $cursor;
  149. }
  150. $ancestorSet = array_flip($ancestorIds);
  151. // ── Step 3:需要展开子节点的集合 = 祖先链 + 当前节点 ─────────────────
  152. $expandParentSet = $ancestorSet;
  153. $expandParentSet[$currentId] = true;
  154. // ── Step 4:过滤构建 toc ──────────────────────────────────────────────
  155. $toc = [];
  156. foreach ($list as $item) {
  157. $id = $item['article_id'];
  158. $level = (int) ($item['level'] ?? 1);
  159. $parentId = $parents[$id];
  160. $isActive = $id === $currentId;
  161. // 显示条件:
  162. // a) level=1(始终显示)
  163. // b) 父节点在 expandParentSet 中(祖先链或当前节点的直接子节点)
  164. $visible = $level === 1
  165. || ($parentId !== null && isset($expandParentSet[$parentId]));
  166. if (!$visible) {
  167. continue;
  168. }
  169. $toc[] = [
  170. 'id' => $id,
  171. 'title' => $item['title'],
  172. 'summary' => '',
  173. 'progress' => 0,
  174. 'level' => $level,
  175. 'disabled' => $isActive,
  176. 'active' => $isActive,
  177. ];
  178. }
  179. return $toc;
  180. }
  181. }