CategoryController.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\Storage;
  5. class CategoryController extends Controller
  6. {
  7. protected static int $nextId = 1;
  8. public function index()
  9. {
  10. $categories = $this->loadCategories();
  11. $books = $this->loadBooks();
  12. // 获取一级分类和对应的书籍
  13. $categoryData = [];
  14. foreach ($categories as $category) {
  15. if ($category['level'] == 1) {
  16. $categoryBooks = array_filter($books, function ($book) use ($category) {
  17. return $book['category_id'] == $category['id'];
  18. });
  19. $categoryData[] = [
  20. 'category' => $category,
  21. 'books' => array_slice(array_values($categoryBooks), 0, 3)
  22. ];
  23. }
  24. }
  25. return view('library.index', compact('categoryData', 'categories'));
  26. }
  27. public function show($id)
  28. {
  29. $categories = $this->loadCategories();
  30. $books = $this->loadBooks();
  31. $currentCategory = collect($categories)->firstWhere('id', $id);
  32. if (!$currentCategory) {
  33. abort(404);
  34. }
  35. // 获取子分类
  36. $subCategories = array_filter($categories, function ($cat) use ($id) {
  37. return $cat['parent_id'] == $id;
  38. });
  39. // 获取该分类下的书籍
  40. $categoryBooks = array_filter($books, function ($book) use ($id) {
  41. return $book['category_id'] == $id;
  42. });
  43. // 获取面包屑
  44. $breadcrumbs = $this->getBreadcrumbs($currentCategory, $categories);
  45. return view('library.category', compact('currentCategory', 'subCategories', 'categoryBooks', 'breadcrumbs'));
  46. }
  47. private function loadCategories()
  48. {
  49. //$json = Storage::disk('public')->get('data/categories.json');
  50. $json = file_get_contents(public_path("app/palicanon/category/default.json"));
  51. $tree = json_decode($json, true);
  52. $flat = self::flattenWithIds($tree);
  53. return $flat;
  54. }
  55. private function loadBooks()
  56. {
  57. $json = Storage::disk('public')->get('data/books.json');
  58. return json_decode($json, true);
  59. }
  60. public static function flattenWithIds(array $tree, int $parentId = 0, int $level = 1): array
  61. {
  62. $flat = [];
  63. foreach ($tree as $node) {
  64. $currentId = self::$nextId++;
  65. $item = [
  66. 'id' => $currentId,
  67. 'parent_id' => $parentId,
  68. 'name' => $node['name'] ?? null,
  69. 'tag' => $node['tag'] ?? [],
  70. "description" => "佛教戒律经典",
  71. 'level' => $level,
  72. ];
  73. $flat[] = $item;
  74. if (isset($node['children']) && is_array($node['children'])) {
  75. $childrenLevel = $level + 1;
  76. $flat = array_merge($flat, self::flattenWithIds($node['children'], $currentId, $childrenLevel));
  77. }
  78. }
  79. return $flat;
  80. }
  81. private function getBreadcrumbs($category, $categories)
  82. {
  83. $breadcrumbs = [];
  84. $current = $category;
  85. while ($current) {
  86. array_unshift($breadcrumbs, $current);
  87. $current = collect($categories)->firstWhere('id', $current['parent_id']);
  88. }
  89. return $breadcrumbs;
  90. }
  91. }