123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326 |
- <?php
- namespace App\Http\Controllers\QuickApp\Book;
- use App\Modules\RecommendBook\Services\RecommendService;
- use App\Modules\Book\Services\RecoBannerService;
- use Illuminate\Http\Request;
- use App\Http\Controllers\QuickApp\BaseController;
- use App\Http\Controllers\QuickApp\Book\Transformers\BookTransformer;
- use App\Http\Controllers\QuickApp\Book\Transformers\KeywordTransformer;
- use App\Modules\Book\Services\BookConfigService;
- 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\BookOrderService;
- use App\Modules\Subscribe\Services\ChapterOrderService;
- use App\Modules\Subscribe\Services\YearOrderService;
- use App\Modules\User\Services\ReadRecordService;
- class BookController extends BaseController
- {
- public function index(Request $request, $bid)
- {
- $bid = BookService::decodeBidStatic($bid);
- $book_info = BookConfigService::getBookById($bid);
- if (!$book_info) {
- return response()->error('QAPP_SYS_ERROR');
- }
- if (!in_array($book_info->is_on_shelf, [2])) {
- 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);
- $record = ReadRecordService::getBookReadRecordStatic($this->uid, $bid);
- if ($record) {
- $book_info['record_chapter_id'] = $record['record_chapter_id'];
- $book_info['record_chapter_name'] = $record['record_chapter_name'];
- }
- return response()->item(new BookTransformer(), $book_info);
- }
- /**
- * 获取订购记录
- * @param $book_info
- * @param $chapter_id
- * @return bool
- */
- protected function getOrderRecord($bid, $chapter_id)
- {
- //包年记录
- $uid = $this->uid;
- $res = YearOrderService::getRecord($uid);
- if ($res) return true;
- $res = null;
- //单本订购记录
- $res = BookOrderService::getRecordByuidBid($uid, $bid);
- if ($res) return true;
- $res = null;
- //章节订购记录
- $chapterOrder = new ChapterOrderService();
- if ($chapterOrder->checkIsOrdered($uid, $bid, $chapter_id)) return true;
- return false;
- }
- /**
- * 判断是否需要充值
- */
- private function isBookNeedCharge(int $bid, float $price)
- {
- $book_order = $this->getOrderRecord($bid, 0);
- 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)
- {
- $book_order = $this->getOrderRecord($bid, $cid);
- if ($book_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);
- }
- /**
- * 首页
- */
- public function getBookLists(Request $request, $sex)
- {
- if ($sex == 'male') {
- $channel = 1;
- $reco_banner_type = ['MALE', 'PUBLIC'];
- } else {
- $reco_banner_type = ['FEMALE', 'PUBLIC'];
- $channel = 2;
- }
- $books = (new RecoBannerService)->getByType($reco_banner_type, 2);
- $books->transform(function ($item) {
- $result = $this->getBidCidFromUrl($item->redirect_url);
- $item->bid = $result['bid'];
- $item->cid = $result['cid'];
- if ($result['cid']) {
- $item->redirect_url = "views/Reader";
- } else {
- $item->redirect_url = "views/Detail";
- }
- return $item;
- });
- $package = $request->header('x-package', '');
- $checkOpen = env('CHECK_OPEN', false);
- if ($checkOpen && $package === 'com.juyu.kuaiying.rmyq') {
- $hotBids = config('home.hot.' . $sex);
- $liveBids = config('home.live.' . $sex);
- $recomBids = config('home.recom.' . $sex);
- $newBids = config('home.new_recom.' . $sex);
- } else {
- $hotBids = RecommendService::getRecommendIdsStatic($channel, 'hot');
- $liveBids = RecommendService::getRecommendIdsStatic($channel, 'live');
- $recomBids = RecommendService::getRecommendIdsStatic($channel, 'recom');
- $newBids = RecommendService::getRecommendIdsStatic($channel, 'new_recom');
- }
- $result = [
- ['type' => 'reco_banner', 'lable' => '首页banner', 'books' => $books],
- ['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 response()->success($result);
- }
- private function getBidCidFromUrl(string $url)
- {
- if (preg_match('/\?bid=(\w+)\S+cid=(\w+)/', $url, $matches) || preg_match('/\?id=(\w+)/', $url, $matches)) {
- return [
- 'bid' => $matches[1],
- 'cid' => isset($matches[2]) ? $matches[2] : 0,
- ];
- } else {
- return [
- 'bid' => '',
- 'cid' => 0,
- ];
- }
- }
- public function library(Request $request)
- {
- $where = [];
- $order = [];
- $where['is_on_shelf'] = [2];
- $category_id = $request->input('category_id');
- if ($category_id) {
- if ($category_id == 1) {
- $where['channel_name'] = '男频';
- } elseif ($category_id == 2) {
- $where['channel_name'] = '女频';
- } else {
- $where['category_id'] = $category_id;
- }
- }
- $key = $request->input('key');
- $where['key'] = $key;
- $order_field = $request->input('order_field');
- $order_seq = $request->input('order_seq');
- if ($order_field != '' && in_array($order_field, ['recommend_index', 'click_count', 'update', 'size', 'create'])) {
- if ($order_field == 'update') {
- $order = ['book_configs.updated_at', 'desc'];
- } elseif ($order_field == 'create') {
- $order = ['book_configs.created_at', 'desc'];
- } else {
- $order = [$order_field, 'desc'];
- }
- if ($order_seq == 'asc') {
- $order = [$order_field, 'asc'];
- }
- if ($order_seq == 'desc') {
- $order = [$order_field, 'desc'];
- }
- }
- $status = $request->input('status');
- if ($status != '') {
- $where['status'] = $status;
- }
- $page_size = $request->input('page_size', 15);
- $books = BookConfigService::getBooks($where, $order, $page_size);
- return response()->pagination(new BookTransformer, $books);
- }
- public function hotWords(Request $request)
- {
- $result = BookConfigService::findBookKeywords();
- return response()->pagination(new KeywordTransformer, $result);
- }
- public function similarRecom(Request $request)
- {
- $category_id = $request->input('category_id');
- $bid = $request->input('bid');
- if (empty($bid) || empty($category_id)) {
- return response()->error('PARAM_ERROR');
- }
- $bid = BookService::decodeBidStatic($bid);
- $where = ['category_id' => $category_id, 'is_on_shelf' => [2]];
- $books = BookConfigService::getBooks($where, [], 4);
- $data = [];
- foreach ($books as $v) {
- if ($v->bid != $bid && count($data) < 3) {
- $data[] = $v;
- }
- }
- return response()->collection(new BookTransformer(), $data);
- }
- public function readOverRecommend(Request $request)
- {
- $bid = $request->input('bid');
- if (empty($bid)) {
- return response()->error('PARAM_ERROR');
- }
- $bid = BookService::decodeBidStatic($bid);
- $book_info = BookConfigService::getBookById($bid);
- $res = BookConfigService::getRecommendBooks($bid, $book_info->channel_name);
- $urge_status = 0;
- if ($book_info->status == 0 && !BookUrgeUpdateService::isHadUrged($this->uid, $bid)) {
- $urge_status = 1;
- }
- $recommend_result = collectionTransform(new BookTransformer(), $res);
- $book_status = [
- 'status' => $book_info->status,
- 'urge_status' => $urge_status
- ];
- $data = [
- 'recommend_result' => $recommend_result,
- 'book_status' => $book_status
- ];
- return response()->success($data);
- }
- public function rank(Request $request)
- {
- $sex = $request->input('sex');
- if ($sex == 1) {
- $books = collectionTransform(new BookTransformer, BookConfigService::getBooksByIds([11601, 11529, 3365, 10377, 11457, 8102, 6464, 7287, 2563, 10419]));
- } elseif ($sex == 2) {
- $books = collectionTransform(new BookTransformer, BookConfigService::getBooksByIds([10823, 10479, 10467, 10139, 9990, 9973, 9479, 9423, 1148, 8693, 8497, 8148, 8129, 7857, 7854, 7629, 7362, 5748, 5362, 4811, 4470, 4135, 3759, 3696, 3418, 3401, 3369, 2698, 1634, 1479]));
- } else {
- return response()->error('PARAM_ERROR');
- }
- return response()->success($books);
- }
- /**
- * 推荐书
- */
- public function recommen()
- {
- $reco_banner_type = ['FEMALE', 'PUBLIC'];
- $books = (new RecoBannerService)->getByType($reco_banner_type, 2);
- $books->transform(function ($item) {
- $result = $this->getBidCidFromUrl($item->redirect_url);
- $item->bid = $result['bid'];
- $item->cid = $result['cid'];
- if ($result['cid']) {
- $item->redirect_url = "views/Reader";
- } else {
- $item->redirect_url = "views/Detail";
- }
- return $item;
- });
- return response()->success($books);
- }
- }
|