BookController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. <?php
  2. namespace App\Http\Controllers\QuickApp\Book;
  3. use App\Libs\Utils;
  4. use App\Modules\Book\Services\BookAuditService;
  5. use App\Modules\RecommendBook\Services\RecommendService;
  6. use App\Modules\Book\Services\RecoBannerService;
  7. use Illuminate\Http\Request;
  8. use App\Http\Controllers\QuickApp\BaseController;
  9. use App\Http\Controllers\QuickApp\Book\Transformers\BookTransformer;
  10. use App\Http\Controllers\QuickApp\Book\Transformers\KeywordTransformer;
  11. use App\Modules\Book\Models\BookConfig;
  12. use App\Modules\Book\Services\BookConfigService;
  13. use App\Modules\Book\Services\BookService;
  14. use App\Modules\Book\Services\BookUrgeUpdateService;
  15. use App\Modules\Book\Services\UserShelfBooksService;
  16. use App\Modules\Book\Services\ChapterService;
  17. use App\Modules\Subscribe\Services\BookOrderService;
  18. use App\Modules\Subscribe\Services\ChapterOrderService;
  19. use App\Modules\Subscribe\Services\YearOrderService;
  20. use App\Modules\User\Services\ReadRecordService;
  21. class BookController extends BaseController
  22. {
  23. public function index(Request $request, $bid)
  24. {
  25. $bid = BookService::decodeBidStatic($bid);
  26. $book_info = BookConfigService::getBookById($bid);
  27. if (!$book_info) {
  28. return response()->error('QAPP_SYS_ERROR');
  29. }
  30. //yuyuedu、xinghe 快应用这两个cp的书屏蔽下
  31. if(in_array($book_info->cp_source,['yuyuedu','xinghe','dingtian','dingtian3'])){
  32. return response()->error('QAPP_SYS_ERROR');
  33. }
  34. if($this->distribution_channel_id == 7477 && $bid == 13765){
  35. $book_info->is_on_shelf = 2;
  36. }
  37. if (!in_array($book_info->is_on_shelf, [2])) {
  38. return response()->error('QAPP_OFF_SHELF');
  39. }
  40. $is_on_shelf = UserShelfBooksService::getUserShelfBooksListByUidAndBid($this->uid, $bid);
  41. $book_info['is_on_user_shelf'] = 0;
  42. if ($is_on_shelf) {
  43. $book_info['is_on_user_shelf'] = 1;
  44. }
  45. $last_chapter = ChapterService::getChapterNameById($book_info['last_cid'], $bid);
  46. $book_info->last_chapter = $last_chapter['name'];
  47. list($is_split,$is_change_chapter_name) = BookService::splitContent($bid);
  48. if($is_split && ($book_info->channel_name == '男频' || $is_change_chapter_name) ){
  49. $book_info->last_chapter = '第'.$book_info->chapter_count.'章';
  50. }
  51. $book_info['last_chapter_is_vip'] = $last_chapter['is_vip'];
  52. $book_info['is_need_charge'] = $this->isNeedCharge($bid, $last_chapter, $book_info);
  53. $record = ReadRecordService::getBookReadRecordStatic($this->uid, $bid);
  54. if ($record) {
  55. $book_info['record_chapter_id'] = $record['record_chapter_id'];
  56. $book_info['record_chapter_name'] = $record['record_chapter_name'];
  57. }
  58. return response()->item(new BookTransformer(), $book_info);
  59. }
  60. /**
  61. * 获取订购记录
  62. * @param $book_info
  63. * @param $chapter_id
  64. * @return bool
  65. */
  66. protected function getOrderRecord($bid, $chapter_id)
  67. {
  68. //包年记录
  69. $uid = $this->uid;
  70. $res = YearOrderService::getRecord($uid);
  71. if ($res) return true;
  72. $res = null;
  73. //单本订购记录
  74. $res = BookOrderService::getRecordByuidBid($uid, $bid);
  75. if ($res) return true;
  76. $res = null;
  77. //章节订购记录
  78. $chapterOrder = new ChapterOrderService();
  79. if ($chapterOrder->checkIsOrdered($uid, $bid, $chapter_id)) return true;
  80. return false;
  81. }
  82. /**
  83. * 判断是否需要充值
  84. */
  85. private function isBookNeedCharge(int $bid, float $price)
  86. {
  87. $book_order = $this->getOrderRecord($bid, 0);
  88. if ($book_order) {
  89. return false;
  90. } else {
  91. $user_info = $this->user_info;
  92. return $user_info['balance'] < $price;
  93. }
  94. }
  95. /**
  96. * 判断章节是否需要充值
  97. */
  98. private function isChapterNeedCharge(int $bid, int $cid, float $price)
  99. {
  100. $book_order = $this->getOrderRecord($bid, $cid);
  101. if ($book_order) {
  102. return false;
  103. } else {
  104. $user_info = $this->user_info;
  105. return $user_info['balance'] < $price;
  106. }
  107. }
  108. /**
  109. * 判断是否需要充值
  110. */
  111. private function isNeedCharge(int $bid, $last_chapter, $book_info)
  112. {
  113. $is_free = BookConfigService::judgeBookIsFree($bid);
  114. if ($is_free) {
  115. return false;
  116. }
  117. switch ($book_info->charge_type) {
  118. case 'BOOK':
  119. $price = $this->getPrice($book_info);
  120. return $this->isBookNeedCharge($bid, $price);
  121. default:
  122. $price = $last_chapter->is_vip ? $this->getPrice($book_info, $last_chapter->size) : 0;
  123. return $last_chapter->is_vip ? $this->isChapterNeedCharge($bid, $last_chapter->id, $price) : false;
  124. }
  125. }
  126. /**
  127. * 计算价格
  128. * @param $book_info
  129. * @param $chapter_size
  130. * @return float
  131. */
  132. protected function getPrice($book_info, $chapter_size = 0)
  133. {
  134. if ($book_info->charge_type == 'BOOK')
  135. return $book_info->price * 100;
  136. return ceil($chapter_size / 100);
  137. }
  138. /**
  139. * 首页
  140. */
  141. public function getBookLists(Request $request, $sex)
  142. {
  143. // 获取基本数据
  144. $package = $request->header('x-package', '');
  145. $brand = $request->header('x-nbrand', '');
  146. $codeVersion = $request->header('x-codeversion', '');
  147. // 根据包名、平台、版本号判断是否审核
  148. if (Utils::checkIsAudit($package, $brand, $codeVersion)) {
  149. $result = BookAuditService::getHomeBooksData($sex, $package);
  150. return response()->success($result);
  151. }
  152. if ($sex == 'male') {
  153. $channel = 1;
  154. $reco_banner_type = ['MALE', 'PUBLIC'];
  155. } else {
  156. $reco_banner_type = ['FEMALE', 'PUBLIC'];
  157. $channel = 2;
  158. }
  159. $books = (new RecoBannerService)->getByType($reco_banner_type, 2);
  160. $books->transform(function ($item) {
  161. $result = $this->getBidCidFromUrl($item->redirect_url);
  162. $item->bid = $result['bid'];
  163. $item->cid = $result['cid'];
  164. if ($result['cid']) {
  165. $item->redirect_url = "views/Reader";
  166. } else {
  167. $item->redirect_url = "views/Detail";
  168. }
  169. return $item;
  170. });
  171. if(!$this->uid || (isset($this->uid) && !$this->send_order_id)){
  172. $result = $this->getCheckBids($channel,$books);
  173. return response()->success($result);
  174. }
  175. $hotBids = RecommendService::getRecommendIdsStatic($channel, 'hot');
  176. $liveBids = RecommendService::getRecommendIdsStatic($channel, 'live');
  177. $recomBids = RecommendService::getRecommendIdsStatic($channel, 'recom');
  178. $newBids = RecommendService::getRecommendIdsStatic($channel, 'new_recom');
  179. $result = [
  180. ['type' => 'reco_banner', 'lable' => '首页banner', 'books' => $books],
  181. ['type' => 'hot', 'lable' => '热门推荐', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($hotBids))],
  182. ['type' => 'zhibo', 'lable' => '神书直播', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($liveBids))],
  183. ['type' => 'recom', 'lable' => '编辑推荐', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($recomBids))],
  184. ['type' => 'new_recom', 'lable' => '新书推荐', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($newBids))],
  185. ];
  186. return response()->success($result);
  187. }
  188. /**
  189. *
  190. * @param $channel
  191. * @param $books
  192. * @return array
  193. */
  194. private function getCheckBids($channel,$books)
  195. {
  196. if($channel == 1){
  197. $hotBids = [13577,16712,13336,12717,15103,13928];
  198. $loopBids = [7287,14297,12716,14312,14000];
  199. $liveBids = [14793,12708,13286,13002,14004,13073];
  200. $recomBids = [15121,13929,13275,13254,14072,10313];
  201. $newBids = [3483,13278,12693,4098,10378,3526];
  202. }else{
  203. $loopBids = [];
  204. $hotBids = RecommendService::getRecommendIdsStatic($channel, 'hot');
  205. $liveBids = RecommendService::getRecommendIdsStatic($channel, 'live');
  206. $recomBids = RecommendService::getRecommendIdsStatic($channel, 'recom');
  207. $newBids = RecommendService::getRecommendIdsStatic($channel, 'new_recom');
  208. $newBids = array_replace($newBids,[array_search('13563',$newBids) => 10721]);
  209. }
  210. return array_filter([
  211. ['type' => 'reco_banner', 'lable' => '首页banner', 'books' => $books],
  212. ['type' => 'hot', 'lable' => '热门推荐', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($hotBids))],
  213. ['type' => 'zhibo', 'lable' => '神书直播', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($liveBids))],
  214. ['type' => 'recom', 'lable' => '编辑推荐', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($recomBids))],
  215. ['type' => 'new_recom', 'lable' => '新书推荐', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($newBids))],
  216. !empty($loopBids) ? ['type' => 'loop', 'lable' => '轮播图', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($loopBids))] : []
  217. ]);
  218. }
  219. private function getBidCidFromUrl(string $url)
  220. {
  221. if (preg_match('/\?bid=(\w+)\S+cid=(\w+)/', $url, $matches) || preg_match('/\?id=(\w+)/', $url, $matches)) {
  222. return [
  223. 'bid' => $matches[1],
  224. 'cid' => isset($matches[2]) ? $matches[2] : 0,
  225. ];
  226. } else {
  227. return [
  228. 'bid' => '',
  229. 'cid' => 0,
  230. ];
  231. }
  232. }
  233. public function library(Request $request)
  234. {
  235. $where = [];
  236. $order = [];
  237. $where['is_on_shelf'] = [2];
  238. $category_id = $request->input('category_id');
  239. if ($category_id) {
  240. if ($category_id == 1) {
  241. $where['channel_name'] = '男频';
  242. } elseif ($category_id == 2) {
  243. $where['channel_name'] = '女频';
  244. } else {
  245. $where['category_id'] = $category_id;
  246. }
  247. }
  248. $key = $request->input('key');
  249. $uid = $request->input('uid', 0);
  250. if ($key && $uid && is_numeric($uid)) {
  251. BookConfigService::saveUserSearchLog($key, $uid);
  252. }
  253. $where['key'] = $key;
  254. $order_field = $request->input('order_field');
  255. $order_seq = $request->input('order_seq');
  256. if ($order_field != '' && in_array($order_field, ['recommend_index', 'click_count', 'update', 'size', 'create'])) {
  257. if ($order_field == 'update') {
  258. $order = ['book_configs.updated_at', 'desc'];
  259. } elseif ($order_field == 'create') {
  260. $order = ['book_configs.created_at', 'desc'];
  261. } else {
  262. $order = [$order_field, 'desc'];
  263. }
  264. if ($order_seq == 'asc') {
  265. $order = [$order_field, 'asc'];
  266. }
  267. if ($order_seq == 'desc') {
  268. $order = [$order_field, 'desc'];
  269. }
  270. }
  271. // 审核状态默认值
  272. $package = $request->header('x-package', '');
  273. $brand = $request->header('x-nbrand', '');
  274. $codeVersion = $request->header('x-codeversion', '');
  275. if ($order_field === 'recommend_index' && Utils::checkIsAudit($package, $brand, $codeVersion)) {
  276. $order = ['book_configs.bid', 'desc'];
  277. }
  278. $status = $request->input('status');
  279. if ($status != '') {
  280. $where['status'] = $status;
  281. }
  282. // 搜索关键词的情况下,屏蔽书籍完本状态
  283. if ($key && isset($where['status'])) {
  284. unset($where['status']);
  285. }
  286. $page_size = $request->input('page_size', 15);
  287. $books = BookConfigService::getBooks($where, $order, $page_size);
  288. return response()->pagination(new BookTransformer, $books);
  289. }
  290. public function hotWords(Request $request)
  291. {
  292. $result = BookConfigService::findBookKeywords();
  293. return response()->pagination(new KeywordTransformer, $result);
  294. }
  295. public function similarRecom(Request $request)
  296. {
  297. $category_id = $request->input('category_id');
  298. $bid = $request->input('bid');
  299. if (empty($bid) || empty($category_id)) {
  300. return response()->error('PARAM_ERROR');
  301. }
  302. $bid = BookService::decodeBidStatic($bid);
  303. $where = ['category_id' => $category_id, 'is_on_shelf' => [2]];
  304. $books = BookConfigService::getBooks($where, [], 4);
  305. $data = [];
  306. foreach ($books as $v) {
  307. if ($v->bid != $bid && count($data) < 3) {
  308. $data[] = $v;
  309. }
  310. }
  311. return response()->collection(new BookTransformer(), $data);
  312. }
  313. public function readOverRecommend(Request $request)
  314. {
  315. $bid = $request->input('bid');
  316. if (empty($bid)) {
  317. return response()->error('PARAM_ERROR');
  318. }
  319. $bid = BookService::decodeBidStatic($bid);
  320. $book_info = BookConfigService::getBookById($bid);
  321. $res = BookConfigService::getRecommendBooks($bid, $book_info->channel_name);
  322. $urge_status = 0;
  323. if ($book_info->status == 0 && !BookUrgeUpdateService::isHadUrged($this->uid, $bid)) {
  324. $urge_status = 1;
  325. }
  326. $recommend_result = collectionTransform(new BookTransformer(), $res);
  327. $book_status = [
  328. 'status' => $book_info->status,
  329. 'urge_status' => $urge_status
  330. ];
  331. $data = [
  332. 'recommend_result' => $recommend_result,
  333. 'book_status' => $book_status
  334. ];
  335. return response()->success($data);
  336. }
  337. public function rank(Request $request)
  338. {
  339. // 1:男频,2:女频
  340. $sex = (int)$request->input('sex');
  341. if (!in_array($sex, [1, 2], true)) {
  342. return response()->error('PARAM_ERROR');
  343. }
  344. // 默认
  345. $bids = [11529, 11941, 12720, 11990, 11988, 11976, 11977, 4183, 12717, 11833];
  346. if ($sex === 2) {
  347. $bids = [8469, 11660, 9117, 7891, 12281, 12470, 8167, 11661, 11670, 8476, 8557, 11662,
  348. 11680, 11926, 12462, 7836, 11681, 11664, 11928, 8631];
  349. }
  350. /**
  351. * pid:1为男频 2为女频
  352. SELECT
  353. CONCAT( books.id, ',' )
  354. FROM
  355. books
  356. LEFT JOIN book_configs ON books.id = book_configs.bid
  357. WHERE
  358. book_configs.is_on_shelf = 2
  359. AND books.category_id IN ( SELECT id FROM book_categories WHERE pid = 2 )
  360. ORDER BY
  361. book_configs.recommend_index DESC
  362. LIMIT 10;
  363. */
  364. // 根据包名、平台、版本号判断是否审核
  365. $package = $request->header('x-package', '');
  366. $brand = $request->header('x-nbrand', '');
  367. $codeVersion = $request->header('x-codeversion', '');
  368. if (Utils::checkIsAudit($package, $brand, $codeVersion)) {
  369. $bids = [2266, 3838, 9700, 10175, 10301, 3422, 1166, 4546, 9163, 2509];
  370. if ($sex === 2) {
  371. $bids = [159, 2439, 6276, 10074, 5409, 9379, 10323, 9078, 3603, 487];
  372. }
  373. }
  374. $books = collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($bids));
  375. return response()->success($books);
  376. }
  377. /**
  378. * 推荐书
  379. */
  380. public function recommen()
  381. {
  382. $reco_banner_type = ['FEMALE', 'PUBLIC'];
  383. $books = (new RecoBannerService)->getByType($reco_banner_type, 2);
  384. $books->transform(function ($item) {
  385. $result = $this->getBidCidFromUrl($item->redirect_url);
  386. $item->bid = $result['bid'];
  387. $item->cid = $result['cid'];
  388. if ($result['cid']) {
  389. $item->redirect_url = "views/Reader";
  390. } else {
  391. $item->redirect_url = "views/Detail";
  392. }
  393. return $item;
  394. });
  395. return response()->success($books);
  396. }
  397. /**
  398. * 限免
  399. */
  400. public function free(int $sex)
  401. {
  402. $result = BookConfigService::findFreeBooks($sex);
  403. return response()->success($result);
  404. }
  405. }