BookController.php 14 KB

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