fly 5 年之前
父節點
當前提交
17e2a8492e

+ 26 - 21
app/Http/Controllers/QuickApp/User/ReadRecordController.php

@@ -7,10 +7,12 @@ use App\Http\Controllers\QuickApp\BaseController;
 use App\Modules\Book\Services\BookConfigService;
 use App\Modules\Book\Services\UserShelfBooksService;
 use App\Http\Controllers\QuickApp\User\Transformers\ReadRecordTransformer;
+use App\Modules\Book\Services\BookService;
 use App\Modules\User\Services\ReadRecordService;
 use Hashids;
 use Redis;
 use Log;
+
 class ReadRecordController extends BaseController
 {
 
@@ -61,9 +63,10 @@ class ReadRecordController extends BaseController
      *           }
      *           ]
      */
-    public function index(Request $request){
+    public function index(Request $request)
+    {
         $res = ReadRecordService::getReadRecord($this->uid);
-        if($res){
+        if ($res) {
             $id_arr = [];
             foreach ($res as $key => $value) {
                 $id_arr[] = $value['bid'];
@@ -72,7 +75,7 @@ class ReadRecordController extends BaseController
             $book = BookConfigService::getBooksByIds($id_arr);
             foreach ($res as $key => &$value) {
                 foreach ($book as  $val) {
-                    if($value['bid'] == $val->bid){
+                    if ($value['bid'] == $val->bid) {
                         $value['cover'] = $val->cover;
                         $value['last_chapter'] = $val->last_chapter;
                         break;
@@ -81,24 +84,23 @@ class ReadRecordController extends BaseController
             }
 
             $shelf = UserShelfBooksService::getUserShelfBooksListByUid($this->uid);
-            foreach ($res as &$v){
+            foreach ($res as &$v) {
                 $v['is_on_user_shelf'] = 0;
-                foreach ($shelf as $val){
-                    if($v['bid'] == $val->bid){
+                foreach ($shelf as $val) {
+                    if ($v['bid'] == $val->bid) {
                         $v['is_on_user_shelf'] = 1;
                         break;
                     }
                 }
             }
         }
-        usort($res,function($a,$b){
-            if($a['time'] >= $b['time']) return -1;
+        usort($res, function ($a, $b) {
+            if ($a['time'] >= $b['time']) return -1;
             return 1;
-
         });
         $res = json_encode($res);
         $res = json_decode($res);
-        return response()->collection(new ReadRecordTransformer(),$res);
+        return response()->collection(new ReadRecordTransformer(), $res);
     }
 
 
@@ -125,22 +127,22 @@ class ReadRecordController extends BaseController
      *       data:{}
      *
      */
-    public function addReadRecord(Request $request){
+    public function addReadRecord(Request $request)
+    {
         $param = $request->except('_url');
-        if(checkParam($param,['bid','cid','chapter_name'])){
+        if (checkParam($param, ['bid', 'cid', 'chapter_name'])) {
             return response()->error('LACK_PARAM');
         }
         $param['uid'] = $this->uid;
         $param['bid'] = Hashids::decode($param['bid'])[0];
-        $record_info = Redis::hget('book_read:'.$this->uid,$param['bid']);
+        $record_info = Redis::hget('book_read:' . $this->uid, $param['bid']);
         $param['book_name'] = 'unknown';
-        if($record_info){
-            $param['book_name'] = explode('_',$record_info)[1];
+        if ($record_info) {
+            $param['book_name'] = explode('_', $record_info)[1];
         }
 
         ReadRecordService::addReadRecord($param);
         return response()->success();
-
     }
 
 
@@ -164,14 +166,17 @@ class ReadRecordController extends BaseController
      *       data:{}
      *
      */
-    public function delReadRecord(Request $request){
+    public function delReadRecord(Request $request)
+    {
         $param = $request->except('_url');
-        if(checkParam($param,['bid'])){
+        if (checkParam($param, ['bid'])) {
             return response()->error('LACK_PARAM');
         }
-        $param['bid'] = Hashids::decode($param['bid'])[0];
-        ReadRecordService::delReadRecord($this->uid,$param['bid']);
+        $bids = explode(',', $param['bid']);
+        array_walk($bids, function (&$item) {
+            $item = BookService::decodeBidStatic($item);
+        });
+        ReadRecordService::delReadRecord($this->uid, $bids);
         return response()->success();
     }
-
 }

+ 0 - 14
app/Http/Controllers/QuickApp/User/UserShelfBooksController.php

@@ -69,15 +69,10 @@ class UserShelfBooksController extends BaseController
      */
     public function index(Request $request){
         $res = UserShelfBooksService::getUserShelfBooksListByUid($this->uid);
-
         if($res->isEmpty()){
             return response()->success();
         }
-
         $record = ReadRecordService::getReadRecord($this->uid);
-
-        //if(empty($record))   return response()->success($res);
-
         foreach ($res as &$v){
             $v['last_cid'] = $v['first_cid'];
             foreach ($record as $val){
@@ -88,13 +83,6 @@ class UserShelfBooksController extends BaseController
                 }
             }
         }
-
-        /*
-        usort($res,function($a,$b){
-            if($a['updated_at'] >= $b['updated_at']) return -1;
-            return 1;
-
-        });*/
         return response()->collection(new UserShelfBooksTransformer(),$res);
     }
 
@@ -198,8 +186,6 @@ class UserShelfBooksController extends BaseController
         $bid = $request->input('bid');
         if(!$bid) return response()->error('LACK_PARAM');
         $bid = Hashids::decode($bid)[0];
-        //$param['uid'] = $this->uid;
-        //$res = $this->apiClient->get($this->joinUrl('Read/isonshelf'),$param);
         $res = UserShelfBooksService::getUserShelfBooksListByUidAndBid($this->uid,$bid);
         if($res){
             $data['is_on'] = 1;

+ 264 - 211
app/Modules/User/Services/ReadRecordService.php

@@ -1,4 +1,5 @@
 <?php
+
 /**
  * Created by PhpStorm.
  * User: hp
@@ -17,10 +18,65 @@ use DB;
 
 class ReadRecordService
 {
+    #region 以后按照这种方式写静态方法
+    use BaseService;
+    /**
+     * 删除最近阅读记录
+     * @param int $uid
+     * @param array $bids
+     */
+    public function delReadRecord(int $uid, array $bids)
+    {
+        $key = 'book_read:' . $uid;
+        $last_record = explode('_', Redis::hGet($key, 'last_read'));
+        $last_record_bid = $last_record[0];
+        $is_del_last = false;
+        foreach ($bids as $bid) {
+            if ($bid == $last_record_bid) {
+                $is_del_last = true;
+            }
+            Redis::hDel($key, $bid);
+        }
+        if ($is_del_last) {
+            $this->reSetLastRecord($uid);
+        }
+    }
+
+    /**
+     * 重置用户最近阅读记录
+     * @param int $uid
+     */
+    public function reSetLastRecord($uid)
+    {
+        $key = 'book_read:' . $uid;
+        $alls = Redis::hGetAll($key);
+        $has_record = false;
+        $last_timestamp = 0;
+        $last_record = '';
+        foreach ($alls as $k => $v) {
+            if (is_numeric($k)) {
+                $has_record = true;
+                $record = explode('_', $v);
+                $timestamp = $record[1];
+                if ($last_timestamp < $timestamp) {
+                    $last_record = $k . '_' . $v;
+                    $last_timestamp = $timestamp;
+                }
+            }
+        }
+        if ($last_record) {
+            Redis::hSet($key, 'last_read', $last_record);
+        }
+        if (!$has_record) {
+            Redis::hDel($key, 'last_read');
+        }
+    }
+    #endregion
+
     //阅读记录数
     const RECORD_COUNT = 50;
 
-    private static $not_uid_key = ['last_read','send_order_id','sign_count','sign_counts','sign_info','sign_day','smart_push','inner_send_order_id','gxhp','property','bind_phone_status','ua','sign_version','new_outer','new_inner','new_total','next_push_hour','person_account_id'];
+    private static $not_uid_key = ['last_read', 'send_order_id', 'sign_count', 'sign_counts', 'sign_info', 'sign_day', 'smart_push', 'inner_send_order_id', 'gxhp', 'property', 'bind_phone_status', 'ua', 'sign_version', 'new_outer', 'new_inner', 'new_total', 'next_push_hour', 'person_account_id'];
 
     /**
      * 获取
@@ -38,13 +94,12 @@ class ReadRecordService
             $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) {
             if ($a['time'] >= $b['time']) return -1;
             return 1;
-
         });
         return $res;
     }
@@ -55,9 +110,9 @@ class ReadRecordService
      * @param $uid
      * @return array
      */
-    public static function getReadRecord($uid,$is_need_check_db=false)
+    public static function getReadRecord($uid, $is_need_check_db = false)
     {
-        if($is_need_check_db){
+        if ($is_need_check_db) {
             self::resetRecordFromDB($uid);
         }
         self::delTheLastRecord($uid);
@@ -66,7 +121,7 @@ class ReadRecordService
         $i = 0;
         //self::delBookNameAndChapter($uid);
         foreach ($read_bids as $key => $v) {
-            if(in_array($key,self::$not_uid_key)){
+            if (in_array($key, self::$not_uid_key)) {
                 continue;
             }
             $record = explode('_', $v);
@@ -74,13 +129,12 @@ class ReadRecordService
             $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];
+            $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) {
             if ($a['time'] >= $b['time']) return -1;
             return 1;
-
         });
         return $res;
     }
@@ -115,62 +169,47 @@ class ReadRecordService
         $bid = $param['bid'];
         $cid = $param['cid'];
 
-        $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;
-        if($book_name){
-            Redis::setex($book_key,3600,$book_name);
+        $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;
+        if ($book_name) {
+            Redis::setex($book_key, 3600, $book_name);
         }
-        if($chapter_name){
-            Redis::setex($chapter_key,3600,$chapter_name);
+        if ($chapter_name) {
+            Redis::setex($chapter_key, 3600, $chapter_name);
         }
-
-        /*Redis::hset('book_read:' . $uid, 'last_read', "{$bid}_{$cid}_" . time());
-        //Redis::hset('book_read:'.$uid, $bid, $cid."_".time());
-        Redis::hset('book_read:' . $uid, $bid, "{$cid}_" . time());*/
-        Redis::hmset('book_read:' . $uid,'last_read', "{$bid}_{$cid}_" . time(),$bid, "{$cid}_" . time(),'next_push_hour',8);
-        $num = random_int(1,100);
-        if($num <=3){
+        Redis::hmset('book_read:' . $uid, 'last_read', "{$bid}_{$cid}_" . time(), $bid, "{$cid}_" . time(), 'next_push_hour', 8);
+        $num = random_int(1, 100);
+        if ($num <= 3) {
             self::delTheLastRecord($uid);
         }
     }
-    
-    /**
-     * 删除
-     * @param $uid
-     * @param $bid
-     */
-    public static function delReadRecord($uid, $bid)
-    {
-        if (Redis::hexists('book_read:' . $uid, $bid)) {
-            Redis::hdel('book_read:' . $uid, $bid);
-        }
-    }
 
     /**
      * 获取最近一条阅读记录
      * @param $uid
      */
-    public static function getFirstReadRecord_($uid){
+    public static function getFirstReadRecord_($uid)
+    {
         $all = self::getReadRecord($uid);
-        if(empty($all)) return [];
+        if (empty($all)) return [];
         $first = $all[0];
-        if(!$first) return [];
-        if(!isset($first['bid']))  return [];
-        try{
+        if (!$first) return [];
+        if (!isset($first['bid']))  return [];
+        try {
             //$bid = Hashids::encode($first['bid']);
             $bid = $first['bid'];
             $book_info = BookConfigService::getBookById($bid);
             $cid = $first['cid'];
             $book_name = $first['book_name'];
             $res = [
-                'url' => '/reader?bid='.$bid.'&cid='.$cid,
-                'book_name'=>$book_name,
-                'cover' =>$book_info->cover,
-                'channel_name'=>$book_info->channel_name,
+                'url' => '/reader?bid=' . $bid . '&cid=' . $cid,
+                'book_name' => $book_name,
+                'cover' => $book_info->cover,
+                'channel_name' => $book_info->channel_name,
             ];
-        }catch (\Exception $e){
+        } catch (\Exception $e) {
             $res = [];
         }
 
@@ -182,26 +221,27 @@ class ReadRecordService
      * @param $uid
      * @return array
      */
-    public static function getFirstReadRecord($uid){
+    public static function getFirstReadRecord($uid)
+    {
         self::delBookBase($uid);
         //Redis::hget('book_base:' . $uid, 'last_read', "{$bid}_{$cid}_{$book_name}_{$chapter_name}_" . time());
         $record = Redis::hget('book_read:' . $uid, 'last_read');
-        if($record){
-            $record_arr = explode('_',$record);
+        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:'';
-            $channel_name = isset($book_info->channel_name)?$book_info->channel_name:'';
+            $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,
-                'channel_name'=>$channel_name,
-                'time'=>$time
+                'url' => '/reader?bid=' . $bid . '&cid=' . $cid,
+                'book_name' => $book_name,
+                'cover' => $cover,
+                'channel_name' => $channel_name,
+                'time' => $time
             ];
             return $res;
         }
@@ -213,17 +253,16 @@ class ReadRecordService
      * @param $uid
      * @return int
      */
-    public static function getSimpleFirstReadRecord($uid){
-        try{
+    public static function getSimpleFirstReadRecord($uid)
+    {
+        try {
             $record = Redis::hget('book_read:' . $uid, 'last_read');
-            if($record){
-                $record_arr = explode('_',$record);
+            if ($record) {
+                $record_arr = explode('_', $record);
                 $bid = $record_arr[0];
-                return (int)$bid;
+                return (int) $bid;
             }
-        }catch (\Exception $e){
-
-        }
+        } catch (\Exception $e) { }
         return 0;
     }
 
@@ -231,15 +270,17 @@ class ReadRecordService
      * 获取客服消息点击数
      * @param $uid
      */
-    public static function getCustomerMsgClickNum($channel_id,$from,$date){
-        $key = "fromcustomermsgenter:distribution_channel_id:".$channel_id.'from:'.$from;
-        return Redis::hget($key,$date);
+    public static function getCustomerMsgClickNum($channel_id, $from, $date)
+    {
+        $key = "fromcustomermsgenter:distribution_channel_id:" . $channel_id . 'from:' . $from;
+        return Redis::hget($key, $date);
     }
 
     /**
      * 获取某本书的阅读记录
      */
-    public static function getRecordByUidBid($uid,$bid){
+    public static function getRecordByUidBid($uid, $bid)
+    {
         return Redis::hget('book_read:' . $uid, $bid);
     }
 
@@ -248,16 +289,17 @@ class ReadRecordService
      * @param $bid
      * @return bool|null|string
      */
-    public static function bid2BookName($bid){
+    public static function bid2BookName($bid)
+    {
         $book_name = null;
-        if(is_null($book_name)){
-            $book_key = 'wap:string:book:'.$bid;
+        if (is_null($book_name)) {
+            $book_key = 'wap:string:book:' . $bid;
             $book_name = Redis::get($book_key);
-            Redis::EXPIRE($book_key,3600);
-            if(!$book_name){
+            Redis::EXPIRE($book_key, 3600);
+            if (!$book_name) {
                 $book_name = '';
                 $book_info = BookConfigService::getBookById($bid);
-                if($book_info && isset($book_info->book_name)){
+                if ($book_info && isset($book_info->book_name)) {
                     $book_name = $book_info->book_name;
                 }
             }
@@ -270,16 +312,17 @@ class ReadRecordService
      * @param $cid
      * @return bool|null|string
      */
-    public static function cid2ChapterName($cid){
+    public static function cid2ChapterName($cid)
+    {
         $chapter_name = null;
-        if(is_null($chapter_name)){
-            $chapter_key = 'wap:string:chapter:'.$cid;
+        if (is_null($chapter_name)) {
+            $chapter_key = 'wap:string:chapter:' . $cid;
             $chapter_name = Redis::get($chapter_key);
-            Redis::EXPIRE($chapter_key,3600);
-            if(!$chapter_name){
+            Redis::EXPIRE($chapter_key, 3600);
+            if (!$chapter_name) {
                 $chapter_name = '';
                 $chapter_info = Chapter::getChapterNameById($cid);
-                if($chapter_info && isset($chapter_info->name)){
+                if ($chapter_info && isset($chapter_info->name)) {
                     $chapter_name = $chapter_info->name;
                 }
             }
@@ -292,16 +335,17 @@ class ReadRecordService
      * @param $uid
      * @param $record
      */
-    public static function delBookNameAndChapter($uid){
+    public static function delBookNameAndChapter($uid)
+    {
         //Redis::hset('book_base:' . $uid, 'last_read', "{$bid}_{$cid}_{$book_name}_{$chapter_name}_" . time()) ;
         $base_record = Redis::hget('book_base:' . $uid, 'last_read');
-        if($base_record){
-            $record_arr = explode('_',$base_record);
+        if ($base_record) {
+            $record_arr = explode('_', $base_record);
             $c = count($record_arr);
-            if($c>3){
+            if ($c > 3) {
                 $bid = $record_arr[0];
                 $cid = $record_arr[1];
-                $time = $record_arr[$c-1];
+                $time = $record_arr[$c - 1];
                 Redis::hset('book_base:' . $uid, 'last_read', "{$bid}_{$cid}_" . $time);
             }
         }
@@ -309,24 +353,25 @@ class ReadRecordService
         foreach ($records as $key => $v) {
             $record = explode('_', $v);
             $count = count($record);
-            if($count >3){
+            if ($count > 3) {
                 $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;
-                $chapter_key = 'wap:string:chapter:'.$latest_read_cid;
-                Redis::set($book_key,$book_name);
-                Redis::set($chapter_key,$chapter_name);
+                $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);
             }
         }
     }
 
-    public static function delBookBase($uid){
+    public static function delBookBase($uid)
+    {
         $base_record = Redis::hget('book_base:' . $uid, 'last_read');
-        if($base_record){
+        if ($base_record) {
             Redis::del('book_base:' . $uid);
             Redis::hset('book_read:' . $uid, 'last_read', $base_record);
         }
@@ -337,35 +382,34 @@ class ReadRecordService
      * @param int $uid
      * @return array
      */
-    public static function getSimpleReadRecord(int $uid):array
+    public static function getSimpleReadRecord(int $uid): array
     {
         $read_bids = Redis::hgetall('book_read:' . $uid);
         $res = [];
-        if(!$read_bids) {
+        if (!$read_bids) {
             return $res;
         }
         foreach ($read_bids as $key => $v) {
-            if(in_array($key,self::$not_uid_key)){
-            	continue;
+            if (in_array($key, self::$not_uid_key)) {
+                continue;
             }
-            array_push($res,$key);
+            array_push($res, $key);
         }
         return $res;
     }
 
 
-    public static function ReadRecordStatistical(int $uid,int $distribution_channel_id,string $from){
-        try{
+    public static function ReadRecordStatistical(int $uid, int $distribution_channel_id, string $from)
+    {
+        try {
             DB::table('temp_read_active')->insert([
-                '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'),
+                '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'),
             ]);
-        }catch (\Exception $e){
-
-        }
+        } catch (\Exception $e) { }
     }
 
     /**
@@ -373,14 +417,13 @@ class ReadRecordService
      * @param int $uid
      * @return int
      */
-    public static function getSendOrderId(int $uid){
-        try{
-            $send_order_id = Redis::hget('book_read:' . $uid,'send_order_id');
-            if($send_order_id)
-                return (int)$send_order_id;
-        }catch (\Exception $e){
-
-        }
+    public static function getSendOrderId(int $uid)
+    {
+        try {
+            $send_order_id = Redis::hget('book_read:' . $uid, 'send_order_id');
+            if ($send_order_id)
+                return (int) $send_order_id;
+        } catch (\Exception $e) { }
         return 0;
     }
 
@@ -389,10 +432,11 @@ class ReadRecordService
      * @param $uid
      * @param $inner_order_id
      */
-    public static function setInnerSendOrderId($uid,$inner_order_id){
-        try{
-            Redis::hset('book_read:' . $uid,'inner_send_order_id',$inner_order_id);
-        }catch (\Exception $e){}
+    public static function setInnerSendOrderId($uid, $inner_order_id)
+    {
+        try {
+            Redis::hset('book_read:' . $uid, 'inner_send_order_id', $inner_order_id);
+        } catch (\Exception $e) { }
     }
 
     /**
@@ -400,14 +444,15 @@ class ReadRecordService
      * @param $uid
      * @return string
      */
-    public static function getInnerSendOrderId($uid){
-        try{
-            $inner_send_order_id = Redis::hget('book_read:' . $uid,'inner_send_order_id');
-            if($inner_send_order_id){
+    public static function getInnerSendOrderId($uid)
+    {
+        try {
+            $inner_send_order_id = Redis::hget('book_read:' . $uid, 'inner_send_order_id');
+            if ($inner_send_order_id) {
                 return $inner_send_order_id;
             }
             return '';
-        }catch (\Exception $e){}
+        } catch (\Exception $e) { }
         return '';
     }
     /**
@@ -415,31 +460,34 @@ class ReadRecordService
      * @param int $uid
      * @return mixed
      */
-    public static function getSignDay(int $uid){
-        try{
-            return Redis::hget('book_read:' . $uid,'sign_day');
-        }catch (\Exception $e){}
+    public static function getSignDay(int $uid)
+    {
+        try {
+            return Redis::hget('book_read:' . $uid, 'sign_day');
+        } catch (\Exception $e) { }
         return -1;
     }
 
-    public static function setSignDay(int $uid){
-        return Redis::hset('book_read:' . $uid,'sign_day',date('Y-m-d'));
+    public static function setSignDay(int $uid)
+    {
+        return Redis::hset('book_read:' . $uid, 'sign_day', date('Y-m-d'));
     }
 
     /**
      * 签到次数和日期
      * @param int $uid
      */
-    public static function sign(int $uid,bool $is_incr){
-        try{
-            if($is_incr){
-                Redis::hincrby('book_read:' . $uid,'sign_counts',1);
-            }else{
-                self::setSignCount($uid,1);
+    public static function sign(int $uid, bool $is_incr)
+    {
+        try {
+            if ($is_incr) {
+                Redis::hincrby('book_read:' . $uid, 'sign_counts', 1);
+            } else {
+                self::setSignCount($uid, 1);
             }
             self::setSignDay($uid);
-        }catch (\Exception $e){
-        	\Log::info('sign_ept:'.$e->getMessage());
+        } catch (\Exception $e) {
+            \Log::info('sign_ept:' . $e->getMessage());
         }
         return;
     }
@@ -448,135 +496,140 @@ class ReadRecordService
      * @param int $uid
      * @return int
      */
-    public static function getSignCount(int $uid){
-        try{
-            $count = Redis::hget('book_read:' . $uid,'sign_counts');
-            if($count){
+    public static function getSignCount(int $uid)
+    {
+        try {
+            $count = Redis::hget('book_read:' . $uid, 'sign_counts');
+            if ($count) {
                 return $count;
             }
-        }catch (\Exception $e){
-
-        }
+        } catch (\Exception $e) { }
         return 0;
     }
 
     /**
- * 获取简单签到次数
- * @param int $uid
- * @return int
- */
-    public static function getSignCountSimple(int $uid){
-        try{
-            $count = Redis::hget('book_read:' . $uid,'sign_counts');
-            if($count){
-                return (int)$count;
+     * 获取简单签到次数
+     * @param int $uid
+     * @return int
+     */
+    public static function getSignCountSimple(int $uid)
+    {
+        try {
+            $count = Redis::hget('book_read:' . $uid, 'sign_counts');
+            if ($count) {
+                return (int) $count;
             }
             return 0;
-        }catch (\Exception $e){
-        }
+        } catch (\Exception $e) { }
         return 0;
     }
 
-    public static function setSignCount(int $uid,int $count){
-        Redis::hset('book_read:' . $uid,'sign_counts',$count);
+    public static function setSignCount(int $uid, int $count)
+    {
+        Redis::hset('book_read:' . $uid, 'sign_counts', $count);
     }
 
 
-    public static function setSignInfo(int $uid,string $info){
-        Redis::hset('book_read:' . $uid,'sign_info',$info);
+    public static function setSignInfo(int $uid, string $info)
+    {
+        Redis::hset('book_read:' . $uid, 'sign_info', $info);
     }
 
-    public static function setSmartPush($uid,$bid){
+    public static function setSmartPush($uid, $bid)
+    {
         $old = self::getSmartPush($uid);
-        if($old && !in_array($bid,$old)){
-            array_push($old,$bid);
-            $bid_str = implode(',',$old);
-            return Redis::hset('book_read:' . $uid,'smart_push',$bid_str);
-        }else{
-            return Redis::hset('book_read:' . $uid,'smart_push',$bid);
+        if ($old && !in_array($bid, $old)) {
+            array_push($old, $bid);
+            $bid_str = implode(',', $old);
+            return Redis::hset('book_read:' . $uid, 'smart_push', $bid_str);
+        } else {
+            return Redis::hset('book_read:' . $uid, 'smart_push', $bid);
         }
     }
 
-    public static function getSmartPush(int $uid):array{
-        $res =  Redis::hget('book_read:' . $uid,'smart_push');
-        if($res){
-            return explode(',',$res);
+    public static function getSmartPush(int $uid): array
+    {
+        $res =  Redis::hget('book_read:' . $uid, 'smart_push');
+        if ($res) {
+            return explode(',', $res);
         }
         return [];
     }
 
-    public static function getByField(int $uid,$field){
-        try{
-            return Redis::hget('book_read:' . $uid,$field);
-
-        }catch (\Exception $e){
-        }
+    public static function getByField(int $uid, $field)
+    {
+        try {
+            return Redis::hget('book_read:' . $uid, $field);
+        } catch (\Exception $e) { }
         return '';
     }
 
-    public static function getByMultiField(int $uid,...$field){
-        try{
-            return Redis::hmget('book_read:' . $uid,$field);
-        }catch (\Exception $e){
-        }
+    public static function getByMultiField(int $uid, ...$field)
+    {
+        try {
+            return Redis::hmget('book_read:' . $uid, $field);
+        } catch (\Exception $e) { }
         return '';
     }
 
-    public static function setByField(int $uid,$field,$value){
-        if(!in_array($field,self::$not_uid_key)){
+    public static function setByField(int $uid, $field, $value)
+    {
+        if (!in_array($field, self::$not_uid_key)) {
             return false;
         }
-        try{
-            return Redis::hset('book_read:' . $uid,$field,$value);
-        }catch (\Exception $e){}
+        try {
+            return Redis::hset('book_read:' . $uid, $field, $value);
+        } catch (\Exception $e) { }
         return '';
     }
 
-    public static function setByMultiField(int $uid,$kv){
+    public static function setByMultiField(int $uid, $kv)
+    {
         $keys = array_keys($kv);
-        foreach ($keys as $field){
-            if(!in_array($field,self::$not_uid_key)){
+        foreach ($keys as $field) {
+            if (!in_array($field, self::$not_uid_key)) {
                 return false;
             }
         }
-        try{
-            return Redis::hmset('book_read:' . $uid,$kv);
-        }catch (\Exception $e){}
+        try {
+            return Redis::hmset('book_read:' . $uid, $kv);
+        } catch (\Exception $e) { }
         return '';
     }
 
 
     private static function resetRecordFromDB($uid)
     {
-        if(self::getByField($uid,'last_read')){
-            return ;
-        }
-        $record = ReadRecordFromRedis::where('uid',$uid)->select('field','value')->get();
-        if($record->isNotEmpty()){
-            foreach ($record as $item){
-                if(!in_array($item->field,self::$not_uid_key) || $item->field == 'last_read'){
-                    Redis::hset('book_read:'.$uid,$item->field,$item->value);
+        if (self::getByField($uid, 'last_read')) {
+            return;
+        }
+        $record = ReadRecordFromRedis::where('uid', $uid)->select('field', 'value')->get();
+        if ($record->isNotEmpty()) {
+            foreach ($record as $item) {
+                if (!in_array($item->field, self::$not_uid_key) || $item->field == 'last_read') {
+                    Redis::hset('book_read:' . $uid, $item->field, $item->value);
                 }
             }
         }
     }
 
     //删除多余的阅读纪律
-    public static function delTheLastRecord($uid){
-        $length = Redis::hlen('book_read:'.$uid);
-        if($length <= self::RECORD_COUNT+count(self::$not_uid_key)){
-            return ;
+    public static function delTheLastRecord($uid)
+    {
+        $length = Redis::hlen('book_read:' . $uid);
+        if ($length <= self::RECORD_COUNT + count(self::$not_uid_key)) {
+            return;
         }
         $read_bids = Redis::hgetall('book_read:' . $uid);
         $i = 0;
         foreach ($read_bids as $key => $v) {
-            if(in_array($key,self::$not_uid_key)){
+            if (in_array($key, self::$not_uid_key)) {
                 continue;
             }
             $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;
@@ -584,10 +637,10 @@ class ReadRecordService
         });
 
         $j = 0;
-        foreach ($res as $v){
-            if($j++ >=self::RECORD_COUNT){
-                Redis::hdel('book_read:'.$uid,$v['bid']);
+        foreach ($res as $v) {
+            if ($j++ >= self::RECORD_COUNT) {
+                Redis::hdel('book_read:' . $uid, $v['bid']);
             }
         }
     }
-}
+}