BookController.php 15 KB

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