CategoryController.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\File;
  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. // 封面渐变色池: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. // 从 uid / id 字符串中提取一个稳定的整数,用于色池取余
  23. // -------------------------------------------------------------------------
  24. private function colorIndex(string $uid): int
  25. {
  26. return hexdec(substr(str_replace('-', '', $uid), 0, 4)) % 255;
  27. }
  28. protected static int $nextId = 1;
  29. public function home()
  30. {
  31. $categories = $this->loadCategories();
  32. // 获取一级分类和对应的书籍
  33. $categoryData = [];
  34. foreach ($categories as $category) {
  35. if ($category['level'] == 1) {
  36. $children = $this->subCategories($categories, $category['id']);
  37. $categoryData[] = [
  38. 'category' => $category,
  39. 'children' => $children,
  40. ];
  41. }
  42. }
  43. $update = $this->getUpdateBooks();
  44. return view('library.index', compact('categoryData', 'categories', 'update'));
  45. }
  46. public function index()
  47. {
  48. $categories = $this->loadCategories();
  49. // 获取一级分类和对应的书籍
  50. $categoryData = [];
  51. foreach ($categories as $category) {
  52. if ($category['level'] == 1) {
  53. $children = $this->subCategories($categories, $category['id']);
  54. $categoryData[] = [
  55. 'category' => $category,
  56. 'children' => $children,
  57. ];
  58. }
  59. }
  60. return view('library.index', compact('categoryData', 'categories'));
  61. }
  62. public function category(int $id)
  63. {
  64. $categories = $this->loadCategories();
  65. $currentCategory = collect($categories)->firstWhere('id', $id);
  66. if (!$currentCategory) {
  67. abort(404);
  68. }
  69. // 获取子分类
  70. $subCategories = array_filter($categories, function ($cat) use ($id) {
  71. return $cat['parent_id'] == $id;
  72. });
  73. // 获取该分类下的书籍
  74. $categoryBooks = $this->getBooks($categories, $id);
  75. // 获取面包屑
  76. $breadcrumbs = $this->getBreadcrumbs($currentCategory, $categories);
  77. return view('library.tipitaka.category', compact('currentCategory', 'subCategories', 'categoryBooks', 'breadcrumbs'));
  78. }
  79. private function subCategories($categories, int $id)
  80. {
  81. return array_filter($categories, function ($cat) use ($id) {
  82. return $cat['parent_id'] == $id;
  83. });
  84. }
  85. private function getUpdateBooks()
  86. {
  87. $books = ProgressChapter::with('channel.owner')
  88. ->leftJoin('pali_texts', function ($join) {
  89. $join->on('progress_chapters.book', '=', 'pali_texts.book')
  90. ->on('progress_chapters.para', '=', 'pali_texts.paragraph');
  91. })
  92. ->whereHas('channel', function ($query) {
  93. $query->where('status', 30);
  94. })
  95. ->where('progress', '>', config('mint.library.list_min_progress'))
  96. ->take(10)
  97. ->get();
  98. return $this->getBooksInfo($books);
  99. }
  100. private function getBooks($categories, $id)
  101. {
  102. $currentCategory = collect($categories)->firstWhere('id', $id);
  103. if (!$currentCategory) {
  104. abort(404);
  105. }
  106. // 标签查章节
  107. $tagNames = $currentCategory['tag'];
  108. $tm = (new TagMap)->getTable();
  109. $tg = (new Tag)->getTable();
  110. $pt = (new PaliText)->getTable();
  111. $where1 = " where co = " . count($tagNames);
  112. $a = implode(",", array_fill(0, count($tagNames), '?'));
  113. $in1 = "and t.name in ({$a})";
  114. $param = $tagNames;
  115. $where2 = "where level = 1";
  116. $query = "
  117. select uid as id,book,paragraph,level,toc as title,chapter_strlen,parent,path from (
  118. select anchor_id as cid from (
  119. select tm.anchor_id , count(*) as co
  120. from $tm as tm
  121. left join $tg as t on tm.tag_id = t.id
  122. where tm.table_name = 'pali_texts'
  123. $in1
  124. group by tm.anchor_id
  125. ) T
  126. $where1
  127. ) CID
  128. left join $pt as pt on CID.cid = pt.uid
  129. $where2
  130. order by book,paragraph";
  131. $chapters = DB::select($query, $param);
  132. $chaptersParam = [];
  133. foreach ($chapters as $key => $chapter) {
  134. $chaptersParam[] = [$chapter->book, $chapter->paragraph];
  135. }
  136. // 获取该分类下的章节
  137. $books = ProgressChapter::with('channel.owner')
  138. ->whereIns(['progress_chapters.book', 'progress_chapters.para'], $chaptersParam)
  139. ->whereHas('channel', function ($query) {
  140. $query->where('status', 30);
  141. })
  142. ->where('progress', '>', config('mint.library.list_min_progress'))
  143. ->get();
  144. return $this->getBooksInfo($books);
  145. }
  146. private function getBooksInfo($books,)
  147. {
  148. $pali = PaliText::where('level', 1)->get();
  149. // 获取该分类下的书籍
  150. $categoryBooks = [];
  151. $books->each(function ($book) use (&$categoryBooks, $pali) {
  152. $title = $book->title;
  153. if (empty($title)) {
  154. $title = $pali->firstWhere('book', $book->book)->toc;
  155. }
  156. //Log::debug('getBooksInfo', ['book' => $book->book, 'paragraph' => $book->para]);
  157. $pcd_book_id = $pali->first(function ($item) use ($book) {
  158. return $item->book == $book->book
  159. && $item->paragraph == $book->para;
  160. })?->pcd_book_id;
  161. $coverFile = "/assets/images/cover/zh-hans/1/{$pcd_book_id}.png";
  162. if (File::exists(public_path($coverFile))) {
  163. $coverUrl = $coverFile;
  164. } else {
  165. $coverUrl = null;
  166. }
  167. $colorIdx = $this->colorIndex($book->uid);
  168. $categoryBooks[] = [
  169. "id" => $book->uid,
  170. "title" => $title,
  171. "author" => $book->channel->name,
  172. "publisher" => $book->channel->owner,
  173. "type" => __('labels.' . $book->channel->type),
  174. "cover" => $coverUrl,
  175. 'cover_gradient' => $this->coverGradients[$colorIdx % count($this->coverGradients)],
  176. "description" => $book->summary ?? "比库戒律的详细说明",
  177. "language" => __('language.' . $book->channel->lang),
  178. ];
  179. });
  180. return $categoryBooks;
  181. }
  182. private function loadCategories()
  183. {
  184. $json = file_get_contents(public_path("data/category/default.json"));
  185. $tree = json_decode($json, true);
  186. $flat = self::flattenWithIds($tree);
  187. return $flat;
  188. }
  189. public static function flattenWithIds(array $tree, int $parentId = 0, int $level = 1): array
  190. {
  191. $flat = [];
  192. foreach ($tree as $node) {
  193. $currentId = self::$nextId++;
  194. $item = [
  195. 'id' => $currentId,
  196. 'parent_id' => $parentId,
  197. 'name' => $node['name'] ?? null,
  198. 'tag' => $node['tag'] ?? [],
  199. "description" => "佛教戒律经典",
  200. 'level' => $level,
  201. ];
  202. $flat[] = $item;
  203. if (isset($node['children']) && is_array($node['children'])) {
  204. $childrenLevel = $level + 1;
  205. $flat = array_merge($flat, self::flattenWithIds($node['children'], $currentId, $childrenLevel));
  206. }
  207. }
  208. return $flat;
  209. }
  210. private function getBreadcrumbs($category, $categories)
  211. {
  212. $breadcrumbs = [];
  213. $current = $category;
  214. while ($current) {
  215. array_unshift($breadcrumbs, $current);
  216. $current = collect($categories)->firstWhere('id', $current['parent_id']);
  217. }
  218. return $breadcrumbs;
  219. }
  220. }