HomeController.php 5.8 KB

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