BookController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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:'.$user);
  174. if($user){
  175. if(!$this->uid || (isset($this->uid) && !$this->send_order_id)){
  176. $result = $this->getCheckBids($channel,$books);
  177. return response()->success($result);
  178. }
  179. $hotBids = RecommendService::getRecommendIdsStatic($channel, 'hot');
  180. $liveBids = RecommendService::getRecommendIdsStatic($channel, 'live');
  181. $recomBids = RecommendService::getRecommendIdsStatic($channel, 'recom');
  182. $newBids = RecommendService::getRecommendIdsStatic($channel, 'new_recom');
  183. $result = [
  184. ['type' => 'reco_banner', 'lable' => '首页banner', 'books' => $books],
  185. ['type' => 'hot', 'lable' => '热门推荐', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($hotBids))],
  186. ['type' => 'zhibo', 'lable' => '神书直播', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($liveBids))],
  187. ['type' => 'recom', 'lable' => '编辑推荐', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($recomBids))],
  188. ['type' => 'new_recom', 'lable' => '新书推荐', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($newBids))],
  189. ];
  190. return response()->success($result);
  191. }
  192. $result = $this->getCheckBids($channel,$books);
  193. return response()->success($result);
  194. }
  195. /**
  196. *
  197. * @param $channel
  198. * @param $books
  199. * @return array
  200. */
  201. private function getCheckBids($channel,$books)
  202. {
  203. if($channel == 1){
  204. $hotBids = [13577,16712,13336,12717,15103,13928];
  205. $loopBids = [7287,14297,12716,14312,14000];
  206. $liveBids = [14793,12708,13286,13002,14004,13073];
  207. $recomBids = [15121,13929,13275,13254,14072,10313];
  208. $newBids = [3483,13278,12693,4098,10378,3526];
  209. }else{
  210. $loopBids = [];
  211. $hotBids = RecommendService::getRecommendIdsStatic($channel, 'hot');
  212. $liveBids = RecommendService::getRecommendIdsStatic($channel, 'live');
  213. $recomBids = RecommendService::getRecommendIdsStatic($channel, 'recom');
  214. $newBids = RecommendService::getRecommendIdsStatic($channel, 'new_recom');
  215. $newBids = array_replace($newBids,[array_search('13563',$newBids) => 10721]);
  216. }
  217. return array_filter([
  218. ['type' => 'reco_banner', 'lable' => '首页banner', 'books' => $books],
  219. ['type' => 'hot', 'lable' => '热门推荐', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($hotBids))],
  220. ['type' => 'zhibo', 'lable' => '神书直播', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($liveBids))],
  221. ['type' => 'recom', 'lable' => '编辑推荐', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($recomBids))],
  222. ['type' => 'new_recom', 'lable' => '新书推荐', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($newBids))],
  223. !empty($loopBids) ? ['type' => 'loop', 'lable' => '轮播图', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($loopBids))] : []
  224. ]);
  225. }
  226. private function getBidCidFromUrl(string $url)
  227. {
  228. if (preg_match('/\?bid=(\w+)\S+cid=(\w+)/', $url, $matches) || preg_match('/\?id=(\w+)/', $url, $matches)) {
  229. return [
  230. 'bid' => $matches[1],
  231. 'cid' => isset($matches[2]) ? $matches[2] : 0,
  232. ];
  233. } else {
  234. return [
  235. 'bid' => '',
  236. 'cid' => 0,
  237. ];
  238. }
  239. }
  240. public function library(Request $request)
  241. {
  242. $where = [];
  243. $order = [];
  244. $where['is_on_shelf'] = [2];
  245. $category_id = $request->input('category_id');
  246. if ($category_id) {
  247. if ($category_id == 1) {
  248. $where['channel_name'] = '男频';
  249. } elseif ($category_id == 2) {
  250. $where['channel_name'] = '女频';
  251. } else {
  252. $where['category_id'] = $category_id;
  253. }
  254. }
  255. $key = $request->input('key');
  256. $uid = $request->input('uid', 0);
  257. if ($key && $uid && is_numeric($uid)) {
  258. BookConfigService::saveUserSearchLog($key, $uid);
  259. }
  260. $where['key'] = $key;
  261. $order_field = $request->input('order_field');
  262. $order_seq = $request->input('order_seq');
  263. if ($order_field != '' && in_array($order_field, ['recommend_index', 'click_count', 'update', 'size', 'create'])) {
  264. if ($order_field == 'update') {
  265. $order = ['book_configs.updated_at', 'desc'];
  266. } elseif ($order_field == 'create') {
  267. $order = ['book_configs.created_at', 'desc'];
  268. } else {
  269. $order = [$order_field, 'desc'];
  270. }
  271. if ($order_seq == 'asc') {
  272. $order = [$order_field, 'asc'];
  273. }
  274. if ($order_seq == 'desc') {
  275. $order = [$order_field, 'desc'];
  276. }
  277. }
  278. // 审核状态默认值
  279. $package = $request->header('x-package', '');
  280. $brand = $request->header('x-nbrand', '');
  281. $codeVersion = $request->header('x-codeversion', '');
  282. if ($order_field === 'recommend_index' && Utils::checkIsAudit($package, $brand, $codeVersion)) {
  283. $order = ['book_configs.bid', 'desc'];
  284. }
  285. $status = $request->input('status');
  286. if ($status != '') {
  287. $where['status'] = $status;
  288. }
  289. // 搜索关键词的情况下,屏蔽书籍完本状态
  290. if ($key && isset($where['status'])) {
  291. unset($where['status']);
  292. }
  293. $page_size = $request->input('page_size', 15);
  294. $books = BookConfigService::getBooks($where, $order, $page_size);
  295. return response()->pagination(new BookTransformer, $books);
  296. }
  297. public function hotWords(Request $request)
  298. {
  299. $result = BookConfigService::findBookKeywords();
  300. return response()->pagination(new KeywordTransformer, $result);
  301. }
  302. public function similarRecom(Request $request)
  303. {
  304. $category_id = $request->input('category_id');
  305. $bid = $request->input('bid');
  306. if (empty($bid) || empty($category_id)) {
  307. return response()->error('PARAM_ERROR');
  308. }
  309. $bid = BookService::decodeBidStatic($bid);
  310. $where = ['category_id' => $category_id, 'is_on_shelf' => [2]];
  311. $books = BookConfigService::getBooks($where, [], 4);
  312. $data = [];
  313. foreach ($books as $v) {
  314. if ($v->bid != $bid && count($data) < 3) {
  315. $data[] = $v;
  316. }
  317. }
  318. return response()->collection(new BookTransformer(), $data);
  319. }
  320. public function readOverRecommend(Request $request)
  321. {
  322. $bid = $request->input('bid');
  323. if (empty($bid)) {
  324. return response()->error('PARAM_ERROR');
  325. }
  326. $bid = BookService::decodeBidStatic($bid);
  327. $book_info = BookConfigService::getBookById($bid);
  328. $res = BookConfigService::getRecommendBooks($bid, $book_info->channel_name);
  329. $urge_status = 0;
  330. if ($book_info->status == 0 && !BookUrgeUpdateService::isHadUrged($this->uid, $bid)) {
  331. $urge_status = 1;
  332. }
  333. $recommend_result = collectionTransform(new BookTransformer(), $res);
  334. $book_status = [
  335. 'status' => $book_info->status,
  336. 'urge_status' => $urge_status
  337. ];
  338. $data = [
  339. 'recommend_result' => $recommend_result,
  340. 'book_status' => $book_status
  341. ];
  342. return response()->success($data);
  343. }
  344. public function rank(Request $request)
  345. {
  346. // 1:男频,2:女频
  347. $sex = (int)$request->input('sex');
  348. if (!in_array($sex, [1, 2], true)) {
  349. return response()->error('PARAM_ERROR');
  350. }
  351. // 默认
  352. $bids = [11529, 11941, 12720, 11990, 11988, 11976, 11977, 4183, 12717, 11833];
  353. if ($sex === 2) {
  354. $bids = [8469, 11660, 9117, 7891, 12281, 12470, 8167, 11661, 11670, 8476, 8557, 11662,
  355. 11680, 11926, 12462, 7836, 11681, 11664, 11928, 8631];
  356. }
  357. /**
  358. * pid:1为男频 2为女频
  359. SELECT
  360. CONCAT( books.id, ',' )
  361. FROM
  362. books
  363. LEFT JOIN book_configs ON books.id = book_configs.bid
  364. WHERE
  365. book_configs.is_on_shelf = 2
  366. AND books.category_id IN ( SELECT id FROM book_categories WHERE pid = 2 )
  367. ORDER BY
  368. book_configs.recommend_index DESC
  369. LIMIT 10;
  370. */
  371. // 根据包名、平台、版本号判断是否审核
  372. $package = $request->header('x-package', '');
  373. $brand = $request->header('x-nbrand', '');
  374. $codeVersion = $request->header('x-codeversion', '');
  375. if (Utils::checkIsAudit($package, $brand, $codeVersion)) {
  376. $bids = [2266, 3838, 9700, 10175, 10301, 3422, 1166, 4546, 9163, 2509];
  377. if ($sex === 2) {
  378. $bids = [159, 2439, 6276, 10074, 5409, 9379, 10323, 9078, 3603, 487];
  379. }
  380. }
  381. $books = collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($bids));
  382. return response()->success($books);
  383. }
  384. /**
  385. * 推荐书
  386. */
  387. public function recommen()
  388. {
  389. $reco_banner_type = ['FEMALE', 'PUBLIC'];
  390. $books = (new RecoBannerService)->getByType($reco_banner_type, 2);
  391. $books->transform(function ($item) {
  392. $result = $this->getBidCidFromUrl($item->redirect_url);
  393. $item->bid = $result['bid'];
  394. $item->cid = $result['cid'];
  395. if ($result['cid']) {
  396. $item->redirect_url = "views/Reader";
  397. } else {
  398. $item->redirect_url = "views/Detail";
  399. }
  400. return $item;
  401. });
  402. return response()->success($books);
  403. }
  404. /**
  405. * 限免
  406. */
  407. public function free(int $sex)
  408. {
  409. $result = BookConfigService::findFreeBooks($sex);
  410. return response()->success($result);
  411. }
  412. }