fly преди 5 години
родител
ревизия
6624cf0501
променени са 1 файла, в които са добавени 51 реда и са изтрити 59 реда
  1. 51 59
      app/Http/Controllers/QuickApp/Book/ChapterController.php

+ 51 - 59
app/Http/Controllers/QuickApp/Book/ChapterController.php

@@ -46,7 +46,7 @@ class ChapterController extends BaseController
      * @apiSuccess {Int}         data.list.prev_cid   上一章节id
      * @apiSuccess {Int}         data.list.next_cid   下一章节
      * @apiSuccess {String}      data.list.recent_update_at   更新时间
-     * @apiSuccess {String}      data.list.is_need_subscirbe   是否强制关注
+     * @apiSuccess {String}      data.list.is_need_charge  是否需要充值
      * @apiSuccess {object}      data.meta   分页信息
      * @apiSuccess {Int}         data.meta.total  总条数
      * @apiSuccess {Int}         data.meta.per_page  每页条数
@@ -71,7 +71,7 @@ class ChapterController extends BaseController
      *               prev_cid: 0,
      *               next_cid: 0,
      *               recent_update_at: 2017-11-20 15:01:56,
-     *               is_need_subscirbe: 1,
+     *               is_need_charge: 1,
      *            },
      *             {
      *               bid: 5,
@@ -83,7 +83,7 @@ class ChapterController extends BaseController
      *               prev_cid: 0,
      *               next_cid: 0,
      *               recent_update_at: 2017-11-20 15:01:56,
-     *               is_need_subscirbe: 1,
+     *               is_need_charge: 1,
      *            },
      *          ]
      *       }
@@ -96,21 +96,7 @@ class ChapterController extends BaseController
         if (!$book_info) {
             return response()->error('PARAM_ERROR');
         }
-        $this->book_info = $book_info;
-        $is_show_price = $this->showChapterPrice($bid, $book_info->charge_type);
-        foreach ($lists as $v) {
-            $v->is_show_price = $is_show_price;
-            $v->price = '';
-            if ($is_show_price) {
-                $v->price = $this->getPrice($book_info, $v->size);
-            }
-
-            if ($v->sequence == $book_info->force_subscribe_chapter_seq) {
-                $v->is_need_subscirb = 1;
-            } else {
-                $v->is_need_subscirb = 0;
-            }
-        }
+        $lists = $this->getChapterCatalog($bid, $lists, $book_info);
         return response()->collection(new ChapterListTransformer, $lists);
     }
 
@@ -137,7 +123,7 @@ class ChapterController extends BaseController
      * @apiSuccess {Int}         data.list.prev_cid   上一章节id
      * @apiSuccess {Int}         data.list.next_cid   下一章节
      * @apiSuccess {String}      data.list.recent_update_at   更新时间
-     * @apiSuccess {String}      data.list.is_need_subscirbe   是否强制关注
+     * @apiSuccess {String}      data.list.is_need_charge  是否需要充值
      * @apiSuccess {object}      data.meta   分页信息
      * @apiSuccess {Int}         data.meta.total  总条数
      * @apiSuccess {Int}         data.meta.per_page  每页条数
@@ -162,7 +148,7 @@ class ChapterController extends BaseController
      *               prev_cid: 0,
      *               next_cid: 0,
      *               recent_update_at: 2017-11-20 15:01:56,
-     *               is_need_subscirbe: 1,
+     *               is_need_charge: 1,
      *            },
      *             {
      *               bid: 5,
@@ -174,7 +160,7 @@ class ChapterController extends BaseController
      *               prev_cid: 0,
      *               next_cid: 0,
      *               recent_update_at: 2017-11-20 15:01:56,
-     *               is_need_subscirbe: 1,
+     *               is_need_charge: 1,
      *            },
      *          ]
      *          meta:{
@@ -194,26 +180,33 @@ class ChapterController extends BaseController
         if (!$book_info) {
             return response()->error('PARAM_ERROR');
         }
-        $this->book_info = $book_info;
         $page_size = $request->input('page_size', 15);
         if ($page_size >= 100) $page_size = 100;
         $res = ChapterService::getChapterListsPage($bid, $page_size);
-        $is_show_price = $this->showChapterPrice($bid, $book_info->charge_type);
-        foreach ($res as $v) {
-            $v->is_show_price = $is_show_price;
-            $v->price = '';
-            if ($is_show_price) {
-                $v->price = $this->getPrice($book_info, $v->size);
-            }
-            if ($v->sequence == $book_info->force_subscribe_chapter_seq) {
-                $v->is_need_subscirb = 1;
-            } else {
-                $v->is_need_subscirb = 0;
-            }
-        }
-        return response()->pagination(new ChapterListTransformer, $res);
+        $lists = $this->getChapterCatalog($bid, $res, $book_info);
+        return response()->pagination(new ChapterListTransformer, $lists);
     }
 
