Ver código fonte

add:第一章节不记录最近阅读;

Wang Chen 4 anos atrás
pai
commit
eab1520e86

+ 4 - 4
app/Http/Controllers/QuickApp/Book/ChapterController.php

@@ -114,7 +114,7 @@ class ChapterController extends BaseController
         if ($chapter->is_vip == 0) {
             ReadRecordService::addReadRecord([
                 'uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
-                'cid' => $cid, 'chapter_name' => $chapter->name
+                'cid' => $cid, 'chapter_name' => $chapter->name, 'sequence' => $chapter->sequence
             ]);
             return response()->item(new ChapterTransformer, $this->getChapter($bid, $cid, $chapter));
         }
@@ -123,7 +123,7 @@ class ChapterController extends BaseController
         if ($this->getOrderRecord($bid, $cid) || BookConfigService::judgeBookIsFree($bid)) {
             ReadRecordService::addReadRecord([
                 'uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
-                'cid' => $cid, 'chapter_name' => $chapter->name
+                'cid' => $cid, 'chapter_name' => $chapter->name, 'sequence' => $chapter->sequence
             ]);
 
             return response()->item(new ChapterTransformer, $this->getChapter($bid, $cid, $chapter));
@@ -164,7 +164,7 @@ class ChapterController extends BaseController
             UserTaskService::addUserTaskQueue($this->uid, BaseTask::read, UserTaskService::judge_trigger);
             ReadRecordService::addReadRecord([
                 'uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
-                'cid' => $cid, 'chapter_name' => $chapter->name
+                'cid' => $cid, 'chapter_name' => $chapter->name, 'sequence' => $chapter->sequence
             ]);
 
             return response()->item(new ChapterTransformer, $this->getChapter($bid, $cid, $chapter));
@@ -203,7 +203,7 @@ class ChapterController extends BaseController
         if ($this->balancePay($book_info, $cid, $chapter->size, $chapter->name, $remind)) {
             ReadRecordService::addReadRecord([
                 'uid' => $this->uid, 'bid' => $bid, 'book_name' => $book_info->book_name,
-                'cid' => $cid, 'chapter_name' => $chapter->name
+                'cid' => $cid, 'chapter_name' => $chapter->name, 'sequence' => $chapter->sequence
             ]);
 
             return response()->item(new ChapterTransformer, $this->getChapter($bid, $cid, $chapter));

+ 9 - 10
app/Http/Controllers/QuickApp/User/ReadRecordController.php

@@ -21,12 +21,11 @@ class ReadRecordController extends BaseController
      */
 
 
-
     /**
      * @apiVersion 1.0.0
      * @apiDescription 获取阅读记录
      * @api {get} readrecord 获取阅读记录
-     * @apiHeader {String} [Authorization]  token 
+     * @apiHeader {String} [Authorization]  token
      * @apiGroup ReadRecord
      * @apiName index
      * @apiSuccess {int}         code 状态码
@@ -65,21 +64,21 @@ class ReadRecordController extends BaseController
     public function index(Request $request)
     {
         $user = $this->user_info;
-        $res = ReadRecordService::getReadRecord($this->uid);
+        $res  = ReadRecordService::getReadRecord($this->uid);
         if ($res) {
             $id_arr = [];
             foreach ($res as $key => $value) {
                 $id_arr[] = $value['bid'];
             }
 
-            $book = BookConfigService::getBooksByIds($id_arr,[],false);//下架图书最近阅读可看到
+            $book = BookConfigService::getBooksByIds($id_arr, [], false);//下架图书最近阅读可看到
             foreach ($res as $key => &$value) {
-                $value['cover'] = '';
+                $value['cover']        = '';
                 $value['last_chapter'] = 0;
-                foreach ($book as  $val) {
+                foreach ($book as $val) {
                     if ($value['bid'] == $val->bid) {
-                        $value['book_name'] = $val->book_name;
-                        $value['cover'] = $val->cover;
+                        $value['book_name']    = $val->book_name;
+                        $value['cover']        = $val->cover;
                         $value['last_chapter'] = $val->last_chapter;
                         break;
                     }
@@ -90,7 +89,7 @@ class ReadRecordController extends BaseController
             if ($a['time'] >= $b['time']) return -1;
             return 1;
         });
-        return response()->collection(new ReadRecordTransformer(),  array_to_object($res));
+        return response()->collection(new ReadRecordTransformer(), array_to_object($res));
     }
 
 
@@ -136,7 +135,7 @@ class ReadRecordController extends BaseController
      * @apiDescription 删除阅读记录
      * @api {get} readrecord/delete 删除阅读记录
      * @apiGroup ReadRecord
-     * @apiHeader {String} [Authorization]  token 
+     * @apiHeader {String} [Authorization]  token
      * @apiName delReadRecord
      * @apiParam   {String}         bid     多个bid以,分隔
      * @apiSuccess {int}         code 状态码

+ 92 - 82
app/Modules/User/Services/ReadRecordService.php

@@ -22,7 +22,7 @@ class ReadRecordService
     {
         $record = Redis::hGet('book_read:' . $uid, $bid);
         if ($record) {
-            $cid = explode('_', $record)[0];
+            $cid  = explode('_', $record)[0];
             $name = self::cid2ChapterName($cid);
             return ['record_chapter_id' => $cid, 'record_chapter_name' => $name];
         }
@@ -31,15 +31,15 @@ class ReadRecordService
 
     /**
      * 删除最近阅读记录
-     * @param int $uid
+     * @param int   $uid
      * @param array $bids
      */
     public static function delReadRecordStatic(int $uid, array $bids)
     {
-        $key = 'book_read:' . $uid;
-        $last_record = explode('_', Redis::hGet($key, 'last_read'));
+        $key             = 'book_read:' . $uid;
+        $last_record     = explode('_', Redis::hGet($key, 'last_read'));
         $last_record_bid = $last_record[0];
-        $is_del_last = false;
+        $is_del_last     = false;
         foreach ($bids as $bid) {
             if ($bid == $last_record_bid) {
                 $is_del_last = true;
@@ -57,18 +57,18 @@ class ReadRecordService
      */
     public static function reSetLastRecord($uid)
     {
-        $key = 'book_read:' . $uid;
-        $alls = Redis::hGetAll($key);
-        $has_record = false;
+        $key            = 'book_read:' . $uid;
+        $alls           = Redis::hGetAll($key);
+        $has_record     = false;
         $last_timestamp = 0;
-        $last_record = '';
+        $last_record    = '';
         foreach ($alls as $k => $v) {
             if (is_numeric($k)) {
                 $has_record = true;
-                $record = explode('_', $v);
-                $timestamp = $record[1];
+                $record     = explode('_', $v);
+                $timestamp  = $record[1];
                 if ($last_timestamp < $timestamp) {
-                    $last_record = $k . '_' . $v;
+                    $last_record    = $k . '_' . $v;
                     $last_timestamp = $timestamp;
                 }
             }
@@ -94,15 +94,15 @@ class ReadRecordService
     public static function getReadRecord_($uid)
     {
         $read_bids = Redis::hgetall('book_read:' . $uid);
-        $res = [];
-        $i = 0;
+        $res       = [];
+        $i         = 0;
         foreach ($read_bids as $key => $v) {
-            $record = explode('_', $v);
-            $latest_read_cid = $record[0];
-            $book_name = $record[1];
-            $chapter_name = $record[2];
+            $record           = explode('_', $v);
+            $latest_read_cid  = $record[0];
+            $book_name        = $record[1];
+            $chapter_name     = $record[2];
             $latest_read_time = $record[count($record) - 1];
-            $res[$i] = ['book_name' => $book_name, 'bid' => $key, 'cid' => (int) $latest_read_cid, 'time' => (int) $latest_read_time, 'chapter_name' => $chapter_name];
+            $res[$i]          = ['book_name' => $book_name, 'bid' => $key, 'cid' => (int)$latest_read_cid, 'time' => (int)$latest_read_time, 'chapter_name' => $chapter_name];
             $i++;
         }
         usort($res, function ($a, $b) {
@@ -125,18 +125,18 @@ class ReadRecordService
         }
         self::delTheLastRecord($uid);
         $read_bids = Redis::hgetall('book_read:' . $uid);
-        $res = [];
-        $i = 0;
+        $res       = [];
+        $i         = 0;
         foreach ($read_bids as $key => $v) {
             if (in_array($key, self::$not_uid_key)) {
                 continue;
             }
-            $record = explode('_', $v);
-            $latest_read_cid = $record[0];
+            $record           = explode('_', $v);
+            $latest_read_cid  = $record[0];
             $latest_read_time = $record[count($record) - 1];
-            $book_name = self::bid2BookName($key);
-            $chapter_name = self::cid2ChapterName($latest_read_cid);
-            $res[$i] = ['book_name' => $book_name, 'bid' => $key, 'cid' => (int) $latest_read_cid, 'time' => (int) $latest_read_time, 'chapter_name' => $chapter_name];
+            $book_name        = self::bid2BookName($key);
+            $chapter_name     = self::cid2ChapterName($latest_read_cid);
+            $res[$i]          = ['book_name' => $book_name, 'bid' => $key, 'cid' => (int)$latest_read_cid, 'time' => (int)$latest_read_time, 'chapter_name' => $chapter_name];
             $i++;
         }
         usort($res, function ($a, $b) {
@@ -156,10 +156,10 @@ class ReadRecordService
      */
     public static function addReadRecord_($param)
     {
-        $uid = $param['uid'];
-        $bid = $param['bid'];
-        $cid = $param['cid'];
-        $book_name = $param['book_name'];
+        $uid          = $param['uid'];
+        $bid          = $param['bid'];
+        $cid          = $param['cid'];
+        $book_name    = $param['book_name'];
         $chapter_name = $param['chapter_name'];
         Redis::hset('book_base:' . $uid, 'last_read', "{$bid}_{$cid}_{$book_name}_{$chapter_name}_" . time());
         //Redis::hset('book_read:'.$uid, $bid, $cid."_".time());
@@ -168,18 +168,25 @@ class ReadRecordService
 
     /**
      * 添加阅读记录升级版
-     * @param array $param
+     * @param $param
+     * @return bool
+     * @throws \Exception
      */
     public static function addReadRecord($param)
     {
-        $uid = $param['uid'];
-        $bid = $param['bid'];
-        $cid = $param['cid'];
+        // 第一章不计入阅读记录
+        $sequence = (int)getProp($param, 'sequence');
+        if ($sequence <= 1) {
+            return false;
+        }
 
-        $book_name = isset($param['book_name']) ? $param['book_name'] : '';
-        $chapter_name = isset($param['chapter_name']) ? $param['chapter_name'] : '';
-        $book_key = 'wap:string:book:' . $bid;
-        $chapter_key = 'wap:string:chapter:' . $cid;
+        $uid          = $param['uid'];
+        $bid          = $param['bid'];
+        $cid          = $param['cid'];
+        $book_name    = getProp($param, 'book_name');
+        $chapter_name = getProp($param, 'chapter_name');
+        $book_key     = 'wap:string:book:' . $bid;
+        $chapter_key  = 'wap:string:chapter:' . $cid;
         if ($book_name) {
             Redis::setex($book_key, 3600, $book_name);
         }
@@ -191,6 +198,8 @@ class ReadRecordService
         if ($num <= 3) {
             self::delTheLastRecord($uid);
         }
+
+        return true;
     }
 
     /**
@@ -203,17 +212,17 @@ class ReadRecordService
         if (empty($all)) return [];
         $first = $all[0];
         if (!$first) return [];
-        if (!isset($first['bid']))  return [];
+        if (!isset($first['bid'])) return [];
         try {
             //$bid = Hashids::encode($first['bid']);
-            $bid = $first['bid'];
+            $bid       = $first['bid'];
             $book_info = BookConfigService::getBookById($bid);
-            $cid = $first['cid'];
+            $cid       = $first['cid'];
             $book_name = $first['book_name'];
-            $res = [
-                'url' => '/reader?bid=' . $bid . '&cid=' . $cid,
-                'book_name' => $book_name,
-                'cover' => $book_info->cover,
+            $res       = [
+                'url'          => '/reader?bid=' . $bid . '&cid=' . $cid,
+                'book_name'    => $book_name,
+                'cover'        => $book_info->cover,
                 'channel_name' => $book_info->channel_name,
             ];
         } catch (\Exception $e) {
@@ -233,21 +242,21 @@ class ReadRecordService
         self::delBookBase($uid);
         $record = Redis::hget('book_read:' . $uid, 'last_read');
         if ($record) {
-            $record_arr = explode('_', $record);
-            $bid = $record_arr[0];
-            $bid = Hashids::encode($bid);
-            $cid = $record_arr[1];
-            $time = $record_arr[2];
-            $book_info = BookConfigService::getBookById($bid);
-            $book_name = isset($book_info->book_name) ? $book_info->book_name : '';
-            $cover = isset($book_info->cover) ? $book_info->cover : '';
+            $record_arr   = explode('_', $record);
+            $bid          = $record_arr[0];
+            $bid          = Hashids::encode($bid);
+            $cid          = $record_arr[1];
+            $time         = $record_arr[2];
+            $book_info    = BookConfigService::getBookById($bid);
+            $book_name    = isset($book_info->book_name) ? $book_info->book_name : '';
+            $cover        = isset($book_info->cover) ? $book_info->cover : '';
             $channel_name = isset($book_info->channel_name) ? $book_info->channel_name : '';
-            $res = [
-                'url' => '/reader?bid=' . $bid . '&cid=' . $cid,
-                'book_name' => $book_name,
-                'cover' => $cover,
+            $res          = [
+                'url'          => '/reader?bid=' . $bid . '&cid=' . $cid,
+                'book_name'    => $book_name,
+                'cover'        => $cover,
                 'channel_name' => $channel_name,
-                'time' => $time
+                'time'         => $time
             ];
             return $res;
         }
@@ -265,8 +274,8 @@ class ReadRecordService
             $record = Redis::hget('book_read:' . $uid, 'last_read');
             if ($record) {
                 $record_arr = explode('_', $record);
-                $bid = $record_arr[0];
-                return (int) $bid;
+                $bid        = $record_arr[0];
+                return (int)$bid;
             }
         } catch (\Exception $e) {
         }
@@ -298,7 +307,7 @@ class ReadRecordService
      */
     public static function bid2BookName($bid)
     {
-        $book_key = 'wap:string:book:' . $bid;
+        $book_key  = 'wap:string:book:' . $bid;
         $book_name = Redis::get($book_key);
         Redis::EXPIRE($book_key, 3600);
         if (!$book_name) {
@@ -317,7 +326,7 @@ class ReadRecordService
      */
     public static function cid2ChapterName($cid)
     {
-        $chapter_key = 'wap:string:chapter:' . $cid;
+        $chapter_key  = 'wap:string:chapter:' . $cid;
         $chapter_name = Redis::get($chapter_key);
         Redis::EXPIRE($chapter_key, 3600);
         if (!$chapter_name) {
@@ -340,10 +349,10 @@ class ReadRecordService
         $base_record = Redis::hget('book_base:' . $uid, 'last_read');
         if ($base_record) {
             $record_arr = explode('_', $base_record);
-            $c = count($record_arr);
+            $c          = count($record_arr);
             if ($c > 3) {
-                $bid = $record_arr[0];
-                $cid = $record_arr[1];
+                $bid  = $record_arr[0];
+                $cid  = $record_arr[1];
                 $time = $record_arr[$c - 1];
                 Redis::hset('book_base:' . $uid, 'last_read', "{$bid}_{$cid}_" . $time);
             }
@@ -351,15 +360,15 @@ class ReadRecordService
         $records = Redis::hgetall('book_read:' . $uid);
         foreach ($records as $key => $v) {
             $record = explode('_', $v);
-            $count = count($record);
+            $count  = count($record);
             if ($count > 3) {
-                $latest_read_cid = $record[0];
-                $book_name = $record[1];
-                $chapter_name = $record[2];
+                $latest_read_cid  = $record[0];
+                $book_name        = $record[1];
+                $chapter_name     = $record[2];
                 $latest_read_time = $record[$count - 1];
                 Redis::hset('book_read:' . $uid, $key, "{$latest_read_cid}_" . $latest_read_time);
 
-                $book_key = 'wap:string:book:' . $key;
+                $book_key    = 'wap:string:book:' . $key;
                 $chapter_key = 'wap:string:chapter:' . $latest_read_cid;
                 Redis::set($book_key, $book_name);
                 Redis::set($chapter_key, $chapter_name);
@@ -384,7 +393,7 @@ class ReadRecordService
     public static function getSimpleReadRecord(int $uid): array
     {
         $read_bids = Redis::hgetall('book_read:' . $uid);
-        $res = [];
+        $res       = [];
         if (!$read_bids) {
             return $res;
         }
@@ -402,11 +411,11 @@ class ReadRecordService
     {
         try {
             DB::table('temp_read_active')->insert([
-                'uid' => $uid,
+                'uid'                     => $uid,
                 'distribution_channel_id' => $distribution_channel_id,
-                'from' => $from,
-                'created_at' => date('Y-m-d H:i:s'),
-                'updated_at' => date('Y-m-d H:i:s'),
+                'from'                    => $from,
+                'created_at'              => date('Y-m-d H:i:s'),
+                'updated_at'              => date('Y-m-d H:i:s'),
             ]);
         } catch (\Exception $e) {
         }
@@ -422,7 +431,7 @@ class ReadRecordService
         try {
             $send_order_id = Redis::hget('book_read:' . $uid, 'send_order_id');
             if ($send_order_id)
-                return (int) $send_order_id;
+                return (int)$send_order_id;
         } catch (\Exception $e) {
         }
         return 0;
@@ -458,6 +467,7 @@ class ReadRecordService
         }
         return '';
     }
+
     /**
      * 签到日期
      * @param int $uid
@@ -522,7 +532,7 @@ class ReadRecordService
         try {
             $count = Redis::hget('book_read:' . $uid, 'sign_counts');
             if ($count) {
-                return (int) $count;
+                return (int)$count;
             }
             return 0;
         } catch (\Exception $e) {
@@ -555,7 +565,7 @@ class ReadRecordService
 
     public static function getSmartPush(int $uid): array
     {
-        $res =  Redis::hget('book_read:' . $uid, 'smart_push');
+        $res = Redis::hget('book_read:' . $uid, 'smart_push');
         if ($res) {
             return explode(',', $res);
         }
@@ -631,15 +641,15 @@ class ReadRecordService
             return;
         }
         $read_bids = Redis::hgetall('book_read:' . $uid);
-        $i = 0;
+        $i         = 0;
         foreach ($read_bids as $key => $v) {
             if (in_array($key, self::$not_uid_key)) {
                 continue;
             }
-            $record = explode('_', $v);
-            $latest_read_cid = $record[0];
+            $record           = explode('_', $v);
+            $latest_read_cid  = $record[0];
             $latest_read_time = $record[count($record) - 1];
-            $res[$i++] = ['bid' => $key, 'cid' => (int) $latest_read_cid, 'time' => (int) $latest_read_time];
+            $res[$i++]        = ['bid' => $key, 'cid' => (int)$latest_read_cid, 'time' => (int)$latest_read_time];
         }
         usort($res, function ($a, $b) {
             if ($a['time'] >= $b['time']) return -1;