BookController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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_is_vip'] = $last_chapter['is_vip'];
  40. $book_info['is_need_charge'] = $this->isNeedCharge($bid, $last_chapter, $book_info);
  41. $record = ReadRecordService::getBookReadRecordStatic($this->uid, $bid);
  42. if ($record) {
  43. $book_info['record_chapter_id'] = $record['record_chapter_id'];
  44. $book_info['record_chapter_name'] = $record['record_chapter_name'];
  45. }
  46. return response()->item(new BookTransformer(), $book_info);
  47. }
  48. /**
  49. * 获取订购记录
  50. * @param $book_info
  51. * @param $chapter_id
  52. * @return bool
  53. */
  54. protected function getOrderRecord($bid, $chapter_id)
  55. {
  56. //包年记录
  57. $uid = $this->uid;
  58. $res = YearOrderService::getRecord($uid);
  59. if ($res) return true;
  60. $res = null;
  61. //单本订购记录
  62. $res = BookOrderService::getRecordByuidBid($uid, $bid);
  63. if ($res) return true;
  64. $res = null;
  65. //章节订购记录
  66. $chapterOrder = new ChapterOrderService();
  67. if ($chapterOrder->checkIsOrdered($uid, $bid, $chapter_id)) return true;
  68. return false;
  69. }
  70. /**
  71. * 判断是否需要充值
  72. */
  73. private function isBookNeedCharge(int $bid, float $price)
  74. {
  75. $book_order = $this->getOrderRecord($bid, 0);
  76. if ($book_order) {
  77. return false;
  78. } else {
  79. $user_info = $this->user_info;
  80. return $user_info['balance'] < $price;
  81. }
  82. }
  83. /**
  84. * 判断章节是否需要充值
  85. */
  86. private function isChapterNeedCharge(int $bid, int $cid, float $price)
  87. {
  88. $book_order = $this->getOrderRecord($bid, $cid);
  89. if ($book_order) {
  90. return false;
  91. } else {
  92. $user_info = $this->user_info;
  93. return $user_info['balance'] < $price;
  94. }
  95. }
  96. /**
  97. * 判断是否需要充值
  98. */
  99. private function isNeedCharge(int $bid, $last_chapter, $book_info)
  100. {
  101. switch ($book_info->charge_type) {
  102. case 'BOOK':
  103. $price = $this->getPrice($book_info);
  104. return $this->isBookNeedCharge($bid, $price);
  105. default:
  106. $price = $last_chapter->is_vip ? $this->getPrice($book_info, $last_chapter->size) : 0;
  107. return $last_chapter->is_vip ? $this->isChapterNeedCharge($bid, $last_chapter->id, $price) : false;
  108. }
  109. }
  110. /**
  111. * 计算价格
  112. * @param $book_info
  113. * @param $chapter_size
  114. * @return float
  115. */
  116. protected function getPrice($book_info, $chapter_size = 0)
  117. {
  118. if ($book_info->charge_type == 'BOOK')
  119. return $book_info->price * 100;
  120. return ceil($chapter_size / 100);
  121. }
  122. /**
  123. * 首页
  124. */
  125. public function getBookLists(Request $request, $sex)
  126. {
  127. // 获取基本数据
  128. $package = $request->header('x-package', '');
  129. $brand = $request->header('x-nbrand', '');
  130. $codeVersion = $request->header('x-codeversion', '');
  131. // 根据包名、平台、版本号判断是否审核
  132. if (Utils::checkIsAudit($package, $brand, $codeVersion)) {
  133. $result = BookAuditService::getHomeBooksData($sex);
  134. return response()->success($result);
  135. }
  136. if ($sex == 'male') {
  137. $channel = 1;
  138. $reco_banner_type = ['MALE', 'PUBLIC'];
  139. } else {
  140. $reco_banner_type = ['FEMALE', 'PUBLIC'];
  141. $channel = 2;
  142. }
  143. $books = (new RecoBannerService)->getByType($reco_banner_type, 2);
  144. $books->transform(function ($item) {
  145. $result = $this->getBidCidFromUrl($item->redirect_url);
  146. $item->bid = $result['bid'];
  147. $item->cid = $result['cid'];
  148. if ($result['cid']) {
  149. $item->redirect_url = "views/Reader";
  150. } else {
  151. $item->redirect_url = "views/Detail";
  152. }
  153. return $item;
  154. });
  155. $hotBids = RecommendService::getRecommendIdsStatic($channel, 'hot');
  156. $liveBids = RecommendService::getRecommendIdsStatic($channel, 'live');
  157. $recomBids = RecommendService::getRecommendIdsStatic($channel, 'recom');
  158. $newBids = RecommendService::getRecommendIdsStatic($channel, 'new_recom');
  159. $result = [
  160. ['type' => 'reco_banner', 'lable' => '首页banner', 'books' => $books],
  161. ['type' => 'hot', 'lable' => '热门推荐', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($hotBids))],
  162. ['type' => 'zhibo', 'lable' => '神书直播', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($liveBids))],
  163. ['type' => 'recom', 'lable' => '编辑推荐', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($recomBids))],
  164. ['type' => 'new_recom', 'lable' => '新书推荐', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($newBids))],
  165. ];
  166. return response()->success($result);
  167. }
  168. private function getBidCidFromUrl(string $url)
  169. {
  170. if (preg_match('/\?bid=(\w+)\S+cid=(\w+)/', $url, $matches) || preg_match('/\?id=(\w+)/', $url, $matches)) {
  171. return [
  172. 'bid' => $matches[1],
  173. 'cid' => isset($matches[2]) ? $matches[2] : 0,
  174. ];
  175. } else {
  176. return [
  177. 'bid' => '',
  178. 'cid' => 0,
  179. ];
  180. }
  181. }
  182. public function library(Request $request)
  183. {
  184. $where = [];
  185. $order = [];
  186. $where['is_on_shelf'] = [2];
  187. $category_id = $request->input('category_id');
  188. if ($category_id) {
  189. if ($category_id == 1) {
  190. $where['channel_name'] = '男频';
  191. } elseif ($category_id == 2) {
  192. $where['channel_name'] = '女频';
  193. } else {
  194. $where['category_id'] = $category_id;
  195. }
  196. }
  197. $key = $request->input('key');
  198. $uid = $request->input('uid', 0);
  199. if ($key && $uid && is_numeric($uid)) {
  200. BookConfigService::saveUserSearchLog($key, $uid);
  201. }
  202. $where['key'] = $key;
  203. $order_field = $request->input('order_field');
  204. $order_seq = $request->input('order_seq');
  205. if ($order_field != '' && in_array($order_field, ['recommend_index', 'click_count', 'update', 'size', 'create'])) {
  206. if ($order_field == 'update') {
  207. $order = ['book_configs.updated_at', 'desc'];
  208. } elseif ($order_field == 'create') {
  209. $order = ['book_configs.created_at', 'desc'];
  210. } else {
  211. $order = [$order_field, 'desc'];
  212. }
  213. if ($order_seq == 'asc') {
  214. $order = [$order_field, 'asc'];
  215. }
  216. if ($order_seq == 'desc') {
  217. $order = [$order_field, 'desc'];
  218. }
  219. }
  220. // 审核状态默认值
  221. $package = $request->header('x-package', '');
  222. $brand = $request->header('x-nbrand', '');
  223. $codeVersion = $request->header('x-codeversion', '');
  224. if ($order_field === 'recommend_index' && Utils::checkIsAudit($package, $brand, $codeVersion)) {
  225. $order = ['book_configs.bid', 'desc'];
  226. }
  227. $status = $request->input('status');
  228. if ($status != '') {
  229. $where['status'] = $status;
  230. }
  231. // 搜索关键词的情况下,屏蔽书籍完本状态
  232. if ($key && isset($where['status'])) {
  233. unset($where['status']);
  234. }
  235. $page_size = $request->input('page_size', 15);
  236. $books = BookConfigService::getBooks($where, $order, $page_size);
  237. return response()->pagination(new BookTransformer, $books);
  238. }
  239. public function hotWords(Request $request)
  240. {
  241. $result = BookConfigService::findBookKeywords();
  242. return response()->pagination(new KeywordTransformer, $result);
  243. }
  244. public function similarRecom(Request $request)
  245. {
  246. $category_id = $request->input('category_id');
  247. $bid = $request->input('bid');
  248. if (empty($bid) || empty($category_id)) {
  249. return response()->error('PARAM_ERROR');
  250. }
  251. $bid = BookService::decodeBidStatic($bid);
  252. $where = ['category_id' => $category_id, 'is_on_shelf' => [2]];
  253. $books = BookConfigService::getBooks($where, [], 4);
  254. $data = [];
  255. foreach ($books as $v) {
  256. if ($v->bid != $bid && count($data) < 3) {
  257. $data[] = $v;
  258. }
  259. }
  260. return response()->collection(new BookTransformer(), $data);
  261. }
  262. public function readOverRecommend(Request $request)
  263. {
  264. $bid = $request->input('bid');
  265. if (empty($bid)) {
  266. return response()->error('PARAM_ERROR');
  267. }
  268. $bid = BookService::decodeBidStatic($bid);
  269. $book_info = BookConfigService::getBookById($bid);
  270. $res = BookConfigService::getRecommendBooks($bid, $book_info->channel_name);
  271. $urge_status = 0;
  272. if ($book_info->status == 0 && !BookUrgeUpdateService::isHadUrged($this->uid, $bid)) {
  273. $urge_status = 1;
  274. }
  275. $recommend_result = collectionTransform(new BookTransformer(), $res);
  276. $book_status = [
  277. 'status' => $book_info->status,
  278. 'urge_status' => $urge_status
  279. ];
  280. $data = [
  281. 'recommend_result' => $recommend_result,
  282. 'book_status' => $book_status
  283. ];
  284. return response()->success($data);
  285. }
  286. public function rank(Request $request)
  287. {
  288. // 1:男频,2:女频
  289. $sex = (int)$request->input('sex');
  290. if (!in_array($sex, [1, 2], true)) {
  291. return response()->error('PARAM_ERROR');
  292. }
  293. // 默认
  294. $bids = [11601, 11529, 3365, 10377, 11457, 8102, 6464, 7287, 2563, 10419];
  295. if ($sex === 2) {
  296. $bids = [10823, 10479, 10467, 10139, 9990, 9973, 9479, 9423, 1148, 8693, 8497, 8148, 8129, 7857, 7854, 7629, 7362, 5748, 5362, 4811, 4470, 4135, 3759, 3696, 3418, 3401, 3369, 2698, 1634, 1479];
  297. }
  298. // 根据包名、平台、版本号判断是否审核
  299. $package = $request->header('x-package', '');
  300. $brand = $request->header('x-nbrand', '');
  301. $codeVersion = $request->header('x-codeversion', '');
  302. if (Utils::checkIsAudit($package, $brand, $codeVersion)) {
  303. $bids = [2266, 3838, 9700, 10175, 10301, 3422, 1166, 4546, 9163, 2509];
  304. if ($sex === 2) {
  305. $bids = [159, 2439, 6276, 10074, 5409, 9379, 10323, 9078, 3603, 487];
  306. }
  307. }
  308. $books = collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($bids));
  309. return response()->success($books);
  310. }
  311. /**
  312. * 推荐书
  313. */
  314. public function recommen()
  315. {
  316. $reco_banner_type = ['FEMALE', 'PUBLIC'];
  317. $books = (new RecoBannerService)->getByType($reco_banner_type, 2);
  318. $books->transform(function ($item) {
  319. $result = $this->getBidCidFromUrl($item->redirect_url);
  320. $item->bid = $result['bid'];
  321. $item->cid = $result['cid'];
  322. if ($result['cid']) {
  323. $item->redirect_url = "views/Reader";
  324. } else {
  325. $item->redirect_url = "views/Detail";
  326. }
  327. return $item;
  328. });
  329. return response()->success($books);
  330. }
  331. /**
  332. * 限免
  333. */
  334. public function free(int $sex)
  335. {
  336. $result = BookConfigService::findFreeBooks($sex);
  337. return response()->success($result);
  338. }
  339. }