BookController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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,['yuyuedu','xinghe','dingtian','dingtian3'])){
  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. return response()->success($result);
  191. }else{
  192. $package_id = 0;
  193. }
  194. \Log::info('un_send_order_book:package_id2:'.$package_id);
  195. }else{
  196. $package_id = 0;
  197. }
  198. \Log::info('un_send_order_book:package_id3:'.$package_id);
  199. $result = $this->getCheckBids($channel,$books,$package_id);
  200. return response()->success($result);
  201. }
  202. /**
  203. * 根据包名
  204. * @param $channel
  205. * @param $books
  206. * @param $package_id
  207. * @return array
  208. */
  209. private function getCheckBids($channel,$books,$package_id)
  210. {
  211. $hotBids = QappRecommendService::getRecommendByPacketId($channel, 'hot',$package_id);
  212. $liveBids = QappRecommendService::getRecommendByPacketId($channel, 'live',$package_id);
  213. $recomBids = QappRecommendService::getRecommendByPacketId($channel, 'recom',$package_id);
  214. $newBids = QappRecommendService::getRecommendByPacketId($channel, 'new_recom',$package_id);
  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) && $category_id != 0)) {
  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. 7287,14297,12716,14312,14000,13577,16712,13002,12717,15103,13928,14793,
  351. 12708,13286];
  352. if ($sex === 2) {
  353. $bids = [8469, 11660, 9117, 7891, 12281, 12470, 8167, 11661, 11670, 8476, 8557, 11662,
  354. 11680, 11926, 12462, 7836, 11681, 11664, 11928, 8631];
  355. }
  356. /**
  357. * pid:1为男频 2为女频
  358. SELECT
  359. CONCAT( books.id, ',' )
  360. FROM
  361. books
  362. LEFT JOIN book_configs ON books.id = book_configs.bid
  363. WHERE
  364. book_configs.is_on_shelf = 2
  365. AND books.category_id IN ( SELECT id FROM book_categories WHERE pid = 2 )
  366. ORDER BY
  367. book_configs.recommend_index DESC
  368. LIMIT 10;
  369. */
  370. // 根据包名、平台、版本号判断是否审核
  371. $package = $request->header('x-package', '');
  372. $brand = $request->header('x-nbrand', '');
  373. $codeVersion = $request->header('x-codeversion', '');
  374. if (Utils::checkIsAudit($package, $brand, $codeVersion)) {
  375. $bids = [2266, 3838, 9700, 10175, 10301, 3422, 1166, 4546, 9163, 2509,
  376. 7287,14297,12716,14312,14000,13577,16712,13002,12717,15103,13928,
  377. 14793,12708,13286,13336,13275,13073,15121,13929,12693,13254,3526,
  378. 10313,3483,13278,14004,4098,10378,14072,21376,21139,21757,19449];
  379. if ($sex === 2) {
  380. $bids = [159, 2439, 6276, 10074, 5409, 9379, 10323, 9078, 3603, 487];
  381. }
  382. }
  383. $books = collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($bids));
  384. return response()->success($books);
  385. }
  386. /**
  387. * 推荐书
  388. */
  389. public function recommen()
  390. {
  391. $reco_banner_type = ['FEMALE', 'PUBLIC'];
  392. $books = (new RecoBannerService)->getByType($reco_banner_type, 2);
  393. $books->transform(function ($item) {
  394. $result = $this->getBidCidFromUrl($item->redirect_url);
  395. $item->bid = $result['bid'];
  396. $item->cid = $result['cid'];
  397. if ($result['cid']) {
  398. $item->redirect_url = "views/Reader";
  399. } else {
  400. $item->redirect_url = "views/Detail";
  401. }
  402. return $item;
  403. });
  404. return response()->success($books);
  405. }
  406. /**
  407. * 限免
  408. */
  409. public function free(int $sex)
  410. {
  411. $result = BookConfigService::findFreeBooks($sex);
  412. return response()->success($result);
  413. }
  414. }