BookController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 Illuminate\Http\Request;
  8. use App\Http\Controllers\QuickApp\BaseController;
  9. use App\Http\Controllers\QuickApp\Book\Transformers\BookTransformer;
  10. use App\Http\Controllers\QuickApp\Book\Transformers\KeywordTransformer;
  11. use App\Modules\Book\Models\BookConfig;
  12. use App\Modules\Book\Services\BookConfigService;
  13. use App\Modules\Book\Services\BookService;
  14. use App\Modules\Book\Services\BookUrgeUpdateService;
  15. use App\Modules\Book\Services\UserShelfBooksService;
  16. use App\Modules\Book\Services\ChapterService;
  17. use App\Modules\Subscribe\Services\BookOrderService;
  18. use App\Modules\Subscribe\Services\ChapterOrderService;
  19. use App\Modules\Subscribe\Services\YearOrderService;
  20. use App\Modules\User\Services\ReadRecordService;
  21. class BookController extends BaseController
  22. {
  23. public function index(Request $request, $bid)
  24. {
  25. $bid = BookService::decodeBidStatic($bid);
  26. $book_info = BookConfigService::getBookById($bid);
  27. if (!$book_info) {
  28. return response()->error('QAPP_SYS_ERROR');
  29. }
  30. if($this->distribution_channel_id == 7477 && $bid == 13765){
  31. $book_info->is_on_shelf = 2;
  32. }
  33. if (!in_array($book_info->is_on_shelf, [2])) {
  34. return response()->error('QAPP_OFF_SHELF');
  35. }
  36. $is_on_shelf = UserShelfBooksService::getUserShelfBooksListByUidAndBid($this->uid, $bid);
  37. $book_info['is_on_user_shelf'] = 0;
  38. if ($is_on_shelf) {
  39. $book_info['is_on_user_shelf'] = 1;
  40. }
  41. $last_chapter = ChapterService::getChapterNameById($book_info['last_cid'], $bid);
  42. $book_info->last_chapter = $last_chapter['name'];
  43. list($is_split,$is_change_chapter_name) = BookService::splitContent($bid);
  44. if($is_split && ($book_info->channel_name == '男频' || $is_change_chapter_name) ){
  45. $book_info->last_chapter = '第'.$book_info->chapter_count.'章';
  46. }
  47. $book_info['last_chapter_is_vip'] = $last_chapter['is_vip'];
  48. $book_info['is_need_charge'] = $this->isNeedCharge($bid, $last_chapter, $book_info);
  49. $record = ReadRecordService::getBookReadRecordStatic($this->uid, $bid);
  50. if ($record) {
  51. $book_info['record_chapter_id'] = $record['record_chapter_id'];
  52. $book_info['record_chapter_name'] = $record['record_chapter_name'];
  53. }
  54. return response()->item(new BookTransformer(), $book_info);
  55. }
  56. /**
  57. * 获取订购记录
  58. * @param $book_info
  59. * @param $chapter_id
  60. * @return bool
  61. */
  62. protected function getOrderRecord($bid, $chapter_id)
  63. {
  64. //包年记录
  65. $uid = $this->uid;
  66. $res = YearOrderService::getRecord($uid);
  67. if ($res) return true;
  68. $res = null;
  69. //单本订购记录
  70. $res = BookOrderService::getRecordByuidBid($uid, $bid);
  71. if ($res) return true;
  72. $res = null;
  73. //章节订购记录
  74. $chapterOrder = new ChapterOrderService();
  75. if ($chapterOrder->checkIsOrdered($uid, $bid, $chapter_id)) return true;
  76. return false;
  77. }
  78. /**
  79. * 判断是否需要充值
  80. */
  81. private function isBookNeedCharge(int $bid, float $price)
  82. {
  83. $book_order = $this->getOrderRecord($bid, 0);
  84. if ($book_order) {
  85. return false;
  86. } else {
  87. $user_info = $this->user_info;
  88. return $user_info['balance'] < $price;
  89. }
  90. }
  91. /**
  92. * 判断章节是否需要充值
  93. */
  94. private function isChapterNeedCharge(int $bid, int $cid, float $price)
  95. {
  96. $book_order = $this->getOrderRecord($bid, $cid);
  97. if ($book_order) {
  98. return false;
  99. } else {
  100. $user_info = $this->user_info;
  101. return $user_info['balance'] < $price;
  102. }
  103. }
  104. /**
  105. * 判断是否需要充值
  106. */
  107. private function isNeedCharge(int $bid, $last_chapter, $book_info)
  108. {
  109. $is_free = BookConfigService::judgeBookIsFree($bid);
  110. if ($is_free) {
  111. return false;
  112. }
  113. switch ($book_info->charge_type) {
  114. case 'BOOK':
  115. $price = $this->getPrice($book_info);
  116. return $this->isBookNeedCharge($bid, $price);
  117. default:
  118. $price = $last_chapter->is_vip ? $this->getPrice($book_info, $last_chapter->size) : 0;
  119. return $last_chapter->is_vip ? $this->isChapterNeedCharge($bid, $last_chapter->id, $price) : false;
  120. }
  121. }
  122. /**
  123. * 计算价格
  124. * @param $book_info
  125. * @param $chapter_size
  126. * @return float
  127. */
  128. protected function getPrice($book_info, $chapter_size = 0)
  129. {
  130. if ($book_info->charge_type == 'BOOK')
  131. return $book_info->price * 100;
  132. return ceil($chapter_size / 100);
  133. }
  134. /**
  135. * 首页
  136. */
  137. public function getBookLists(Request $request, $sex)
  138. {
  139. // 获取基本数据
  140. $package = $request->header('x-package', '');
  141. $brand = $request->header('x-nbrand', '');
  142. $codeVersion = $request->header('x-codeversion', '');
  143. // 根据包名、平台、版本号判断是否审核
  144. if (Utils::checkIsAudit($package, $brand, $codeVersion)) {
  145. $result = BookAuditService::getHomeBooksData($sex, $package);
  146. return response()->success($result);
  147. }
  148. if ($sex == 'male') {
  149. $channel = 1;
  150. $reco_banner_type = ['MALE', 'PUBLIC'];
  151. } else {
  152. $reco_banner_type = ['FEMALE', 'PUBLIC'];
  153. $channel = 2;
  154. }
  155. $books = (new RecoBannerService)->getByType($reco_banner_type, 2);
  156. $books->transform(function ($item) {
  157. $result = $this->getBidCidFromUrl($item->redirect_url);
  158. $item->bid = $result['bid'];
  159. $item->cid = $result['cid'];
  160. if ($result['cid']) {
  161. $item->redirect_url = "views/Reader";
  162. } else {
  163. $item->redirect_url = "views/Detail";
  164. }
  165. return $item;
  166. });
  167. $hotBids = RecommendService::getRecommendIdsStatic($channel, 'hot');
  168. $liveBids = RecommendService::getRecommendIdsStatic($channel, 'live');
  169. $recomBids = RecommendService::getRecommendIdsStatic($channel, 'recom');
  170. $newBids = RecommendService::getRecommendIdsStatic($channel, 'new_recom');
  171. $result = [
  172. ['type' => 'reco_banner', 'lable' => '首页banner', 'books' => $books],
  173. ['type' => 'hot', 'lable' => '热门推荐', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($hotBids))],
  174. ['type' => 'zhibo', 'lable' => '神书直播', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($liveBids))],
  175. ['type' => 'recom', 'lable' => '编辑推荐', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($recomBids))],
  176. ['type' => 'new_recom', 'lable' => '新书推荐', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($newBids))],
  177. ];
  178. return response()->success($result);
  179. }
  180. private function getBidCidFromUrl(string $url)
  181. {
  182. if (preg_match('/\?bid=(\w+)\S+cid=(\w+)/', $url, $matches) || preg_match('/\?id=(\w+)/', $url, $matches)) {
  183. return [
  184. 'bid' => $matches[1],
  185. 'cid' => isset($matches[2]) ? $matches[2] : 0,
  186. ];
  187. } else {
  188. return [
  189. 'bid' => '',
  190. 'cid' => 0,
  191. ];
  192. }
  193. }
  194. public function library(Request $request)
  195. {
  196. $where = [];
  197. $order = [];
  198. $where['is_on_shelf'] = [2];
  199. $category_id = $request->input('category_id');
  200. if ($category_id) {
  201. if ($category_id == 1) {
  202. $where['channel_name'] = '男频';
  203. } elseif ($category_id == 2) {
  204. $where['channel_name'] = '女频';
  205. } else {
  206. $where['category_id'] = $category_id;
  207. }
  208. }
  209. $key = $request->input('key');
  210. $uid = $request->input('uid', 0);
  211. if ($key && $uid && is_numeric($uid)) {
  212. BookConfigService::saveUserSearchLog($key, $uid);
  213. }
  214. $where['key'] = $key;
  215. $order_field = $request->input('order_field');
  216. $order_seq = $request->input('order_seq');
  217. if ($order_field != '' && in_array($order_field, ['recommend_index', 'click_count', 'update', 'size', 'create'])) {
  218. if ($order_field == 'update') {
  219. $order = ['book_configs.updated_at', 'desc'];
  220. } elseif ($order_field == 'create') {
  221. $order = ['book_configs.created_at', 'desc'];
  222. } else {
  223. $order = [$order_field, 'desc'];
  224. }
  225. if ($order_seq == 'asc') {
  226. $order = [$order_field, 'asc'];
  227. }
  228. if ($order_seq == 'desc') {
  229. $order = [$order_field, 'desc'];
  230. }
  231. }
  232. // 审核状态默认值
  233. $package = $request->header('x-package', '');
  234. $brand = $request->header('x-nbrand', '');
  235. $codeVersion = $request->header('x-codeversion', '');
  236. if ($order_field === 'recommend_index' && Utils::checkIsAudit($package, $brand, $codeVersion)) {
  237. $order = ['book_configs.bid', 'desc'];
  238. }
  239. $status = $request->input('status');
  240. if ($status != '') {
  241. $where['status'] = $status;
  242. }
  243. // 搜索关键词的情况下,屏蔽书籍完本状态
  244. if ($key && isset($where['status'])) {
  245. unset($where['status']);
  246. }
  247. $page_size = $request->input('page_size', 15);
  248. $books = BookConfigService::getBooks($where, $order, $page_size);
  249. return response()->pagination(new BookTransformer, $books);
  250. }
  251. public function hotWords(Request $request)
  252. {
  253. $result = BookConfigService::findBookKeywords();
  254. return response()->pagination(new KeywordTransformer, $result);
  255. }
  256. public function similarRecom(Request $request)
  257. {
  258. $category_id = $request->input('category_id');
  259. $bid = $request->input('bid');
  260. if (empty($bid) || empty($category_id)) {
  261. return response()->error('PARAM_ERROR');
  262. }
  263. $bid = BookService::decodeBidStatic($bid);
  264. $where = ['category_id' => $category_id, 'is_on_shelf' => [2]];
  265. $books = BookConfigService::getBooks($where, [], 4);
  266. $data = [];
  267. foreach ($books as $v) {
  268. if ($v->bid != $bid && count($data) < 3) {
  269. $data[] = $v;
  270. }
  271. }
  272. return response()->collection(new BookTransformer(), $data);
  273. }
  274. public function readOverRecommend(Request $request)
  275. {
  276. $bid = $request->input('bid');
  277. if (empty($bid)) {
  278. return response()->error('PARAM_ERROR');
  279. }
  280. $bid = BookService::decodeBidStatic($bid);
  281. $book_info = BookConfigService::getBookById($bid);
  282. $res = BookConfigService::getRecommendBooks($bid, $book_info->channel_name);
  283. $urge_status = 0;
  284. if ($book_info->status == 0 && !BookUrgeUpdateService::isHadUrged($this->uid, $bid)) {
  285. $urge_status = 1;
  286. }
  287. $recommend_result = collectionTransform(new BookTransformer(), $res);
  288. $book_status = [
  289. 'status' => $book_info->status,
  290. 'urge_status' => $urge_status
  291. ];
  292. $data = [
  293. 'recommend_result' => $recommend_result,
  294. 'book_status' => $book_status
  295. ];
  296. return response()->success($data);
  297. }
  298. public function rank(Request $request)
  299. {
  300. // 1:男频,2:女频
  301. $sex = (int)$request->input('sex');
  302. if (!in_array($sex, [1, 2], true)) {
  303. return response()->error('PARAM_ERROR');
  304. }
  305. // 默认
  306. $bids = [11529, 11941, 12720, 11990, 11988, 11976, 11977, 4183, 12717, 11833];
  307. if ($sex === 2) {
  308. $bids = [8469, 11660, 9117, 7891, 12281, 12470, 8167, 11661, 11670, 8476, 8557, 11662,
  309. 11680, 11926, 12462, 7836, 11681, 11664, 11928, 8631];
  310. }
  311. /**
  312. * pid:1为男频 2为女频
  313. SELECT
  314. CONCAT( books.id, ',' )
  315. FROM
  316. books
  317. LEFT JOIN book_configs ON books.id = book_configs.bid
  318. WHERE
  319. book_configs.is_on_shelf = 2
  320. AND books.category_id IN ( SELECT id FROM book_categories WHERE pid = 2 )
  321. ORDER BY
  322. book_configs.recommend_index DESC
  323. LIMIT 10;
  324. */
  325. // 根据包名、平台、版本号判断是否审核
  326. $package = $request->header('x-package', '');
  327. $brand = $request->header('x-nbrand', '');
  328. $codeVersion = $request->header('x-codeversion', '');
  329. if (Utils::checkIsAudit($package, $brand, $codeVersion)) {
  330. $bids = [2266, 3838, 9700, 10175, 10301, 3422, 1166, 4546, 9163, 2509];
  331. if ($sex === 2) {
  332. $bids = [159, 2439, 6276, 10074, 5409, 9379, 10323, 9078, 3603, 487];
  333. }
  334. }
  335. $books = collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($bids));
  336. return response()->success($books);
  337. }
  338. /**
  339. * 推荐书
  340. */
  341. public function recommen()
  342. {
  343. $reco_banner_type = ['FEMALE', 'PUBLIC'];
  344. $books = (new RecoBannerService)->getByType($reco_banner_type, 2);
  345. $books->transform(function ($item) {
  346. $result = $this->getBidCidFromUrl($item->redirect_url);
  347. $item->bid = $result['bid'];
  348. $item->cid = $result['cid'];
  349. if ($result['cid']) {
  350. $item->redirect_url = "views/Reader";
  351. } else {
  352. $item->redirect_url = "views/Detail";
  353. }
  354. return $item;
  355. });
  356. return response()->success($books);
  357. }
  358. /**
  359. * 限免
  360. */
  361. public function free(int $sex)
  362. {
  363. $result = BookConfigService::findFreeBooks($sex);
  364. return response()->success($result);
  365. }
  366. }