BookController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. $is_free = BookConfigService::judgeBookIsFree($bid);
  102. if ($is_free) {
  103. return false;
  104. }
  105. switch ($book_info->charge_type) {
  106. case 'BOOK':
  107. $price = $this->getPrice($book_info);
  108. return $this->isBookNeedCharge($bid, $price);
  109. default:
  110. $price = $last_chapter->is_vip ? $this->getPrice($book_info, $last_chapter->size) : 0;
  111. return $last_chapter->is_vip ? $this->isChapterNeedCharge($bid, $last_chapter->id, $price) : false;
  112. }
  113. }
  114. /**
  115. * 计算价格
  116. * @param $book_info
  117. * @param $chapter_size
  118. * @return float
  119. */
  120. protected function getPrice($book_info, $chapter_size = 0)
  121. {
  122. if ($book_info->charge_type == 'BOOK')
  123. return $book_info->price * 100;
  124. return ceil($chapter_size / 100);
  125. }
  126. /**
  127. * 首页
  128. */
  129. public function getBookLists(Request $request, $sex)
  130. {
  131. // 获取基本数据
  132. $package = $request->header('x-package', '');
  133. $brand = $request->header('x-nbrand', '');
  134. $codeVersion = $request->header('x-codeversion', '');
  135. // 根据包名、平台、版本号判断是否审核
  136. if (Utils::checkIsAudit($package, $brand, $codeVersion)) {
  137. $result = BookAuditService::getHomeBooksData($sex);
  138. return response()->success($result);
  139. }
  140. if ($sex == 'male') {
  141. $channel = 1;
  142. $reco_banner_type = ['MALE', 'PUBLIC'];
  143. } else {
  144. $reco_banner_type = ['FEMALE', 'PUBLIC'];
  145. $channel = 2;
  146. }
  147. $books = (new RecoBannerService)->getByType($reco_banner_type, 2);
  148. $books->transform(function ($item) {
  149. $result = $this->getBidCidFromUrl($item->redirect_url);
  150. $item->bid = $result['bid'];
  151. $item->cid = $result['cid'];
  152. if ($result['cid']) {
  153. $item->redirect_url = "views/Reader";
  154. } else {
  155. $item->redirect_url = "views/Detail";
  156. }
  157. return $item;
  158. });
  159. $hotBids = RecommendService::getRecommendIdsStatic($channel, 'hot');
  160. $liveBids = RecommendService::getRecommendIdsStatic($channel, 'live');
  161. $recomBids = RecommendService::getRecommendIdsStatic($channel, 'recom');
  162. $newBids = RecommendService::getRecommendIdsStatic($channel, 'new_recom');
  163. $result = [
  164. ['type' => 'reco_banner', 'lable' => '首页banner', 'books' => $books],
  165. ['type' => 'hot', 'lable' => '热门推荐', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($hotBids))],
  166. ['type' => 'zhibo', 'lable' => '神书直播', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($liveBids))],
  167. ['type' => 'recom', 'lable' => '编辑推荐', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($recomBids))],
  168. ['type' => 'new_recom', 'lable' => '新书推荐', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($newBids))],
  169. ];
  170. return response()->success($result);
  171. }
  172. private function getBidCidFromUrl(string $url)
  173. {
  174. if (preg_match('/\?bid=(\w+)\S+cid=(\w+)/', $url, $matches) || preg_match('/\?id=(\w+)/', $url, $matches)) {
  175. return [
  176. 'bid' => $matches[1],
  177. 'cid' => isset($matches[2]) ? $matches[2] : 0,
  178. ];
  179. } else {
  180. return [
  181. 'bid' => '',
  182. 'cid' => 0,
  183. ];
  184. }
  185. }
  186. public function library(Request $request)
  187. {
  188. $where = [];
  189. $order = [];
  190. $where['is_on_shelf'] = [2];
  191. $category_id = $request->input('category_id');
  192. if ($category_id) {
  193. if ($category_id == 1) {
  194. $where['channel_name'] = '男频';
  195. } elseif ($category_id == 2) {
  196. $where['channel_name'] = '女频';
  197. } else {
  198. $where['category_id'] = $category_id;
  199. }
  200. }
  201. $key = $request->input('key');
  202. $uid = $request->input('uid', 0);
  203. if ($key && $uid && is_numeric($uid)) {
  204. BookConfigService::saveUserSearchLog($key, $uid);
  205. }
  206. $where['key'] = $key;
  207. $order_field = $request->input('order_field');
  208. $order_seq = $request->input('order_seq');
  209. if ($order_field != '' && in_array($order_field, ['recommend_index', 'click_count', 'update', 'size', 'create'])) {
  210. if ($order_field == 'update') {
  211. $order = ['book_configs.updated_at', 'desc'];
  212. } elseif ($order_field == 'create') {
  213. $order = ['book_configs.created_at', 'desc'];
  214. } else {
  215. $order = [$order_field, 'desc'];
  216. }
  217. if ($order_seq == 'asc') {
  218. $order = [$order_field, 'asc'];
  219. }
  220. if ($order_seq == 'desc') {
  221. $order = [$order_field, 'desc'];
  222. }
  223. }
  224. // 审核状态默认值
  225. $package = $request->header('x-package', '');
  226. $brand = $request->header('x-nbrand', '');
  227. $codeVersion = $request->header('x-codeversion', '');
  228. if ($order_field === 'recommend_index' && Utils::checkIsAudit($package, $brand, $codeVersion)) {
  229. $order = ['book_configs.bid', 'desc'];
  230. }
  231. $status = $request->input('status');
  232. if ($status != '') {
  233. $where['status'] = $status;
  234. }
  235. // 搜索关键词的情况下,屏蔽书籍完本状态
  236. if ($key && isset($where['status'])) {
  237. unset($where['status']);
  238. }
  239. $page_size = $request->input('page_size', 15);
  240. $books = BookConfigService::getBooks($where, $order, $page_size);
  241. return response()->pagination(new BookTransformer, $books);
  242. }
  243. public function hotWords(Request $request)
  244. {
  245. $result = BookConfigService::findBookKeywords();
  246. return response()->pagination(new KeywordTransformer, $result);
  247. }
  248. public function similarRecom(Request $request)
  249. {
  250. $category_id = $request->input('category_id');
  251. $bid = $request->input('bid');
  252. if (empty($bid) || empty($category_id)) {
  253. return response()->error('PARAM_ERROR');
  254. }
  255. $bid = BookService::decodeBidStatic($bid);
  256. $where = ['category_id' => $category_id, 'is_on_shelf' => [2]];
  257. $books = BookConfigService::getBooks($where, [], 4);
  258. $data = [];
  259. foreach ($books as $v) {
  260. if ($v->bid != $bid && count($data) < 3) {
  261. $data[] = $v;
  262. }
  263. }
  264. return response()->collection(new BookTransformer(), $data);
  265. }
  266. public function readOverRecommend(Request $request)
  267. {
  268. $bid = $request->input('bid');
  269. if (empty($bid)) {
  270. return response()->error('PARAM_ERROR');
  271. }
  272. $bid = BookService::decodeBidStatic($bid);
  273. $book_info = BookConfigService::getBookById($bid);
  274. $res = BookConfigService::getRecommendBooks($bid, $book_info->channel_name);
  275. $urge_status = 0;
  276. if ($book_info->status == 0 && !BookUrgeUpdateService::isHadUrged($this->uid, $bid)) {
  277. $urge_status = 1;
  278. }
  279. $recommend_result = collectionTransform(new BookTransformer(), $res);
  280. $book_status = [
  281. 'status' => $book_info->status,
  282. 'urge_status' => $urge_status
  283. ];
  284. $data = [
  285. 'recommend_result' => $recommend_result,
  286. 'book_status' => $book_status
  287. ];
  288. return response()->success($data);
  289. }
  290. public function rank(Request $request)
  291. {
  292. // 1:男频,2:女频
  293. $sex = (int)$request->input('sex');
  294. if (!in_array($sex, [1, 2], true)) {
  295. return response()->error('PARAM_ERROR');
  296. }
  297. // 默认
  298. $bids = [11601, 11529, 3365, 10377, 11457, 8102, 6464, 7287, 2563, 10419];
  299. if ($sex === 2) {
  300. $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];
  301. }
  302. // 根据包名、平台、版本号判断是否审核
  303. $package = $request->header('x-package', '');
  304. $brand = $request->header('x-nbrand', '');
  305. $codeVersion = $request->header('x-codeversion', '');
  306. if (Utils::checkIsAudit($package, $brand, $codeVersion)) {
  307. $bids = [2266, 3838, 9700, 10175, 10301, 3422, 1166, 4546, 9163, 2509];
  308. if ($sex === 2) {
  309. $bids = [159, 2439, 6276, 10074, 5409, 9379, 10323, 9078, 3603, 487];
  310. }
  311. }
  312. $books = collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($bids));
  313. return response()->success($books);
  314. }
  315. /**
  316. * 推荐书
  317. */
  318. public function recommen()
  319. {
  320. $reco_banner_type = ['FEMALE', 'PUBLIC'];
  321. $books = (new RecoBannerService)->getByType($reco_banner_type, 2);
  322. $books->transform(function ($item) {
  323. $result = $this->getBidCidFromUrl($item->redirect_url);
  324. $item->bid = $result['bid'];
  325. $item->cid = $result['cid'];
  326. if ($result['cid']) {
  327. $item->redirect_url = "views/Reader";
  328. } else {
  329. $item->redirect_url = "views/Detail";
  330. }
  331. return $item;
  332. });
  333. return response()->success($books);
  334. }
  335. /**
  336. * 限免
  337. */
  338. public function free(int $sex)
  339. {
  340. $result = BookConfigService::findFreeBooks($sex);
  341. return response()->success($result);
  342. }
  343. }