BookController.php 17 KB

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