BookController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\Storage;
  5. use App\Models\ProgressChapter;
  6. use App\Models\PaliText;
  7. class BookController extends Controller
  8. {
  9. public function show($id)
  10. {
  11. $bookRaw = $this->loadBook($id);
  12. if (!$bookRaw) {
  13. abort(404);
  14. }
  15. //查询章节
  16. $channelId = $bookRaw->channel_id; // 替换为具体的 channel_id 值
  17. $paliTexts = PaliText::where('book', $bookRaw->book)
  18. ->where('paragraph', '>', $bookRaw->para)
  19. ->where('level', 2)->orderBy('paragraph')->get();
  20. $chapters = ProgressChapter::where('book', $bookRaw->book)
  21. ->where('para', '>', $bookRaw->para)
  22. ->where('channel_id', $channelId)->orderBy('para')->get();
  23. $book = [
  24. "id" => $bookRaw->uid,
  25. "title" => $bookRaw->title . "(" . $bookRaw->book . "-" . $bookRaw->para . ")",
  26. "author" => $bookRaw->channel->name,
  27. "publisher" => $bookRaw->channel->owner->nickname,
  28. "type" => __('label.' . $bookRaw->channel->type),
  29. "category_id" => 11,
  30. "cover" => "/assets/images/cover/1/214.jpg",
  31. "description" => $book->summary ?? "比库戒律的详细说明",
  32. "language" => __('language.' . $bookRaw->channel->lang),
  33. "contents" => $paliTexts->map(function ($paliText) use ($chapters) {
  34. $title = $paliText->toc;
  35. if (count($chapters) > 0) {
  36. $found = array_filter($chapters->toArray(), function ($chapter) use ($paliText) {
  37. return $chapter['book'] == $paliText->book && $chapter['para'] == $paliText->paragraph;
  38. });
  39. if (count($found) > 0) {
  40. $chapter = array_shift($found);
  41. if (!empty($chapter['title'])) {
  42. $title = $chapter['title'];
  43. }
  44. if (!empty($chapter['summary'])) {
  45. $summary = $chapter['summary'];
  46. }
  47. $progress = (int)($chapter['progress'] * 100);
  48. }
  49. }
  50. return [
  51. "title" => $title,
  52. "content" => "诸恶莫作,众善奉行,自净其意,是诸佛教...",
  53. "summary" => $summary ?? "",
  54. "progress" => $progress ?? 0,
  55. ];
  56. }),
  57. ];
  58. // 获取其他版本
  59. $others = ProgressChapter::with('channel.owner')
  60. ->where('book', $bookRaw->book)
  61. ->where('para', $bookRaw->para)
  62. ->whereHas('channel', function ($query) {
  63. $query->where('status', 30);
  64. })
  65. ->where('progress', '>', 0.2)
  66. ->get();
  67. $otherVersions = [];
  68. $others->each(function ($book) use (&$otherVersions, $id) {
  69. $otherVersions[] = [
  70. "id" => $book->uid,
  71. "title" => $book->title . "(" . $book->book . "-" . $book->para . ")",
  72. "author" => $book->channel->name,
  73. "publisher" => $book->channel->owner->nickname,
  74. "type" => __('label.' . $book->channel->type),
  75. "category_id" => $id,
  76. "cover" => "/assets/images/cover/1/214.jpg",
  77. "description" => $book->summary ?? "比库戒律的详细说明",
  78. "language" => __('language.' . $book->channel->lang),
  79. "contents" => [
  80. [
  81. "title" => "比库戒本",
  82. "content" => "诸恶莫作,众善奉行,自净其意,是诸佛教...",
  83. "summary" => "基本戒律",
  84. ]
  85. ],
  86. ];
  87. });
  88. return view('library.book.show', compact('book', 'otherVersions'));
  89. }
  90. public function read($id)
  91. {
  92. $books = $this->loadBooks();
  93. $book = collect($books)->firstWhere('id', $id);
  94. if (!$book) {
  95. abort(404);
  96. }
  97. return view('library.book.read', compact('book'));
  98. }
  99. private function loadBook($id)
  100. {
  101. $book = ProgressChapter::with('channel.owner')->find($id);
  102. return $book;
  103. }
  104. }