+    private function getChapterCatalog(int $bid, $chapters, $book_info)
+    {
+        switch ($book_info->charge_type) {
+            case 'BOOK':
+                $price = $this->getPrice($book_info);
+                $is_need_charge = $this->isBookNeedCharge($bid, $price);
+                foreach ($chapters as $v) {
+                    $v->is_need_charge = $is_need_charge;
+                    $v->price = $price;
+                }
+                break;
+            default:
+                foreach ($chapters as $v) {
+                    $v->is_need_charge = $this->isChapterNeedCharge($bid, $v->id, $price);
+                    $v->price = $this->getPrice($book_info, $v->size);
+                }
+                break;
+        }
+        return $chapters;
+    }
 
 
     /**
@@ -236,7 +229,6 @@ class ChapterController extends BaseController
      * @apiSuccess {Int}         data.next_cid   下一章节
      * @apiSuccess {String}      data.recent_update_at   更新时间
      * @apiSuccess {String}      data.chapter_content  章节内容
-     * @apiSuccess {Int}         data.is_need_subscirbe  是否强制关注(删除)
      * @apiSuccessExample {json} Success-Response:
      *     HTTP/1.1 200 OK
      *     {
@@ -360,7 +352,6 @@ class ChapterController extends BaseController
      * @apiSuccess {Int}         data.next_cid   下一章节
      * @apiSuccess {String}      data.recent_update_at   更新时间
      * @apiSuccess {String}      data.chapter_content  章节内容
-     * @apiSuccess {Int}         data.is_need_subscirbe  是否强制关注(删除)
      * @apiSuccessExample {json} Success-Response:
      *     HTTP/1.1 200 OK
      *     {
@@ -584,7 +575,7 @@ class ChapterController extends BaseController
      * @param $chapter_size
      * @return float
      */
-    protected function getPrice($book_info, $chapter_size)
+    protected function getPrice($book_info, $chapter_size = 0)
     {
         if ($book_info->charge_type == 'BOOK')
             return $book_info->price * 100;
@@ -628,30 +619,31 @@ class ChapterController extends BaseController
         return false;
     }
 
-    private function showChapterPrice($bid, $charge_type)
+    /**
+     * 判断是否需要充值
+     */
+    private function isBookNeedCharge(int $bid, float $price)
     {
-        if ($charge_type == 'BOOK') {
-            return false;
-        }
-        $show_chapter_price_bid = env('SHOW_CHAPTER_PRICE_BID', 'all');
-        if ($show_chapter_price_bid != 'all') {
-            $show_chapter_price_bid_array = explode(',', $show_chapter_price_bid);
-            if (!in_array($bid, $show_chapter_price_bid_array)) {
-                return false;
-            }
-        }
-        $show_chapter_price_channel = env('SHOW_CHAPTER_PRICE_CHANNEL', '');
-        if (!$show_chapter_price_channel) {
+        $book_order = BookOrderService::getRecordByuidBid($this->uid, $bid);
+        if ($book_order) {
             return false;
+        } else {
+            $user_info = $this->user_info;
+            return $user_info['balance'] < $price;
         }
+    }
 
-        if ($show_chapter_price_channel == 'all') {
-            return true;
-        }
-        $show_chapter_price_channel_array = explode(',', $show_chapter_price_channel);
-        if (in_array($this->distribution_channel_id, $show_chapter_price_channel_array)) {
-            return true;
+    /**
+     * 判断章节是否需要充值
+     */
+    private function isChapterNeedCharge(int $bid, int $cid, float $price)
+    {
+        $chapter_order = ChapterOrderService::checkIsOrdered($this->uid, $bid, $cid);
+        if ($chapter_order) {
+            return false;
+        } else {
+            $user_info = $this->user_info;
+            return $user_info['balance'] < $price;
         }
-        return false;
     }
 }