2
0

AnthologyReadController.php 8.7 KB

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