2
0

HomeController.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace App\Http\Controllers\Library;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\PaliText;
  5. use App\Models\ProgressChapter;
  6. use App\Services\TermService;
  7. use Illuminate\Support\Facades\Cookie;
  8. use Illuminate\Support\Facades\File;
  9. class HomeController extends Controller
  10. {
  11. // 封面渐变色池:uid 首字节取余循环,保证同一文集颜色稳定
  12. private array $coverGradients = [
  13. 'linear-gradient(160deg, #2d1020, #ae6b8b)',
  14. 'linear-gradient(160deg, #1a2d10,rgba(75, 114, 36, 0.61))',
  15. 'linear-gradient(160deg, #0d1f3c,rgb(55, 98, 150))',
  16. 'linear-gradient(160deg, #2d1020,rgb(151, 69, 94))',
  17. 'linear-gradient(160deg, #1a1a2d,rgb(76, 68, 146))',
  18. 'linear-gradient(160deg, #1a2820,rgb(55, 124, 99))',
  19. ];
  20. /**
  21. * 构造函数,注入 TermService
  22. */
  23. public function __construct(
  24. protected TermService $termService,
  25. ) {}
  26. protected static int $nextId = 1;
  27. public function index()
  28. {
  29. $categories = $this->loadCategories();
  30. $locale = Cookie::get('language') ?? 'en';
  31. // 获取一级分类和对应的书籍
  32. $categoryData = [];
  33. foreach ($categories as $category) {
  34. if ($category['level'] == 1) {
  35. $children = $this->subCategories($categories, $category['id']);
  36. $categoryData[] = [
  37. 'category' => $category,
  38. 'children' => $children,
  39. ];
  40. }
  41. }
  42. $categoryData = $this->termService->attachLocalName($categoryData, $locale);
  43. $recentBooks = $this->getUpdateBooks();
  44. return view('library.index', compact(
  45. 'categoryData',
  46. 'categories',
  47. 'recentBooks'
  48. ));
  49. }
  50. // -------------------------------------------------------------------------
  51. // 从 uid / id 字符串中提取一个稳定的整数,用于色池取余
  52. // -------------------------------------------------------------------------
  53. private function colorIndex(string $uid): int
  54. {
  55. return hexdec(substr(str_replace('-', '', $uid), 0, 4)) % 255;
  56. }
  57. private function subCategories($categories, int $id)
  58. {
  59. return array_filter($categories, function ($cat) use ($id) {
  60. return $cat['parent_id'] == $id;
  61. });
  62. }
  63. private function getUpdateBooks()
  64. {
  65. $books = ProgressChapter::with('channel.owner')
  66. ->leftJoin('pali_texts', function ($join) {
  67. $join->on('progress_chapters.book', '=', 'pali_texts.book')
  68. ->on('progress_chapters.para', '=', 'pali_texts.paragraph');
  69. })
  70. ->whereHas('channel', function ($query) {
  71. $query->where('status', 30);
  72. })
  73. ->where('progress', '>', config('mint.library.list_min_progress'))
  74. ->take(10)
  75. ->get();
  76. return $this->getBooksInfo($books);
  77. }
  78. private function getBooksInfo($books)
  79. {
  80. $pali = PaliText::where('level', 1)->get();
  81. // 获取该分类下的书籍
  82. $categoryBooks = [];
  83. $books->each(function ($book) use (&$categoryBooks, $pali) {
  84. $title = $book->title;
  85. if (empty($title)) {
  86. $title = $pali->firstWhere('book', $book->book)->toc;
  87. }
  88. // Log::debug('getBooksInfo', ['book' => $book->book, 'paragraph' => $book->para]);
  89. $pcd_book_id = $pali->first(function ($item) use ($book) {
  90. return $item->book == $book->book
  91. && $item->paragraph == $book->para;
  92. })?->pcd_book_id;
  93. $coverFile = "/assets/images/cover/zh-hans/1/{$pcd_book_id}.png";
  94. if (File::exists(public_path($coverFile))) {
  95. $coverUrl = $coverFile;
  96. } else {
  97. $coverUrl = null;
  98. }
  99. $colorIdx = $this->colorIndex($book->uid);
  100. $categoryBooks[] = [
  101. 'id' => $book->uid,
  102. 'title' => $title,
  103. 'author' => $book->channel->name,
  104. 'publisher' => $book->channel->owner,
  105. 'type' => __('labels.'.$book->channel->type),
  106. 'cover' => $coverUrl,
  107. 'cover_gradient' => $this->coverGradients[$colorIdx % count($this->coverGradients)],
  108. 'description' => $book->summary ?? '比库戒律的详细说明',
  109. 'language' => __('language.'.$book->channel->lang),
  110. 'updated_at' => $book->updated_at,
  111. 'is_new' => false, // FIXME
  112. 'category' => '经藏', // FIXME
  113. ];
  114. });
  115. return $categoryBooks;
  116. }
  117. private function loadCategories()
  118. {
  119. $json = file_get_contents(public_path('data/category/default.json'));
  120. $tree = json_decode($json, true);
  121. $flat = self::flattenWithIds($tree);
  122. return $flat;
  123. }
  124. public static function flattenWithIds(array $tree, int $parentId = 0, int $level = 1): array
  125. {
  126. $flat = [];
  127. foreach ($tree as $node) {
  128. $currentId = self::$nextId++;
  129. $item = [
  130. 'id' => $currentId,
  131. 'parent_id' => $parentId,
  132. 'name' => $node['name'] ?? null,
  133. 'tag' => $node['tag'] ?? [],
  134. 'description' => '佛教戒律经典',
  135. 'level' => $level,
  136. ];
  137. $flat[] = $item;
  138. if (isset($node['children']) && is_array($node['children'])) {
  139. $childrenLevel = $level + 1;
  140. $flat = array_merge($flat, self::flattenWithIds($node['children'], $currentId, $childrenLevel));
  141. }
  142. }
  143. return $flat;
  144. }
  145. }