Browse Source

使用 PaliContentService 渲染

visuddhinanda 3 days ago
parent
commit
1dd0b53ec4
1 changed files with 28 additions and 17 deletions
  1. 28 17
      api-v12/app/Http/Controllers/Library/BookController.php

+ 28 - 17
api-v12/app/Http/Controllers/Library/BookController.php

@@ -10,6 +10,7 @@ use App\Models\PaliText;
 use App\Models\Sentence;
 use App\Models\Sentence;
 
 
 use App\Services\ChapterService;
 use App\Services\ChapterService;
+use App\Services\PaliContentService;
 
 
 class BookController extends Controller
 class BookController extends Controller
 {
 {
@@ -204,30 +205,40 @@ class BookController extends Controller
             }
             }
         }
         }
 
 
+        $paraStart = $curr->paragraph;
+        $paraEnd = $endParagraph;
+        $paragraphs = app(PaliContentService::class)->paragraphs(
+            $book->book,
+            $paraStart,
+            $paraEnd,
+            [$book->channel_id],
+            ['mode' => 'read', 'format' => 'html', 'original' => false]
+        );
+
         //获取句子数据
         //获取句子数据
-        $sentences = Sentence::where('book_id', $book->book)
-            ->whereBetween('paragraph', [$curr->paragraph, $endParagraph])
-            ->where('channel_uid', $book->channel_id)
-            ->orderBy('paragraph')
-            ->orderBy('word_start')
-            ->get();
+
         $pali = PaliText::where('book', $book->book)
         $pali = PaliText::where('book', $book->book)
             ->whereBetween('paragraph', [$curr->paragraph, $endParagraph])
             ->whereBetween('paragraph', [$curr->paragraph, $endParagraph])
+            ->select(['paragraph', 'level'])
             ->orderBy('paragraph')
             ->orderBy('paragraph')
             ->get();
             ->get();
         $result = [];
         $result = [];
-        for ($i = $curr->paragraph; $i <= $endParagraph; $i++) {
-            $texts = array_filter($sentences->toArray(), function ($sentence) use ($i) {
-                return $sentence['paragraph'] == $i;
-            });
-            $contents = array_map(function ($text) {
-                return $text['content'];
-            }, $texts);
-            $currPali = $pali->firstWhere('paragraph', $i);
+        foreach ($paragraphs as $key => $paragraph) {
+            $content = [];
+            foreach ($paragraph['children'] as $key => $sent) {
+                if (isset($sent['translation'])) {
+                    foreach ($sent['translation'] as $key => $translation) {
+                        $curr = $translation['html'] ?? $translation['content'];
+                        $content[] = "<span class='sentence'>{$curr}</span>";
+                    }
+                }
+            }
+            $currPaliPara = $pali->firstWhere('paragraph', $paragraph['para']);
+            $level = $currPaliPara->level;
             $paragraph = [
             $paragraph = [
-                'id' => $i,
-                'level' => $currPali->level,
-                'text' => [[implode('', $contents)]],
+                'id' => $paragraph['para'],
+                'level' => $level,
+                'text' => [[implode('', $content)]],
             ];
             ];
             $result[] = $paragraph;
             $result[] = $paragraph;
         }
         }