ChapterController.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. namespace App\Http\Controllers\WapBrowser\Chapter;
  3. use Illuminate\Http\Request;
  4. use App\Http\Controllers\WapBrowser\BaseController;
  5. use Cookie;
  6. use App\Modules\Book\Services\ChapterService;
  7. use App\Http\Controllers\Wap\Book\Transformers\ChapterTransformer;
  8. use App\Modules\Book\Services\BookConfigService;
  9. use App\Http\Controllers\Wap\Book\Transformers\ChapterListTransformer;
  10. use App\Modules\OfficialAccount\Services\OfficialAccountService;
  11. use App\Http\Controllers\Wap\Book\Transformers\BookTransformer;
  12. use Hashids;
  13. use GuzzleHttp\Client;
  14. use Log;
  15. use DB;
  16. class ChapterController extends BaseController
  17. {
  18. public function bookDetail(Request $request){
  19. $bid = $request->input('bid',0);
  20. $bid = Hashids::decode($bid)[0];
  21. $book_info = BookConfigService::getBookById($bid);
  22. if(!$book_info){
  23. return redirect()->to('/');
  24. }
  25. if(!in_array($book_info->is_on_shelf,[1,2])){
  26. return redirect()->to('/');
  27. }
  28. $book_info['is_on_user_shelf'] = 0;
  29. $book = itemTransform(new BookTransformer(), $book_info);
  30. $where = ['category_id'=>$book['book_category_id'],'is_on_shelf'=>[1]];
  31. $recommend_books = BookConfigService::getBooks($where,[],4);
  32. $data = [];
  33. foreach ($recommend_books as $v){
  34. if($v->bid != $bid && count($data) <3){
  35. $data[] = $v;
  36. }
  37. }
  38. $recommend_books = collectionTransform(new BookTransformer(),$data);
  39. $paginator = ChapterService::getChapterListsPage($bid, 15);
  40. $ret =collectionTransform(new ChapterListTransformer,$paginator);
  41. //return $ret;
  42. //return response()->pagination(new ChapterListTransformer, $res);
  43. $book = json_decode(json_encode($book));
  44. //return;
  45. return view('wap-browser.detail',['title'=>$book->book_name,'book'=>$book,'recommend'=>$recommend_books,'catalog'=>$ret]);
  46. }
  47. public function test(Request $request){
  48. $id = $request->input('id',1);
  49. return Hashids::encode($id);
  50. }
  51. public function index(Request $request)
  52. {
  53. $bid = $request->input('bid',0);
  54. $cid = $request->input('cid',0);
  55. $oldbid = $bid;
  56. if(!is_numeric($bid)){
  57. $bid = Hashids::decode($bid)[0];
  58. }
  59. //获取图书信息
  60. $book_info = BookConfigService::getBookById($bid);
  61. if (empty($book_info)) return redirect()->to('/');
  62. if ($book_info->is_on_shelf == 0 || $book_info->is_on_shelf == 3) {
  63. return view('wap-browser.subscribe',['title'=>'下架']);
  64. }
  65. if ($bid == 1042 && $this->distribution_channel_id == 123) {
  66. return view('wap-browser.subscribe',['title'=>'下架']);
  67. }
  68. if ($book_info->is_on_shelf == 4) {
  69. return view('wap-browser.subscribe',['title'=>'下架']);
  70. }
  71. //获取章节信息
  72. $chapter = ChapterService::getChapterNameById($cid, $bid);
  73. if (!$chapter) {
  74. return redirect()->to('/');
  75. }
  76. //强关
  77. $force_subscribe = $book_info->force_subscribe_chapter_seq > $chapter->sequence;
  78. if ($force_subscribe) {
  79. $content = $this->getChapter($book_info, $chapter);
  80. $content = itemTransform(new ChapterTransformer, $content);
  81. return view('wap-browser.reader',['content'=>$content,'book'=>$book_info]);
  82. }
  83. //需要强制关注 没关注
  84. if (!$force_subscribe) {
  85. return redirect()->to('/subscribe?bid='.$bid.'&cid='.$cid);
  86. }
  87. return redirect()->to('/');
  88. }
  89. /**
  90. * 获取章节内容
  91. * @param $book
  92. * @param $chapter
  93. * @return bool|array
  94. */
  95. protected function getChapter($book, $chapter)
  96. {
  97. $chapter_content = ChapterService::getChapter($book->bid, $chapter->id);
  98. if (!$chapter_content) return false;
  99. $chapter->content = str_replace($chapter_content->name, '', $chapter_content->content);
  100. return $chapter;
  101. }
  102. /**
  103. * 获取公众号信息
  104. * @param $distribution_channel_id
  105. */
  106. protected function getOfficialAccount()
  107. {
  108. $distribution_channel_id = $this->distribution_channel_id;
  109. $res = OfficialAccountService::canUseOfficialAccountByChannelId(compact('distribution_channel_id'));
  110. if (isset($res->nickname) && !empty($res->nickname)) {
  111. Cookie::queue('force_subscribe_name', $res->nickname, env('U_COOKIE_EXPIRE'));
  112. }
  113. return $res;
  114. }
  115. /**
  116. * 获取二维码
  117. * @param $param
  118. * @return bool
  119. */
  120. protected function getRcodeInfo($param, $bid)
  121. {
  122. $client = new Client(['timeout' => 3.0,]);
  123. $param_need = [];
  124. try {
  125. $qrcode_url_res = $client->request('get', 'https://zsyauth.aizhuishu.com/api/get_qrcode_url?' . http_build_query($param_need))->getBody()->getContents();
  126. } catch (\Exception $e) {
  127. }
  128. return false;
  129. }
  130. /* 小说章节目录 */
  131. public function catalog(Request $request)
  132. {
  133. $request_bid = $request->input('bid',0);
  134. // $catalog = [
  135. // [
  136. // 'bid'=>1,
  137. // 'chapter_id'=>2,
  138. // 'chapter_name'=>'第1章 初相见',
  139. // 'chapter_is_vip'=>0,
  140. // ]
  141. // ];
  142. return view('wap-browser.catalog', [
  143. 'title'=>'目录',
  144. 'bid'=>$request_bid
  145. ]);
  146. }
  147. /* 小说章节目录 */
  148. public function apiCatalog(Request $request)
  149. {
  150. $bid = $request->input('bid',0);
  151. if(!is_numeric($bid)){
  152. $bid = Hashids::decode($bid)[0];
  153. }
  154. $page_size = $request->input('page_size', 15);
  155. $res = ChapterService::getChapterListsPage($bid, $page_size);
  156. return response()->pagination(new ChapterListTransformer, $res);
  157. }
  158. }