2
0

HomeController.php 5.8 KB

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