wangzq 2 年之前
父节点
当前提交
66d54d46f8

+ 44 - 0
app/Http/Controllers/QuickApp/Book/BookController.php

@@ -1236,4 +1236,48 @@ class BookController extends BaseController
         return response()->pagination(new BookTransformer,$books);
     }
 
+    /**
+     * 首页
+     */
+    public function getBookListsNew(Request $request, $sex)
+    {
+        // 获取基本数据
+        $package     = $request->header('x-package', '');
+        $brand       = $request->header('x-nbrand', '');
+        $codeVersion = $request->header('x-codeversion', '');
+        $isAuth = check_qapp_auth($package ,0);
+
+        //新判断: 根据包名来获取对应所需的bid
+        $qapp_package = QappPackage::getPackageByPackage($package);
+        if($qapp_package){
+            $package_id = $qapp_package->id;
+        }else{
+            $package_id = 0;
+        }
+        $channel  = $sex == 'male' ? 1 : 2;
+
+        $result = $this->getRecommendBooks($package_id,$package,$channel, $isAuth);
+
+        return response()->success($result);
+
+    }
+
+    private function getRecommendBooks($package_id,$package, $channel, $is_auth)
+    {
+
+        $bids = BookConfigService::getRecommendBids($package,$channel,$is_auth,[26],64);
+        // $hotBids   = BookConfigService::getCheckBooks(QappRecommendService::getRecommendByPacketId($channel, 'hot',$package_id),$channel,$package,$isAuthor);
+        // $liveBids  = BookConfigService::getCheckBooks(QappRecommendService::getRecommendByPacketId($channel, 'live',$package_id),$channel,$package,$isAuthor);
+        // $recomBids = BookConfigService::getCheckBooks(QappRecommendService::getRecommendByPacketId($channel, 'recom',$package_id),$channel,$package,$isAuthor);
+        // $newBids   = BookConfigService::getCheckBooks(QappRecommendService::getRecommendByPacketId($channel, 'new_recom',$package_id),$channel,$package,$isAuthor);
+        // myLog("Qapp_home_data")->info(['liveBids' =>$liveBids,'hotbids'=>$hotBids,'new' => $newBids,'recom'=> $recomBids]);
+        return array_filter([
+            ['type' => 'hot', 'lable' => '热门书单', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($hotBids))],
+            ['type' => 'zhibo', 'lable' => '神书直播', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($liveBids))],
+            ['type' => 'recom', 'lable' => '小编精选', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($recomBids))],
+            ['type' => 'new_recom', 'lable' => '人气新书', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($newBids))]
+        ]);
+        return [];
+    }
+
 }

+ 7 - 0
app/Http/Routes/QuickApp/QuickAppRoutes.php

