BookController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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['pagination'] = $this->pagination($bookRaw);
  49. $book['content'] = $this->getBookContent($id);
  50. return view('library.book.read', compact('book'));
  51. }
  52. private function loadBook($id)
  53. {
  54. $book = ProgressChapter::with('channel.owner')->find($id);
  55. return $book;
  56. }
  57. public function toggleTheme(Request $request)
  58. {
  59. $theme = $request->input('theme', 'light');
  60. session(['theme' => $theme]);
  61. return response()->json(['status' => 'success']);
  62. }
  63. private function getBookInfo($book)
  64. {
  65. $title = $book->title;
  66. if (empty($title)) {
  67. $title = PaliText::where('book', $book->book)
  68. ->where('paragraph', $book->para)->first()->toc;
  69. }
  70. return [
  71. "id" => $book->uid,
  72. "title" => $title,
  73. "author" => $book->channel->name,
  74. "publisher" => $book->channel->owner,
  75. "type" => __('label.' . $book->channel->type),
  76. "category_id" => 11,
  77. "cover" => "/assets/images/cover/1/214.jpg",
  78. "description" => $book->summary ?? "",
  79. "language" => __('language.' . $book->channel->lang),
  80. ];
  81. }
  82. private function getBookToc(int $book, int $paragraph, string $channelId, $minLevel = 2, $maxLevel = 2)
  83. {
  84. //先找到书的起始(书名)章节
  85. //一个book 里面可以有多本书
  86. $currBook = $this->bookStart($book, $paragraph);
  87. $start = $currBook->paragraph;
  88. $end = $currBook->paragraph + $currBook->chapter_len - 1;
  89. $paliTexts = PaliText::where('book', $book)
  90. ->whereBetween('paragraph', [$start, $end])
  91. ->whereBetween('level', [$minLevel, $maxLevel])->orderBy('paragraph')->get();
  92. $chapters = ProgressChapter::where('book', $book)
  93. ->whereBetween('para', [$start, $end])
  94. ->where('channel_id', $channelId)->orderBy('para')->get();
  95. $toc = $paliTexts->map(function ($paliText) use ($chapters) {
  96. $title = $paliText->toc;
  97. if (count($chapters) > 0) {
  98. $found = array_filter($chapters->toArray(), function ($chapter) use ($paliText) {
  99. return $chapter['book'] == $paliText->book && $chapter['para'] == $paliText->paragraph;
  100. });
  101. if (count($found) > 0) {
  102. $chapter = array_shift($found);
  103. if (!empty($chapter['title'])) {
  104. $title = $chapter['title'];
  105. }
  106. if (!empty($chapter['summary'])) {
  107. $summary = $chapter['summary'];
  108. }
  109. $progress = (int)($chapter['progress'] * 100);
  110. $id = $chapter['uid'];
  111. }
  112. }
  113. return [
  114. "id" => $id ?? '',
  115. "title" => $title,
  116. "summary" => $summary ?? "",
  117. "progress" => $progress ?? 0,
  118. "level" => (int)$paliText->level,
  119. "disabled" => !isset($progress),
  120. ];
  121. })->toArray();
  122. return $toc;
  123. }
  124. public function getBookCategory($book, $paragraph)
  125. {
  126. $tags = PaliText::with('tagMaps.tags')
  127. ->where('book', $book)
  128. ->where('paragraph', $paragraph)
  129. ->first()->tagMaps->map(function ($tagMap) {
  130. return $tagMap->tags;
  131. })->toArray();
  132. return $tags;
  133. }
  134. private function bookStart($book, $paragraph)
  135. {
  136. $currBook = PaliText::where('book', $book)
  137. ->where('paragraph', '<=', $paragraph)
  138. ->where('level', 1)
  139. ->orderBy('paragraph', 'desc')
  140. ->first();
  141. return $currBook;
  142. }
  143. public function getBookContent($id)
  144. {
  145. //查询book信息
  146. $book = $this->loadBook($id);
  147. $currBook = $this->bookStart($book->book, $book->para);
  148. $start = $currBook->paragraph;
  149. $end = $currBook->paragraph + $currBook->chapter_len - 1;
  150. // 查询起始段落
  151. $paragraphs = PaliText::where('book', $book->book)
  152. ->whereBetween('paragraph', [$start, $end])
  153. ->where('level', '<', 8)
  154. ->orderBy('paragraph')
  155. ->get();
  156. $curr = $paragraphs->firstWhere('paragraph', $book->para);
  157. $endParagraph = $curr->paragraph + $curr->chapter_len - 1;
  158. if ($curr->chapter_strlen > $this->maxChapterLen) {
  159. //太大了,修改结束位置 找到下一级
  160. foreach ($paragraphs as $key => $paragraph) {
  161. if ($paragraph->paragraph > $curr->paragraph) {
  162. if ($paragraph->chapter_strlen <= $this->maxChapterLen) {
  163. $endParagraph = $paragraph->paragraph + $paragraph->chapter_len - 1;
  164. break;
  165. }
  166. if ($paragraph->level <= $curr->level) {
  167. //不能往下走了,就是它了
  168. $endParagraph = $paragraphs[$key - 1]->paragraph + $paragraphs[$key - 1]->chapter_len - 1;
  169. break;
  170. }
  171. }
  172. }
  173. }
  174. //获取句子数据
  175. $sentences = Sentence::where('book_id', $book->book)
  176. ->whereBetween('paragraph', [$curr->paragraph, $endParagraph])
  177. ->where('channel_uid', $book->channel_id)
  178. ->orderBy('paragraph')
  179. ->orderBy('word_start')
  180. ->get();
  181. $pali = PaliText::where('book', $book->book)
  182. ->whereBetween('paragraph', [$curr->paragraph, $endParagraph])
  183. ->orderBy('paragraph')
  184. ->get();
  185. $result = [];
  186. for ($i = $curr->paragraph; $i <= $endParagraph; $i++) {
  187. $texts = array_filter($sentences->toArray(), function ($sentence) use ($i) {
  188. return $sentence['paragraph'] == $i;
  189. });
  190. $contents = array_map(function ($text) {
  191. return $text['content'];
  192. }, $texts);
  193. $currPali = $pali->firstWhere('paragraph', $i);
  194. $paragraph = [
  195. 'id' => $i,
  196. 'level' => $currPali->level,
  197. 'text' => [[implode('', $contents)]],
  198. ];
  199. $result[] = $paragraph;
  200. }
  201. return $result;
  202. }
  203. public function pagination($book)
  204. {
  205. $currBook = $this->bookStart($book->book, $book->para);
  206. $start = $currBook->paragraph;
  207. $end = $currBook->paragraph + $currBook->chapter_len - 1;
  208. // 查询起始段落
  209. $paragraphs = PaliText::where('book', $book->book)
  210. ->whereBetween('paragraph', [$start, $end])
  211. ->where('level', '<', 8)
  212. ->orderBy('paragraph')
  213. ->get();
  214. $curr = $paragraphs->firstWhere('paragraph', $book->para);
  215. $current = $curr; //实际显示的段落
  216. $endParagraph = $curr->paragraph + $curr->chapter_len - 1;
  217. if ($curr->chapter_strlen > $this->maxChapterLen) {
  218. //太大了,修改结束位置 找到下一级
  219. foreach ($paragraphs as $key => $paragraph) {
  220. if ($paragraph->paragraph > $curr->paragraph) {
  221. if ($paragraph->chapter_strlen <= $this->maxChapterLen) {
  222. $endParagraph = $paragraph->paragraph + $paragraph->chapter_len - 1;
  223. $current = $paragraph;
  224. break;
  225. }
  226. if ($paragraph->level <= $curr->level) {
  227. //不能往下走了,就是它了
  228. $endParagraph = $paragraphs[$key - 1]->paragraph + $paragraphs[$key - 1]->chapter_len - 1;
  229. $current = $paragraph;
  230. break;
  231. }
  232. }
  233. }
  234. }
  235. $start = $curr->paragraph;
  236. $end = $endParagraph;
  237. $nextPali = $this->next($current->book, $current->paragraph, $current->level);
  238. $prevPali = $this->prev($current->book, $current->paragraph, $current->level);
  239. $next = null;
  240. if ($nextPali) {
  241. $nextTranslation = ProgressChapter::with('channel.owner')
  242. ->where('book', $nextPali->book)
  243. ->where('para', $nextPali->paragraph)
  244. ->where('channel_id', $book->channel_id)
  245. ->first();
  246. if ($nextTranslation) {
  247. if (!empty($nextTranslation->title)) {
  248. $next['title'] = $nextTranslation->title;
  249. } else {
  250. $next['title'] = $nextPali->toc;
  251. }
  252. $next['id'] = $nextTranslation->uid;
  253. }
  254. }
  255. $prev = null;
  256. if ($prevPali) {
  257. $prevTranslation = ProgressChapter::with('channel.owner')
  258. ->where('book', $prevPali->book)
  259. ->where('para', $prevPali->paragraph)
  260. ->where('channel_id', $book->channel_id)
  261. ->first();
  262. if ($prevTranslation) {
  263. if (!empty($prevTranslation->title)) {
  264. $prev['title'] = $prevTranslation->title;
  265. } else {
  266. $prev['title'] = $prevPali->toc;
  267. }
  268. $prev['id'] = $prevTranslation->uid;
  269. }
  270. }
  271. return compact('start', 'end', 'next', 'prev');
  272. }
  273. public function next($book, $paragraph, $level)
  274. {
  275. $next = PaliText::where('book', $book)
  276. ->where('paragraph', '>', $paragraph)
  277. ->where('level', $level)
  278. ->orderBy('paragraph')
  279. ->first();
  280. return $next ?? null;
  281. }
  282. public function prev($book, $paragraph, $level)
  283. {
  284. $prev = PaliText::where('book', $book)
  285. ->where('paragraph', '<', $paragraph)
  286. ->where('level', $level)
  287. ->orderBy('paragraph', 'desc')
  288. ->first();
  289. return $prev ?? null;
  290. }
  291. public function show2($id)
  292. {
  293. // Sample book data (replace with database query)
  294. $book = [
  295. 'title' => 'Sample Book Title',
  296. 'author' => 'John Doe',
  297. 'category' => 'Fiction',
  298. 'tags' => ['Adventure', 'Mystery', 'Bestseller'],
  299. 'toc' => ['Introduction', 'Chapter 1', 'Chapter 2', 'Conclusion'],
  300. 'content' => [
  301. 'This is the introduction to the book...',
  302. 'Chapter 1 content goes here...',
  303. 'Chapter 2 content goes here...',
  304. 'Conclusion of the book...',
  305. ],
  306. 'downloads' => [
  307. ['format' => 'PDF', 'url' => '#'],
  308. ['format' => 'EPUB', 'url' => '#'],
  309. ['format' => 'MOBI', 'url' => '#'],
  310. ],
  311. ];
  312. // Sample related books (replace with database query)
  313. $relatedBooks = [
  314. [
  315. 'title' => 'Related Book 1',
  316. 'description' => 'A thrilling adventure...',
  317. 'image' => 'https://via.placeholder.com/300x200',
  318. 'link' => '#',
  319. ],
  320. [
  321. 'title' => 'Related Book 2',
  322. 'description' => 'A mystery novel...',
  323. 'image' => 'https://via.placeholder.com/300x200',
  324. 'link' => '#',
  325. ],
  326. [
  327. 'title' => 'Related Book 3',
  328. 'description' => 'A bestseller...',
  329. 'image' => 'https://via.placeholder.com/300x200',
  330. 'link' => '#',
  331. ],
  332. ];
  333. return view('library.book.read2', compact('book', 'relatedBooks'));
  334. }
  335. }