BookController.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. use App\Models\Sentence;
  8. class BookController extends Controller
  9. {
  10. protected $maxChapterLen = 50000;
  11. protected $minChapterLen = 100;
  12. public function show($id)
  13. {
  14. $bookRaw = $this->loadBook($id);
  15. if (!$bookRaw) {
  16. abort(404);
  17. }
  18. //查询章节
  19. $channelId = $bookRaw->channel_id; // 替换为具体的 channel_id 值
  20. $book = $this->getBookInfo($bookRaw);
  21. $book['contents'] = $this->getBookToc($bookRaw->book, $bookRaw->para, $channelId);
  22. // 获取其他版本
  23. $others = ProgressChapter::with('channel.owner')
  24. ->where('book', $bookRaw->book)
  25. ->where('para', $bookRaw->para)
  26. ->whereHas('channel', function ($query) {
  27. $query->where('status', 30);
  28. })
  29. ->where('progress', '>', 0.2)
  30. ->get();
  31. $otherVersions = [];
  32. $others->each(function ($book) use (&$otherVersions, $id) {
  33. $otherVersions[] = $this->getBookInfo($book);
  34. });
  35. return view('library.book.show', compact('book', 'otherVersions'));
  36. }
  37. public function read($id)
  38. {
  39. $bookRaw = $this->loadBook($id);
  40. if (!$bookRaw) {
  41. abort(404);
  42. }
  43. $channelId = $bookRaw->channel_id; // 替换为具体的 channel_id 值
  44. $book = $this->getBookInfo($bookRaw);
  45. $book['toc'] = $this->getBookToc($bookRaw->book, $bookRaw->para, $channelId, 2, 7);
  46. $book['categories'] = $this->getBookCategory($bookRaw->book, $bookRaw->para);
  47. $book['tags'] = [];
  48. $book['content'] = $this->getBookContent($id);
  49. return view('library.book.read', compact('book'));
  50. }
  51. private function loadBook($id)
  52. {
  53. $book = ProgressChapter::with('channel.owner')->find($id);
  54. return $book;
  55. }
  56. public function toggleTheme(Request $request)
  57. {
  58. $theme = $request->input('theme', 'light');
  59. session(['theme' => $theme]);
  60. return response()->json(['status' => 'success']);
  61. }
  62. private function getBookInfo($book)
  63. {
  64. $title = $book->title;
  65. if (empty($title)) {
  66. $title = PaliText::where('book', $book->book)
  67. ->where('paragraph', $book->para)->first()->toc;
  68. }
  69. return [
  70. "id" => $book->uid,
  71. "title" => $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" => 11,
  76. "cover" => "/assets/images/cover/1/214.jpg",
  77. "description" => $book->summary ?? "",
  78. "language" => __('language.' . $book->channel->lang),
  79. ];
  80. }
  81. private function getBookToc(int $book, int $paragraph, string $channelId, $minLevel = 2, $maxLevel = 2)
  82. {
  83. //先找到书的起始(书名)章节
  84. //一个book 里面可以有多本书
  85. $currBook = $this->bookStart($book, $paragraph);
  86. $start = $currBook->paragraph;
  87. $end = $currBook->paragraph + $currBook->chapter_len - 1;
  88. $paliTexts = PaliText::where('book', $book)
  89. ->whereBetween('paragraph', [$start, $end])
  90. ->whereBetween('level', [$minLevel, $maxLevel])->orderBy('paragraph')->get();
  91. $chapters = ProgressChapter::where('book', $book)
  92. ->whereBetween('para', [$start, $end])
  93. ->where('channel_id', $channelId)->orderBy('para')->get();
  94. $toc = $paliTexts->map(function ($paliText) use ($chapters) {
  95. $title = $paliText->toc;
  96. if (count($chapters) > 0) {
  97. $found = array_filter($chapters->toArray(), function ($chapter) use ($paliText) {
  98. return $chapter['book'] == $paliText->book && $chapter['para'] == $paliText->paragraph;
  99. });
  100. if (count($found) > 0) {
  101. $chapter = array_shift($found);
  102. if (!empty($chapter['title'])) {
  103. $title = $chapter['title'];
  104. }
  105. if (!empty($chapter['summary'])) {
  106. $summary = $chapter['summary'];
  107. }
  108. $progress = (int)($chapter['progress'] * 100);
  109. $id = $chapter['uid'];
  110. }
  111. }
  112. return [
  113. "id" => $id ?? '',
  114. "title" => $title,
  115. "summary" => $summary ?? "",
  116. "progress" => $progress ?? 0,
  117. "level" => (int)$paliText->level,
  118. "disabled" => !isset($progress),
  119. ];
  120. })->toArray();
  121. return $toc;
  122. }
  123. public function getBookCategory($book, $paragraph)
  124. {
  125. $tags = PaliText::with('tagMaps.tags')
  126. ->where('book', $book)
  127. ->where('paragraph', $paragraph)
  128. ->first()->tagMaps->map(function ($tagMap) {
  129. return $tagMap->tags;
  130. })->toArray();
  131. return $tags;
  132. }
  133. private function bookStart($book, $paragraph)
  134. {
  135. $currBook = PaliText::where('book', $book)
  136. ->where('paragraph', '<=', $paragraph)
  137. ->where('level', 1)
  138. ->orderBy('paragraph', 'desc')
  139. ->first();
  140. return $currBook;
  141. }
  142. public function getBookContent($id)
  143. {
  144. //查询book信息
  145. $book = $this->loadBook($id);
  146. $currBook = $this->bookStart($book->book, $book->para);
  147. $start = $currBook->paragraph;
  148. $end = $currBook->paragraph + $currBook->chapter_len - 1;
  149. // 查询起始段落
  150. $paragraphs = PaliText::where('book', $book->book)
  151. ->whereBetween('paragraph', [$start, $end])
  152. ->where('level', '<', 8)
  153. ->orderBy('paragraph')
  154. ->get();
  155. $curr = $paragraphs->firstWhere('paragraph', $book->para);
  156. $endParagraph = $curr->paragraph + $curr->chapter_len - 1;
  157. if ($curr->chapter_strlen > $this->maxChapterLen) {
  158. //太大了,修改结束位置 找到下一级
  159. foreach ($paragraphs as $key => $paragraph) {
  160. if ($paragraph->paragraph > $curr->paragraph) {
  161. if ($paragraph->chapter_strlen <= $this->maxChapterLen) {
  162. $endParagraph = $paragraph->paragraph + $paragraph->chapter_len - 1;
  163. break;
  164. }
  165. if ($paragraph->level <= $curr->level) {
  166. //不能往下走了,就是它了
  167. $endParagraph = $paragraphs[$key - 1]->paragraph + $paragraphs[$key - 1]->chapter_len - 1;
  168. break;
  169. }
  170. }
  171. }
  172. }
  173. //获取句子数据
  174. $sentences = Sentence::where('book_id', $book->book)
  175. ->whereBetween('paragraph', [$curr->paragraph, $endParagraph])
  176. ->where('channel_uid', $book->channel_id)
  177. ->orderBy('paragraph')
  178. ->orderBy('word_start')
  179. ->get();
  180. $pali = PaliText::where('book', $book->book)
  181. ->whereBetween('paragraph', [$curr->paragraph, $endParagraph])
  182. ->orderBy('paragraph')
  183. ->get();
  184. $result = [];
  185. for ($i = $curr->paragraph; $i <= $endParagraph; $i++) {
  186. $texts = array_filter($sentences->toArray(), function ($sentence) use ($i) {
  187. return $sentence['paragraph'] == $i;
  188. });
  189. $contents = array_map(function ($text) {
  190. return $text['content'];
  191. }, $texts);
  192. $currPali = $pali->firstWhere('paragraph', $i);
  193. $paragraph = [
  194. 'id' => $i,
  195. 'level' => $currPali->level,
  196. 'text' => [[implode('', $contents)]],
  197. ];
  198. $result[] = $paragraph;
  199. }
  200. return $result;
  201. }
  202. public function show2($id)
  203. {
  204. // Sample book data (replace with database query)
  205. $book = [
  206. 'title' => 'Sample Book Title',
  207. 'author' => 'John Doe',
  208. 'category' => 'Fiction',
  209. 'tags' => ['Adventure', 'Mystery', 'Bestseller'],
  210. 'toc' => ['Introduction', 'Chapter 1', 'Chapter 2', 'Conclusion'],
  211. 'content' => [
  212. 'This is the introduction to the book...',
  213. 'Chapter 1 content goes here...',
  214. 'Chapter 2 content goes here...',
  215. 'Conclusion of the book...',
  216. ],
  217. 'downloads' => [
  218. ['format' => 'PDF', 'url' => '#'],
  219. ['format' => 'EPUB', 'url' => '#'],
  220. ['format' => 'MOBI', 'url' => '#'],
  221. ],
  222. ];
  223. // Sample related books (replace with database query)
  224. $relatedBooks = [
  225. [
  226. 'title' => 'Related Book 1',
  227. 'description' => 'A thrilling adventure...',
  228. 'image' => 'https://via.placeholder.com/300x200',
  229. 'link' => '#',
  230. ],
  231. [
  232. 'title' => 'Related Book 2',
  233. 'description' => 'A mystery novel...',
  234. 'image' => 'https://via.placeholder.com/300x200',
  235. 'link' => '#',
  236. ],
  237. [
  238. 'title' => 'Related Book 3',
  239. 'description' => 'A bestseller...',
  240. 'image' => 'https://via.placeholder.com/300x200',
  241. 'link' => '#',
  242. ],
  243. ];
  244. return view('library.book.read2', compact('book', 'relatedBooks'));
  245. }
  246. }