CategoryController.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\Storage;
  5. use Illuminate\Support\Facades\DB;
  6. use App\Models\PaliText;
  7. use App\Models\ProgressChapter;
  8. use App\Models\Tag;
  9. use App\Models\TagMap;
  10. class CategoryController extends Controller
  11. {
  12. protected static int $nextId = 1;
  13. public function index()
  14. {
  15. $categories = $this->loadCategories();
  16. $books = $this->loadBooks();
  17. // 获取一级分类和对应的书籍
  18. $categoryData = [];
  19. foreach ($categories as $category) {
  20. if ($category['level'] == 1) {
  21. $categoryBooks = $this->getBooks($categories, $category['id']);
  22. $categoryData[] = [
  23. 'category' => $category,
  24. 'books' => array_slice(array_values($categoryBooks), 0, 3)
  25. ];
  26. }
  27. }
  28. return view('library.index', compact('categoryData', 'categories'));
  29. }
  30. public function show($id)
  31. {
  32. $categories = $this->loadCategories();
  33. $books = $this->loadBooks();
  34. $currentCategory = collect($categories)->firstWhere('id', $id);
  35. if (!$currentCategory) {
  36. abort(404);
  37. }
  38. // 获取子分类
  39. $subCategories = array_filter($categories, function ($cat) use ($id) {
  40. return $cat['parent_id'] == $id;
  41. });
  42. // 获取该分类下的书籍
  43. $categoryBooks = $this->getBooks($categories, $id);
  44. // 获取面包屑
  45. $breadcrumbs = $this->getBreadcrumbs($currentCategory, $categories);
  46. return view('library.category', compact('currentCategory', 'subCategories', 'categoryBooks', 'breadcrumbs'));
  47. }
  48. private function getBooks($categories, $id)
  49. {
  50. $currentCategory = collect($categories)->firstWhere('id', $id);
  51. if (!$currentCategory) {
  52. abort(404);
  53. }
  54. // 标签查章节
  55. $tagNames = $currentCategory['tag'];
  56. $tm = (new TagMap)->getTable();
  57. $tg = (new Tag)->getTable();
  58. $pt = (new PaliText)->getTable();
  59. $where1 = " where co = " . count($tagNames);
  60. $a = implode(",", array_fill(0, count($tagNames), '?'));
  61. $in1 = "and t.name in ({$a})";
  62. $param = $tagNames;
  63. $where2 = "where level = 1";
  64. $query = "
  65. select uid as id,book,paragraph,level,toc as title,chapter_strlen,parent,path from (
  66. select anchor_id as cid from (
  67. select tm.anchor_id , count(*) as co
  68. from $tm as tm
  69. left join $tg as t on tm.tag_id = t.id
  70. where tm.table_name = 'pali_texts'
  71. $in1
  72. group by tm.anchor_id
  73. ) T
  74. $where1
  75. ) CID
  76. left join $pt as pt on CID.cid = pt.uid
  77. $where2
  78. order by book,paragraph";
  79. $chapters = DB::select($query, $param);
  80. $chaptersParam = [];
  81. foreach ($chapters as $key => $chapter) {
  82. $chaptersParam[] = [$chapter->book, $chapter->paragraph];
  83. }
  84. // 获取该分类下的章节
  85. $books = ProgressChapter::with('channel.owner')->whereIns(['book', 'para'], $chaptersParam)
  86. ->whereHas('channel', function ($query) {
  87. $query->where('status', 30);
  88. })
  89. ->where('progress', '>', 0.2)
  90. ->get();
  91. // 获取该分类下的书籍
  92. $categoryBooks = [];
  93. $books->each(function ($book) use (&$categoryBooks, $id) {
  94. $categoryBooks[] = [
  95. "id" => $book->uid,
  96. "title" => $book->title . "(" . $book->book . "-" . $book->para . ")",
  97. "author" => $book->channel->name,
  98. "publisher" => $book->channel->owner->nickname,
  99. "type" => __('label.' . $book->channel->type),
  100. "category_id" => $id,
  101. "cover" => "/assets/images/cover/1/214.jpg",
  102. "description" => $book->summary ?? "比库戒律的详细说明",
  103. "language" => __('language.' . $book->channel->lang),
  104. "contents" => [
  105. [
  106. "title" => "比库戒本",
  107. "content" => "诸恶莫作,众善奉行,自净其意,是诸佛教...",
  108. "summary" => "基本戒律",
  109. ]
  110. ],
  111. ];
  112. });
  113. return $categoryBooks;
  114. }
  115. private function loadCategories()
  116. {
  117. $json = file_get_contents(public_path("app/palicanon/category/default.json"));
  118. $tree = json_decode($json, true);
  119. $flat = self::flattenWithIds($tree);
  120. return $flat;
  121. }
  122. private function loadBooks()
  123. {
  124. $json = Storage::disk('public')->get('data/books.json');
  125. return json_decode($json, true);
  126. }
  127. public static function flattenWithIds(array $tree, int $parentId = 0, int $level = 1): array
  128. {
  129. $flat = [];
  130. foreach ($tree as $node) {
  131. $currentId = self::$nextId++;
  132. $item = [
  133. 'id' => $currentId,
  134. 'parent_id' => $parentId,
  135. 'name' => $node['name'] ?? null,
  136. 'tag' => $node['tag'] ?? [],
  137. "description" => "佛教戒律经典",
  138. 'level' => $level,
  139. ];
  140. $flat[] = $item;
  141. if (isset($node['children']) && is_array($node['children'])) {
  142. $childrenLevel = $level + 1;
  143. $flat = array_merge($flat, self::flattenWithIds($node['children'], $currentId, $childrenLevel));
  144. }
  145. }
  146. return $flat;
  147. }
  148. private function getBreadcrumbs($category, $categories)
  149. {
  150. $breadcrumbs = [];
  151. $current = $category;
  152. while ($current) {
  153. array_unshift($breadcrumbs, $current);
  154. $current = collect($categories)->firstWhere('id', $current['parent_id']);
  155. }
  156. return $breadcrumbs;
  157. }
  158. }