@@ -24,6 +24,10 @@ Route::group(['domain' => env('QUICKAPP_DOMAIN'), 'namespace' => 'App\Http\Contr
         Route::middleware(['userrefresh'])->group(function (){
             // 首页
             Route::get('books/{sex}/index', 'Book\BookController@getBookLists');
+
+            // 首页推荐位
+            Route::get('books/{sex}/index_recom', 'Book\BookController@getBookListsNew');
+
         });
 
         // 书库
@@ -199,6 +203,9 @@ Route::group(['domain' => env('QUICKAPP_BACK_DOMAIN'), 'namespace' => 'App\Http\
         // 首页
         Route::get('books/{sex}/index', 'Book\BookController@getBookLists');
 
+        // 首页推荐位
+        Route::get('books/{sex}/index_recom', 'Book\BookController@getBookListsNew');
+
         // 书库
         Route::get('books/library', 'Book\BookController@library');
 

+ 93 - 64
app/Modules/Book/Services/BookConfigService.php

@@ -32,7 +32,7 @@ class BookConfigService
      */
     public static function getBookById($bid)
     {
-        return  BookConfig::getBookById($bid);
+        return BookConfig::getBookById($bid);
     }
 
 
@@ -43,8 +43,8 @@ class BookConfigService
      */
     public static function getCombinationInfo($bid)
     {
-        $info = BookCombinationConfig::where('bid',$bid)->where('is_enable',1)->select('bid_group')->first();
-        if($info) return explode(',',$info->bid_group);
+        $info = BookCombinationConfig::where('bid', $bid)->where('is_enable', 1)->select('bid_group')->first();
+        if ($info) return explode(',', $info->bid_group);
         return [];
     }
 
@@ -54,27 +54,27 @@ class BookConfigService
      * @param null $order
      * @return mixed
      */
-    public static function getBooksByIds(array $where, $order = [],$is_external_shelf=true)
+    public static function getBooksByIds(array $where, $order = [], $is_external_shelf = true)
     {
         if (empty($where)) {
             return (object)array();
         }
         if ($order)
-            $res = BookConfig::getBooksByIds($where,$order,$is_external_shelf);
+            $res = BookConfig::getBooksByIds($where, $order, $is_external_shelf);
         else
-            $res = BookConfig::getBooksByIds($where,[],$is_external_shelf);
+            $res = BookConfig::getBooksByIds($where, [], $is_external_shelf);
         return $res;
     }
 
-    public static function getBookLists(array $where, $order = [],$is_external_shelf=true)
+    public static function getBookLists(array $where, $order = [], $is_external_shelf = true)
     {
         if (empty($where)) {
             return (object)array();
         }
         if ($order) {
-            $res = BookConfig::getBookLists($where,$order,$is_external_shelf);
-        } else{
-            $res = BookConfig::getBookLists($where,[],$is_external_shelf);
+            $res = BookConfig::getBookLists($where, $order, $is_external_shelf);
+        } else {
+            $res = BookConfig::getBookLists($where, [], $is_external_shelf);
         }
 
         return $res;
@@ -94,7 +94,6 @@ class BookConfigService
     }
 
 
-
     /**
      * 获取阅读完的推荐
      * @param $category_id
@@ -127,7 +126,6 @@ class BookConfigService
     }
 
 
-
     public static function findBookKeywords(bool $is_all = false)
     {
         $sql = BookKeyword::where('status', 1)->orderBy('sequence');
@@ -164,10 +162,10 @@ class BookConfigService
     public static function findFreeBooks(int $sex)
     {
         $config = self::findFreeBookConfig($sex);
-        \Log::info('return_empty_free_books:'.$sex);
+        \Log::info('return_empty_free_books:' . $sex);
         \Log::info($config);
         if ($config) {
-            $free_books =  FreeBook::where('config_id', $config->id)
+            $free_books = FreeBook::where('config_id', $config->id)
                 ->where('is_enabled', 1)
                 ->get();
             $bids = $free_books->pluck('bid')->all();
@@ -215,31 +213,34 @@ class BookConfigService
     }
 
 
-    public static function getByBidNoFilter($bid){
-        return   FreeBook::join('free_book_config','free_book_config.id','=','free_books.config_id')
-            ->where('bid',$bid)
-            ->select('free_books.id','end_time')
-            ->where('end_time','<',date('Y-m-d H:i:s'))
-            ->orderBy('free_book_config.end_time','desc')
+    public static function getByBidNoFilter($bid)
+    {
+        return FreeBook::join('free_book_config', 'free_book_config.id', '=', 'free_books.config_id')
+            ->where('bid', $bid)
+            ->select('free_books.id', 'end_time')
+            ->where('end_time', '<', date('Y-m-d H:i:s'))
+            ->orderBy('free_book_config.end_time', 'desc')
             ->first();
     }
 
-    public static function chargeStats($id,$amount,$uid){
-        if(!Redis::Sismember('qapp:free:virtual:uids'.$id,$uid)){
-            return ;
+    public static function chargeStats($id, $amount, $uid)
+    {
+        if (!Redis::Sismember('qapp:free:virtual:uids' . $id, $uid)) {
+            return;
         }
         $now = date('Y-m-d');
-        $amount = $amount*100;
-        Redis::hincrby('qapp:book:free:charge:'.$id,$now,$amount);
+        $amount = $amount * 100;
+        Redis::hincrby('qapp:book:free:charge:' . $id, $now, $amount);
         #Redis::sadd('qapp:free:charge'.$now,$id);
         Redis::sadd('qapp:free:actuality' . $now, $id);
-        Redis::sadd('qapp:free:charge:uids'.$now.$id,$uid);
+        Redis::sadd('qapp:free:charge:uids' . $now . $id, $uid);
     }
 
-    public static function getBookByField($bids,$field){
-        if(!$bids || !$field) return null;
-        return BookConfig::join('books','books.id','=','book_configs.bid')->
-        whereIn('bid',$bids)->select($field)->get();
+    public static function getBookByField($bids, $field)
+    {
+        if (!$bids || !$field) return null;
+        return BookConfig::join('books', 'books.id', '=', 'book_configs.bid')->
+        whereIn('bid', $bids)->select($field)->get();
     }
 
     /**
@@ -251,22 +252,22 @@ class BookConfigService
      * @return array
      * date 2022/09/20 10:39
      */
-    public static function  getAvailableBIdsbyBids($bids,$channel_id=0,$is_external_shelf=true)
+    public static function getAvailableBIdsbyBids($bids, $channel_id = 0, $is_external_shelf = true)
     {
-        if (empty($bids)){
+        if (empty($bids)) {
             return [];
         }
-        $res = BookConfig::whereIn('bid',$bids);
-//        if($channel_id == config('qapp_public_package_channel')){
-//            $res->whereNotIn('cp_source',getHiddenCp());
-//        }else{
-//            $res->whereNotIn('cp_source',array_merge(getHiddenCp(),['lianshang']));
-//        }
-        $res->whereNotIn('cp_source',getHiddenCp());
-        if ($is_external_shelf){
-            $res->where('is_on_shelf',2);
-        }else{
-            $res->whereIn('is_on_shelf',[1,2]);
+        $res = BookConfig::whereIn('bid', $bids);
+        //        if($channel_id == config('qapp_public_package_channel')){
+        //            $res->whereNotIn('cp_source',getHiddenCp());
+        //        }else{
+        //            $res->whereNotIn('cp_source',array_merge(getHiddenCp(),['lianshang']));
+        //        }
+        $res->whereNotIn('cp_source', getHiddenCp());
+        if ($is_external_shelf) {
+            $res->where('is_on_shelf', 2);
+        } else {
+            $res->whereIn('is_on_shelf', [1, 2]);
         }
 
         return $res->pluck("bid")->toArray();
@@ -282,52 +283,80 @@ class BookConfigService
      * @return mixed
      * date 2022/10/26 10:11
      */
-    public  static  function getCheckBooks($bid_list,$channel,$package,$is_author)
+    public static function getCheckBooks($bid_list, $channel, $package, $is_author)
     {
         $hidden_cp = getHiddenCp();
-//        if(!is_public_package($package)){
-//            $hidden_cp = array_merge($hidden_cp,['lianshang']);
-//        }
+        //        if(!is_public_package($package)){
+        //            $hidden_cp = array_merge($hidden_cp,['lianshang']);
+        //        }
         //获取书本数量
         $count = count($bid_list);
-        if (!$is_author){
+        if (!$is_author) {
             $where = [
-                ['book_configs.charge_type','!=','BOOK'],
-                ['book_configs.cp_source','=','ycsd'],
+                ['book_configs.charge_type', '!=', 'BOOK'],
+                ['book_configs.cp_source', '=', 'ycsd'],
             ];
-        }else{
+        } else {
             $where = [
-                ['book_configs.charge_type','!=','BOOK'],
+                ['book_configs.charge_type', '!=', 'BOOK'],
             ];
         }
 
         //获取书籍交集bid,过滤掉不符合要求的书
         $bid_list = BookConfig::join('books', 'book_configs.bid', '=', 'books.id')
             ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
-            ->whereIn('book_configs.bid',$bid_list)
-            ->where('book_configs.is_on_shelf',2)
+            ->whereIn('book_configs.bid', $bid_list)
+            ->where('book_configs.is_on_shelf', 2)
             ->where($where)
-            ->whereNotIn('book_configs.cp_source',$hidden_cp)
-            ->where('book_categories.pid',$channel)
+            ->whereNotIn('book_configs.cp_source', $hidden_cp)
+            ->where('book_categories.pid', $channel)
             ->pluck('book_configs.bid')->all();
 
         $book_count = count($bid_list);
-        if ($book_count === $count){
-            return  $bid_list;
+        if ($book_count === $count) {
+            return $bid_list;
         }
         $supplement_count = $count - $book_count;
         //获取随机的有效的书籍bid
         $rand_bid = BookConfig::join('books', 'book_configs.bid', '=', 'books.id')
             ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
-            ->where('book_configs.is_on_shelf',2)
+            ->where('book_configs.is_on_shelf', 2)
             ->where($where)
-            ->whereNotIn('book_configs.cp_source',$hidden_cp)
+            ->whereNotIn('book_configs.cp_source', $hidden_cp)
             // ->where('book_configs.is_high_quality',1)
-            ->where('book_categories.pid',$channel)
-            ->orderBy('book_configs.is_high_quality','desc')
+            ->where('book_categories.pid', $channel)
+            ->orderBy('book_configs.is_high_quality', 'desc')
             ->inRandomOrder()
             ->limit($supplement_count)
-             ->pluck('book_configs.bid')->all();
-        return array_filter(array_merge($bid_list,$rand_bid));
+            ->pluck('book_configs.bid')->all();
+        return array_filter(array_merge($bid_list, $rand_bid));
+    }
+
+    public static function getRecommendBids($package = '', $channel = 1, $is_auth = true, $no_in_bid = [], $limit = 64)
+    {
+        $where = [
+            ['book_configs.charge_type', '!=', 'BOOK'],
+        ];
+        if (!$is_auth) {
+            $where[] = ['book_configs.cp_source', '=', 'ycsd'];
+        }
+
+        //获取书籍交集bid,过滤掉不符合要求的书
+        $bid_list = BookConfig::join('books', 'book_configs.bid', '=', 'books.id')
+            ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
+            ->where('book_configs.is_on_shelf', 2)
+            ->where($where)
+            ->whereNotIn('book_configs.cp_source', getHiddenCp())
+            ->where('book_categories.pid', $channel);
+        if (!empty($no_in_bid)) {
+             $bid_list->whereNotIn('book_configs.bid', $no_in_bid);
+        }
+
+         $bid_list = $bid_list->orderBy('recommend_index' ,  'desc')->limit($limit)->pluck('book_configs.bid');
+
+        echo "<pre><hr>";
+        var_export($bid_list);
+        echo "<hr>";
+        die();
     }
 }