fly 5 年 前
コミット
9df9ac9c44

+ 57 - 4
app/Http/Controllers/QuickApp/Book/BookController.php

@@ -12,8 +12,8 @@ use App\Modules\Book\Services\BookService;
 use App\Modules\Book\Services\BookUrgeUpdateService;
 use App\Modules\Book\Services\UserShelfBooksService;
 use App\Modules\Book\Services\ChapterService;
+use App\Modules\Subscribe\Services\ChapterOrderService;
 use Hashids;
-use App\Modules\Subscribe\Services\YearOrderService;
 use Log;
 
 class BookController extends BaseController
@@ -105,19 +105,72 @@ class BookController extends BaseController
             return response()->error('QAPP_OFF  _SHELF');
         }
         $is_on_shelf = UserShelfBooksService::getUserShelfBooksListByUidAndBid($this->uid, $bid);
-
         $book_info['is_on_user_shelf'] = 0;
 
         if ($is_on_shelf) {
             $book_info['is_on_user_shelf'] = 1;
         }
-
         $last_chapter = ChapterService::getChapterNameById($book_info['last_cid'], $bid);
-
         $book_info['last_chapter_is_vip'] = $last_chapter['is_vip'];
+        $book_info['is_need_charge'] = $this->isNeedCharge($bid, $last_chapter, $book_info);
         return response()->item(new BookTransformer(), $book_info);
     }
 
+    /**
+     * 判断是否需要充值
+     */
+    private function isBookNeedCharge(int $bid, float $price)
+    {
+        $book_order = BookOrderService::getRecordByuidBid($this->uid, $bid);
+        if ($book_order) {
+            return false;
+        } else {
+            $user_info = $this->user_info;
+            return $user_info['balance'] < $price;
+        }
+    }
+
+    /**
+     * 判断章节是否需要充值
+     */
+    private function isChapterNeedCharge(int $bid, int $cid, float $price)
+    {
+        $chapter_order = ChapterOrderService::checkIsOrderedStatic($this->uid, $bid, $cid);
+        if ($chapter_order) {
+            return false;
+        } else {
+            $user_info = $this->user_info;
+            return $user_info['balance'] < $price;
+        }
+    }
+
+    /**
+     * 判断是否需要充值
+     */
+    private function isNeedCharge(int $bid, $last_chapter, $book_info)
+    {
+        switch ($book_info->charge_type) {
+            case 'BOOK':
+                $price = $this->getPrice($book_info);
+                return $this->isBookNeedCharge($bid, $price);
+            default:
+                $price = $last_chapter->is_vip ? $this->getPrice($book_info, $last_chapter->size) : 0;
+                return  $last_chapter->is_vip ? $this->isChapterNeedCharge($bid, $last_chapter->id, $price) : false;
+        }
+    }
+
+    /**
+     * 计算价格
+     * @param $book_info
+     * @param $chapter_size
+     * @return float
+     */
+    protected function getPrice($book_info, $chapter_size = 0)
+    {
+        if ($book_info->charge_type == 'BOOK')
+            return $book_info->price * 100;
+        return   ceil($chapter_size / 100);
+    }
 
     /**
      * @apiVersion 1.0.0

+ 35 - 31
app/Http/Controllers/QuickApp/Book/Transformers/BookTransformer.php

@@ -1,4 +1,5 @@
 <?php
+
 /**
  * Created by PhpStorm.
  * Date: 2017/3/31
@@ -6,40 +7,43 @@
  */
 
 namespace App\Http\Controllers\QuickApp\Book\Transformers;
+
 use Hashids;
+
 class BookTransformer
 {
-    public function transform($book){
+    public function transform($book)
+    {
         return [
-            'book_id'=>Hashids::encode($book->bid),
-            //'book_id_no_hash'=>$book->bid,
-            'book_name'=>$book->book_name,
-            'book_summary'=>$book->intro,
-            'book_author'=>$book->author,
-            'cover_url'=>$book->cover,
-            'book_word_count'=>$book->size,
-            'book_chapter_total'=>$book->chapter_count,
-            'book_category_id'=>$book->category_id,
-            'book_category'=>$book->category_name,
-            'book_end_status'=>$book->status,
-            'book_published_time'=>is_null($book->updated_at)?'':$book->updated_at,
-            'copyright'=>is_null($book->copyright)? '':$book->copyright,
-            'charge_type'=>is_null($book->charge_type)?'':$book->charge_type,
-            'force_subscribe_chapter_id'=>$book->force_subscribe_chapter_seq,
-            'update_time'=>$book->updated_at,
-            'is_on_shelf'=>$book->is_on_shelf,
-            'book_price'=>is_null($book->price)?0:$book->price,
-            'keyword'=>$book->keyword,
-            'recommend_index'=> $book->recommend_index,
-            'is_show_index_content'=>$book->is_show_index_content,
-            'click_count'=>$book->click_count,
-            'product_id'=>$book->product_id,
-            'sex_preference'=>$book->channel_name,
-            'last_cid'=>$book->last_cid,
-            'last_chapter'=>$book->last_chapter,
-            'first_cid'=>$book->first_cid,
-            'is_on_user_shelf'=>$book->is_on_user_shelf,
-            'last_chapter_is_vip'=>$book->last_chapter_is_vip
+            'book_id' => Hashids::encode($book->bid),
+            'book_name' => $book->book_name,
+            'book_summary' => $book->intro,
+            'book_author' => $book->author,
+            'cover_url' => $book->cover,
+            'book_word_count' => $book->size,
+            'book_chapter_total' => $book->chapter_count,
+            'book_category_id' => $book->category_id,
+            'book_category' => $book->category_name,
+            'book_end_status' => $book->status,
+            'book_published_time' => is_null($book->updated_at) ? '' : $book->updated_at,
+            'copyright' => is_null($book->copyright) ? '' : $book->copyright,
+            'charge_type' => is_null($book->charge_type) ? '' : $book->charge_type,
+            'force_subscribe_chapter_id' => $book->force_subscribe_chapter_seq,
+            'update_time' => $book->updated_at,
+            'is_on_shelf' => $book->is_on_shelf,
+            'book_price' => is_null($book->price) ? 0 : $book->price,
+            'keyword' => $book->keyword,
+            'recommend_index' => $book->recommend_index,
+            'is_show_index_content' => $book->is_show_index_content,
+            'click_count' => $book->click_count,
+            'product_id' => $book->product_id,
+            'sex_preference' => $book->channel_name,
+            'last_cid' => $book->last_cid,
+            'last_chapter' => $book->last_chapter,
+            'first_cid' => $book->first_cid,
+            'is_on_user_shelf' => $book->is_on_user_shelf,
+            'last_chapter_is_vip' => $book->last_chapter_is_vip,
+            'is_need_charge' => $book->is_need_charge ? 1 : 0,
         ];
     }
-}
+}