BookController.php 17 KB

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