Browse Source

书籍信息从db读取

visuddhinanda 11 months ago
parent
commit
877711167e
1 changed files with 82 additions and 8 deletions
  1. 82 8
      api-v8/app/Http/Controllers/BookController.php

+ 82 - 8
api-v8/app/Http/Controllers/BookController.php

@@ -4,21 +4,95 @@ namespace App\Http\Controllers;
 
 use Illuminate\Http\Request;
 use Illuminate\Support\Facades\Storage;
+use App\Models\ProgressChapter;
+use App\Models\PaliText;
 
 class BookController extends Controller
 {
     public function show($id)
     {
-        $books = $this->loadBooks();
-        $book = collect($books)->firstWhere('id', $id);
+        $bookRaw = $this->loadBook($id);
 
-        if (!$book) {
+        if (!$bookRaw) {
             abort(404);
         }
 
+        //查询章节
+        $channelId = $bookRaw->channel_id; // 替换为具体的 channel_id 值
+
+        $paliTexts = PaliText::where('book', $bookRaw->book)
+            ->where('paragraph', '>', $bookRaw->para)
+            ->where('level', 2)->orderBy('paragraph')->get();
+        $chapters = ProgressChapter::where('book', $bookRaw->book)
+            ->where('para', '>', $bookRaw->para)
+            ->where('channel_id', $channelId)->orderBy('para')->get();
+        $book =  [
+            "id" => $bookRaw->uid,
+            "title" => $bookRaw->title . "(" . $bookRaw->book . "-" . $bookRaw->para . ")",
+            "author" => $bookRaw->channel->name,
+            "publisher" => $bookRaw->channel->owner->nickname,
+            "type" => __('label.' . $bookRaw->channel->type),
+            "category_id" => 11,
+            "cover" => "/assets/images/cover/1/214.jpg",
+            "description" => $book->summary ?? "比库戒律的详细说明",
+            "language" => __('language.' . $bookRaw->channel->lang),
+            "contents" => $paliTexts->map(function ($paliText) use ($chapters) {
+                $title = $paliText->toc;
+                if (count($chapters) > 0) {
+
+                    $found = array_filter($chapters->toArray(), function ($chapter) use ($paliText) {
+                        return $chapter['book'] == $paliText->book && $chapter['para'] == $paliText->paragraph;
+                    });
+                    if (count($found) > 0) {
+                        $chapter = array_shift($found);
+                        if (!empty($chapter['title'])) {
+                            $title = $chapter['title'];
+                        }
+                        if (!empty($chapter['summary'])) {
+                            $summary = $chapter['summary'];
+                        }
+                        $progress = (int)($chapter['progress'] * 100);
+                    }
+                }
+                return [
+                    "title" => $title,
+                    "content" => "诸恶莫作,众善奉行,自净其意,是诸佛教...",
+                    "summary" => $summary ?? "",
+                    "progress" => $progress ?? 0,
+                ];
+            }),
+        ];
+
         // 获取其他版本
-        $otherVersions = array_filter($books, function ($b) use ($book) {
-            return $b['title'] == $book['title'] && $b['id'] != $book['id'];
+        $others = ProgressChapter::with('channel.owner')
+            ->where('book', $bookRaw->book)
+            ->where('para', $bookRaw->para)
+            ->whereHas('channel', function ($query) {
+                $query->where('status', 30);
+            })
+            ->where('progress', '>', 0.2)
+            ->get();
+
+        $otherVersions = [];
+        $others->each(function ($book) use (&$otherVersions, $id) {
+            $otherVersions[] = [
+                "id" => $book->uid,
+                "title" => $book->title . "(" . $book->book . "-" . $book->para . ")",
+                "author" => $book->channel->name,
+                "publisher" => $book->channel->owner->nickname,
+                "type" => __('label.' . $book->channel->type),
+                "category_id" => $id,
+                "cover" => "/assets/images/cover/1/214.jpg",
+                "description" => $book->summary ?? "比库戒律的详细说明",
+                "language" => __('language.' . $book->channel->lang),
+                "contents" => [
+                    [
+                        "title" => "比库戒本",
+                        "content" => "诸恶莫作,众善奉行,自净其意,是诸佛教...",
+                        "summary" => "基本戒律",
+                    ]
+                ],
+            ];
         });
 
         return view('library.book.show', compact('book', 'otherVersions'));
@@ -36,9 +110,9 @@ class BookController extends Controller
         return view('library.book.read', compact('book'));
     }
 
-    private function loadBooks()
+    private function loadBook($id)
     {
-        $json = Storage::disk('public')->get('data/books.json');
-        return json_decode($json, true);
+        $book = ProgressChapter::with('channel.owner')->find($id);
+        return $book;
     }
 }