BookController.php 17 KB

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