BookController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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 = $last_chapter->is_vip ? $this->getPrice($book_info, $last_chapter->size) : 0;
  129. return $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);
  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);
  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);
  199. }else{
  200. $package_id = 0;
  201. }
  202. \Log::info('un_send_order_book:package_id3:'.$package_id);
  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. //获取书本数量
  236. $count = count($bid_list);
  237. //获取当前有效书本数量
  238. $book_count = BookConfig::join('books', 'book_configs.bid', '=', 'books.id')
  239. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  240. ->whereIn('book_configs.bid',$bid_list)
  241. ->whereNotIn('book_configs.cp_source',getHiddenCp())
  242. ->where('book_categories.pid',$channel)
  243. ->count();
  244. if($count == $book_count){
  245. return $bid_list;
  246. }
  247. //获取需要补充的书籍数量
  248. $supplement_count = (($count - $book_count) > 0) ? $count - $book_count : 0;
  249. if($supplement_count <= 0){
  250. return $bid_list;
  251. }
  252. //获取随机的有效的书籍bid
  253. $rand_bid = BookConfig::join('books', 'book_configs.bid', '=', 'books.id')
  254. ->leftjoin('book_categories', 'books.category_id', 'book_categories.id')
  255. ->where('book_configs.is_on_shelf',1)
  256. ->whereNotIn('book_configs.cp_source',getHiddenCp())
  257. ->inRandomOrder()
  258. ->limit($supplement_count)
  259. ->where('book_categories.pid',$channel)
  260. ->get()->pluck('book_configs.bid')->toArray();
  261. return array_fill(array_merge($bid_list,$rand_bid));
  262. }
  263. private function getBidCidFromUrl(string $url)
  264. {
  265. if (preg_match('/\?bid=(\w+)\S+cid=(\w+)/', $url, $matches) || preg_match('/\?id=(\w+)/', $url, $matches)) {
  266. return [
  267. 'bid' => $matches[1],
  268. 'cid' => isset($matches[2]) ? $matches[2] : 0,
  269. ];
  270. } else {
  271. return [
  272. 'bid' => '',
  273. 'cid' => 0,
  274. ];
  275. }
  276. }
  277. public function library(Request $request)
  278. {
  279. $where = [];
  280. $order = [];
  281. $where['is_on_shelf'] = [2];
  282. $category_id = $request->input('category_id');
  283. if ($category_id) {
  284. if ($category_id == 1) {
  285. $where['channel_name'] = '男频';
  286. } elseif ($category_id == 2) {
  287. $where['channel_name'] = '女频';
  288. } else {
  289. $where['category_id'] = $category_id;
  290. }
  291. }
  292. $key = $request->input('key');
  293. $uid = $request->input('uid', 0);
  294. if ($key && $uid && is_numeric($uid)) {
  295. BookConfigService::saveUserSearchLog($key, $uid);
  296. }
  297. $where['key'] = $key;
  298. $order_field = $request->input('order_field');
  299. $order_seq = $request->input('order_seq');
  300. if ($order_field != '' && in_array($order_field, ['recommend_index', 'click_count', 'update', 'size', 'create'])) {
  301. if ($order_field == 'update') {
  302. $order = ['book_configs.updated_at', 'desc'];
  303. } elseif ($order_field == 'create') {
  304. $order = ['book_configs.created_at', 'desc'];
  305. } else {
  306. $order = [$order_field, 'desc'];
  307. }
  308. if ($order_seq == 'asc') {
  309. $order = [$order_field, 'asc'];
  310. }
  311. if ($order_seq == 'desc') {
  312. $order = [$order_field, 'desc'];
  313. }
  314. }
  315. // 审核状态默认值
  316. $package = $request->header('x-package', '');
  317. $brand = $request->header('x-nbrand', '');
  318. $codeVersion = $request->header('x-codeversion', '');
  319. if ($order_field === 'recommend_index' && Utils::checkIsAudit($package, $brand, $codeVersion)) {
  320. $order = ['book_configs.bid', 'desc'];
  321. }
  322. $status = $request->input('status');
  323. if ($status != '') {
  324. $where['status'] = $status;
  325. }
  326. // 搜索关键词的情况下,屏蔽书籍完本状态
  327. if ($key && isset($where['status'])) {
  328. unset($where['status']);
  329. }
  330. $page_size = $request->input('page_size', 15);
  331. $books = BookConfigService::getBooks($where, $order, $page_size);
  332. return response()->pagination(new BookTransformer, $books);
  333. }
  334. public function hotWords(Request $request)
  335. {
  336. $result = BookConfigService::findBookKeywords();
  337. return response()->pagination(new KeywordTransformer, $result);
  338. }
  339. public function similarRecom(Request $request)
  340. {
  341. $category_id = $request->input('category_id');
  342. $bid = $request->input('bid');
  343. if (empty($bid) || (empty($category_id) && $category_id != 0)) {
  344. return response()->error('PARAM_ERROR');
  345. }
  346. $bid = BookService::decodeBidStatic($bid);
  347. $where = ['category_id' => $category_id, 'is_on_shelf' => [2]];
  348. $books = BookConfigService::getBooks($where, [], 4);
  349. $data = [];
  350. foreach ($books as $v) {
  351. if ($v->bid != $bid && count($data) < 3) {
  352. $data[] = $v;
  353. }
  354. }
  355. return response()->collection(new BookTransformer(), $data);
  356. }
  357. public function readOverRecommend(Request $request)
  358. {
  359. $bid = $request->input('bid');
  360. if (empty($bid)) {
  361. return response()->error('PARAM_ERROR');
  362. }
  363. $bid = BookService::decodeBidStatic($bid);
  364. $book_info = BookConfigService::getBookById($bid);
  365. $res = BookConfigService::getRecommendBooks($bid, $book_info->channel_name);
  366. $urge_status = 0;
  367. if ($book_info->status == 0 && !BookUrgeUpdateService::isHadUrged($this->uid, $bid)) {
  368. $urge_status = 1;
  369. }
  370. $recommend_result = collectionTransform(new BookTransformer(), $res);
  371. $book_status = [
  372. 'status' => $book_info->status,
  373. 'urge_status' => $urge_status
  374. ];
  375. $data = [
  376. 'recommend_result' => $recommend_result,
  377. 'book_status' => $book_status
  378. ];
  379. return response()->success($data);
  380. }
  381. public function rank(Request $request)
  382. {
  383. // 1:男频,2:女频
  384. $sex = (int)$request->input('sex');
  385. if (!in_array($sex, [1, 2], true)) {
  386. return response()->error('PARAM_ERROR');
  387. }
  388. // 默认
  389. $bids = [11529, 11941, 12720, 11990, 11988, 11976, 11977, 4183, 12717, 11833,
  390. 7287,14297,12716,14312,14000,13577,16712,13002,12717,15103,13928,14793,
  391. 12708,13286];
  392. if ($sex === 2) {
  393. $bids = [8469, 11660, 9117, 7891, 12281, 12470, 8167, 11661, 11670, 8476, 8557, 11662,
  394. 11680, 11926, 12462, 7836, 11681, 11664, 11928, 8631];
  395. }
  396. /**
  397. * pid:1为男频 2为女频
  398. SELECT
  399. CONCAT( books.id, ',' )
  400. FROM
  401. books
  402. LEFT JOIN book_configs ON books.id = book_configs.bid
  403. WHERE
  404. book_configs.is_on_shelf = 2
  405. AND books.category_id IN ( SELECT id FROM book_categories WHERE pid = 2 )
  406. ORDER BY
  407. book_configs.recommend_index DESC
  408. LIMIT 10;
  409. */
  410. // 根据包名、平台、版本号判断是否审核
  411. $package = $request->header('x-package', '');
  412. $brand = $request->header('x-nbrand', '');
  413. $codeVersion = $request->header('x-codeversion', '');
  414. if (Utils::checkIsAudit($package, $brand, $codeVersion)) {
  415. $bids = [2266, 3838, 9700, 10175, 10301, 3422, 1166, 4546, 9163, 2509,
  416. 7287,14297,12716,14312,14000,13577,16712,13002,12717,15103,13928,
  417. 14793,12708,13286,13336,13275,13073,15121,13929,12693,13254,3526,
  418. 10313,3483,13278,14004,4098,10378,14072,21376,21139,21757,19449];
  419. if ($sex === 2) {
  420. $bids = [159, 2439, 6276, 10074, 5409, 9379, 10323, 9078, 3603, 487];
  421. }
  422. }
  423. $books = collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($bids));
  424. return response()->success($books);
  425. }
  426. /**
  427. * 推荐书
  428. */
  429. public function recommen()
  430. {
  431. $reco_banner_type = ['FEMALE', 'PUBLIC'];
  432. $books = (new RecoBannerService)->getByType($reco_banner_type, 2);
  433. $books->transform(function ($item) {
  434. $result = $this->getBidCidFromUrl($item->redirect_url);
  435. $item->bid = $result['bid'];
  436. $item->cid = $result['cid'];
  437. if ($result['cid']) {
  438. $item->redirect_url = "views/Reader";
  439. } else {
  440. $item->redirect_url = "views/Detail";
  441. }
  442. return $item;
  443. });
  444. return response()->success($books);
  445. }
  446. /**
  447. * 限免
  448. */
  449. public function free(int $sex)
  450. {
  451. $result = BookConfigService::findFreeBooks($sex);
  452. return response()->success($result);
  453. }
  454. }