BookController.php 15 KB

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