BookController.php 16 KB

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