BookController.php 20 KB

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