From e65887f52ebedd45779e3aa47ca791feb7ba2b89 Mon Sep 17 00:00:00 2001 From: Default Date: Thu, 3 Apr 2025 17:21:55 +0800 Subject: [PATCH] fix chapter page --- code/app/home/controller/Novel.php | 26 ++++++++++- code/app/model/ChapterModel.php | 69 ++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 1 deletion(-) diff --git a/code/app/home/controller/Novel.php b/code/app/home/controller/Novel.php index b71137b..9c8debc 100644 --- a/code/app/home/controller/Novel.php +++ b/code/app/home/controller/Novel.php @@ -604,7 +604,32 @@ class Novel extends BaseController $ChapterModel = new ChapterModel; + $strKey = sprintf('Nid:%s:Chapter-Page:%s:Order:%s', $intNId, $intPage, $strOrder); + $arrFilter = ['n_id' => $intNId, 'c_page' => 0,]; + $intLimit = 100; + $intLifeTime = 12 * 3600; + + if ($strOrder == 'zheng') { + $arrSort = ['c_sort_num' => 1]; + } else { + $arrSort = ['c_sort_num' => -1]; + } + + $arrPage = $ChapterModel->getCommonNovelPageOnCache($strKey, $arrFilter, $intPage, $intLimit, $intLifeTime, $arrSort); + + $intButtomNum = 5; + if ($boolIsMobile == false) { + $intButtomNum = 10; + + } + + $strBaseUrl = sprintf('/pc/xiaoshuo/%s-%s/mulu/%s/{page}', $arrNovel['og_novel_pin_yin'], $intNId, $strOrder); + $arrPaginator = CusteomPage02::generate($intPage, $arrPage['total'], $intLimit, $strBaseUrl, $intButtomNum); + + // echo '
';
+        // print_r($arrPaginator);
+        // exit;
         // 章节目录
         $arrChapter = [
             ['c_sort_num' => 1, 'name' => '第1章', 'created_at' => '作者1'],
@@ -614,7 +639,6 @@ class Novel extends BaseController
             ['c_sort_num' => 1, 'name' => '第5章', 'created_at' => '作者1'],
             ['c_sort_num' => 1, 'name' => '第6章', 'created_at' => '作者1'],
 
-
         ];
 
         View::assign([
diff --git a/code/app/model/ChapterModel.php b/code/app/model/ChapterModel.php
index 0e3dc33..f95ade6 100644
--- a/code/app/model/ChapterModel.php
+++ b/code/app/model/ChapterModel.php
@@ -36,6 +36,75 @@ class ChapterModel extends MongoModel
         ],
     ];
 
+
+
+    /**
+     * Undocumented function
+     *
+     * @param string $strKey
+     * @param array $arrFilter
+     * @param integer $intPage
+     * @param integer $intLimit
+     * @param integer $intLifeTime
+     * @param array $arrSort
+     * @return array
+     */
+    public function getCommonNovelPageOnCache(
+        string $strKey,
+        array $arrFilter,
+        int $intPage = 1,
+        int $intLimit = 20,
+        int $intLifeTime = 24 * 3600,
+        array $arrSort = ['c_sort_num' => 1]
+    ): array {
+        try {
+
+            $arrResult = Cache::get($strKey);
+
+            // if (empty($arrResult)) {
+            if (true) {
+                $arrOptions = self::$arrChapterOptions;
+
+                $arrOptions = array_merge($arrOptions, [
+                    'sort' => $arrSort,
+                    // 'limit' => $intCount * 3,
+                    'skip' => ($intPage - 1) * $intLimit,
+                    'limit' => $intLimit,
+                ]);
+
+
+                $Cursor = $this->getCol()->find($arrFilter, $arrOptions);
+
+                $intTotal =  $this->getCol()->countDocuments($arrFilter);
+
+                $arrResult = iterator_to_array($Cursor);
+
+                if (!empty($arrResult)) {
+                    foreach($arrResult as &$arrChapter){
+                        applyToKeys($arrChapter, ['created_at', 'updated_at'], 'formatMongoDate');
+                    }
+
+                }
+
+                $arrResult = [
+                    'data' => $arrResult,
+                    'total' => $intTotal,
+                    'page' => $intPage,
+                    'limit' => $intLimit,
+                    'pages' => ceil($intTotal / $intLimit)
+                ];
+
+                if (!empty($arrResult)) {
+                    Cache::set($strKey, $arrResult, $intLifeTime);
+                }
+            }
+
+            return $arrResult;
+        } catch (\Exception $e) {
+            return [];
+        }
+    }
+
     /**
      * Undocumented function
      *