CategoryController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. $recentBooks = $this->getRecent();
  44. return view('library.index', compact(
  45. 'categoryData',
  46. 'categories',
  47. 'recentBooks'
  48. ));
  49. }
  50. public function index()
  51. {
  52. $categories = $this->loadCategories();
  53. // 获取一级分类和对应的书籍
  54. $categoryData = [];
  55. foreach ($categories as $category) {
  56. if ($category['level'] == 1) {
  57. $children = $this->subCategories($categories, $category['id']);
  58. $categoryData[] = [
  59. 'category' => $category,
  60. 'children' => $children,
  61. ];
  62. }
  63. }
  64. return view('library.index', compact('categoryData', 'categories'));
  65. }
  66. public function category(int $id)
  67. {
  68. $categories = $this->loadCategories();
  69. $currentCategory = collect($categories)->firstWhere('id', $id);
  70. if (!$currentCategory) {
  71. abort(404);
  72. }
  73. // 获取子分类
  74. $subCategories = array_filter($categories, function ($cat) use ($id) {
  75. return $cat['parent_id'] == $id;
  76. });
  77. // 获取该分类下的书籍
  78. $categoryBooks = $this->getBooks($categories, $id);
  79. // 获取面包屑
  80. $breadcrumbs = $this->getBreadcrumbs($currentCategory, $categories);
  81. $types = $this->types();
  82. return view('library.tipitaka.category', compact(
  83. 'currentCategory',
  84. 'subCategories',
  85. 'categoryBooks',
  86. 'breadcrumbs',
  87. 'types'
  88. ));
  89. }
  90. private function types()
  91. {
  92. return ['translation', 'original', 'nissaya'];
  93. }
  94. private function subCategories($categories, int $id)
  95. {
  96. return array_filter($categories, function ($cat) use ($id) {
  97. return $cat['parent_id'] == $id;
  98. });
  99. }
  100. private function getRecent()
  101. {
  102. return [
  103. [
  104. 'id' => 'book-001',
  105. 'title' => '相应部·因缘篇',
  106. 'author' => 'Bhikkhu Bodhi',
  107. 'cover' => null, // 无封面时显示渐变
  108. 'cover_gradient' => 'linear-gradient(135deg, #2d5a8e 0%, #1a3a5c 100%)',
  109. 'updated_at' => '2小时前',
  110. 'is_new' => true, // true=新增, false=更新
  111. 'category' => '经藏',
  112. ],
  113. [
  114. 'id' => 'book-002',
  115. 'title' => '长部·梵网经',
  116. 'author' => 'Bhikkhu Sujato',
  117. 'cover' => null,
  118. 'cover_gradient' => 'linear-gradient(135deg, #5a2d8e 0%, #3a1a5c 100%)',
  119. 'updated_at' => '昨天',
  120. 'is_new' => false,
  121. 'category' => '经藏',
  122. ],
  123. [
  124. 'id' => 'book-003',
  125. 'title' => '法句经注',
  126. 'author' => 'Buddhaghosa',
  127. 'cover' => null,
  128. 'cover_gradient' => 'linear-gradient(135deg, #8e5a2d 0%, #5c3a1a 100%)',
  129. 'updated_at' => '3天前',
  130. 'is_new' => false,
  131. 'category' => '注释',
  132. ],
  133. [
  134. 'id' => 'book-004',
  135. 'title' => '律藏·波罗夷',
  136. 'author' => 'Bhikkhu Brahmali',
  137. 'cover' => null,
  138. 'cover_gradient' => 'linear-gradient(135deg, #2d8e5a 0%, #1a5c3a 100%)',
  139. 'updated_at' => '5天前',
  140. 'is_new' => true,
  141. 'category' => '律藏',
  142. ],
  143. [
  144. 'id' => 'book-005',
  145. 'title' => '清净道论',
  146. 'author' => 'Buddhaghosa',
  147. 'cover' => null,
  148. 'cover_gradient' => 'linear-gradient(135deg, #8e2d2d 0%, #5c1a1a 100%)',
  149. 'updated_at' => '1周前',
  150. 'is_new' => false,
  151. 'category' => '注释',
  152. ],
  153. [
  154. 'id' => 'book-006',
  155. 'title' => '增支部·一集',
  156. 'author' => 'Bhikkhu Bodhi',
  157. 'cover' => null,
  158. 'cover_gradient' => 'linear-gradient(135deg, #2d7a8e 0%, #1a4a5c 100%)',
  159. 'updated_at' => '1周前',
  160. 'is_new' => false,
  161. 'category' => '经藏',
  162. ],
  163. ];
  164. }
  165. private function getUpdateBooks()
  166. {
  167. $books = ProgressChapter::with('channel.owner')
  168. ->leftJoin('pali_texts', function ($join) {
  169. $join->on('progress_chapters.book', '=', 'pali_texts.book')
  170. ->on('progress_chapters.para', '=', 'pali_texts.paragraph');
  171. })
  172. ->whereHas('channel', function ($query) {
  173. $query->where('status', 30);
  174. })
  175. ->where('progress', '>', config('mint.library.list_min_progress'))
  176. ->take(10)
  177. ->get();
  178. return $this->getBooksInfo($books);
  179. }
  180. private function getBooks($categories, $id)
  181. {
  182. $currentCategory = collect($categories)->firstWhere('id', $id);
  183. if (!$currentCategory) {
  184. abort(404);
  185. }
  186. // 标签查章节
  187. $tagNames = $currentCategory['tag'];
  188. $tm = (new TagMap)->getTable();
  189. $tg = (new Tag)->getTable();
  190. $pt = (new PaliText)->getTable();
  191. $where1 = " where co = " . count($tagNames);
  192. $a = implode(",", array_fill(0, count($tagNames), '?'));
  193. $in1 = "and t.name in ({$a})";
  194. $param = $tagNames;
  195. $where2 = "where level = 1";
  196. $query = "
  197. select uid as id,book,paragraph,level,toc as title,chapter_strlen,parent,path from (
  198. select anchor_id as cid from (
  199. select tm.anchor_id , count(*) as co
  200. from $tm as tm
  201. left join $tg as t on tm.tag_id = t.id
  202. where tm.table_name = 'pali_texts'
  203. $in1
  204. group by tm.anchor_id
  205. ) T
  206. $where1
  207. ) CID
  208. left join $pt as pt on CID.cid = pt.uid
  209. $where2
  210. order by book,paragraph";
  211. $chapters = DB::select($query, $param);
  212. $chaptersParam = [];
  213. foreach ($chapters as $key => $chapter) {
  214. $chaptersParam[] = [$chapter->book, $chapter->paragraph];
  215. }
  216. // 获取该分类下的章节
  217. $books = ProgressChapter::with('channel.owner')
  218. ->whereIns(['progress_chapters.book', 'progress_chapters.para'], $chaptersParam)
  219. ->whereHas('channel', function ($query) {
  220. $query->where('status', 30);
  221. })
  222. ->where('progress', '>', config('mint.library.list_min_progress'))
  223. ->get();
  224. return $this->getBooksInfo($books);
  225. }
  226. private function getBooksInfo($books,)
  227. {
  228. $pali = PaliText::where('level', 1)->get();
  229. // 获取该分类下的书籍
  230. $categoryBooks = [];
  231. $books->each(function ($book) use (&$categoryBooks, $pali) {
  232. $title = $book->title;
  233. if (empty($title)) {
  234. $title = $pali->firstWhere('book', $book->book)->toc;
  235. }
  236. //Log::debug('getBooksInfo', ['book' => $book->book, 'paragraph' => $book->para]);
  237. $pcd_book_id = $pali->first(function ($item) use ($book) {
  238. return $item->book == $book->book
  239. && $item->paragraph == $book->para;
  240. })?->pcd_book_id;
  241. $coverFile = "/assets/images/cover/zh-hans/1/{$pcd_book_id}.png";
  242. if (File::exists(public_path($coverFile))) {
  243. $coverUrl = $coverFile;
  244. } else {
  245. $coverUrl = null;
  246. }
  247. $colorIdx = $this->colorIndex($book->uid);
  248. $categoryBooks[] = [
  249. "id" => $book->uid,
  250. "title" => $title,
  251. "author" => $book->channel->name,
  252. "publisher" => $book->channel->owner,
  253. "type" => __('labels.' . $book->channel->type),
  254. "cover" => $coverUrl,
  255. 'cover_gradient' => $this->coverGradients[$colorIdx % count($this->coverGradients)],
  256. "description" => $book->summary ?? "比库戒律的详细说明",
  257. "language" => __('language.' . $book->channel->lang),
  258. ];
  259. });
  260. return $categoryBooks;
  261. }
  262. private function loadCategories()
  263. {
  264. $json = file_get_contents(public_path("data/category/default.json"));
  265. $tree = json_decode($json, true);
  266. $flat = self::flattenWithIds($tree);
  267. return $flat;
  268. }
  269. public static function flattenWithIds(array $tree, int $parentId = 0, int $level = 1): array
  270. {
  271. $flat = [];
  272. foreach ($tree as $node) {
  273. $currentId = self::$nextId++;
  274. $item = [
  275. 'id' => $currentId,
  276. 'parent_id' => $parentId,
  277. 'name' => $node['name'] ?? null,
  278. 'tag' => $node['tag'] ?? [],
  279. "description" => "佛教戒律经典",
  280. 'level' => $level,
  281. ];
  282. $flat[] = $item;
  283. if (isset($node['children']) && is_array($node['children'])) {
  284. $childrenLevel = $level + 1;
  285. $flat = array_merge($flat, self::flattenWithIds($node['children'], $currentId, $childrenLevel));
  286. }
  287. }
  288. return $flat;
  289. }
  290. private function getBreadcrumbs($category, $categories)
  291. {
  292. $breadcrumbs = [];
  293. $current = $category;
  294. while ($current) {
  295. array_unshift($breadcrumbs, $current);
  296. $current = collect($categories)->firstWhere('id', $current['parent_id']);
  297. }
  298. return $breadcrumbs;
  299. }
  300. }