OutputController.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace Modules\ContentManage\Http\Controllers;
  3. use Catch\Base\CatchController as Controller;
  4. use Illuminate\Http\Request;
  5. use Catch\Exceptions\FailedException;
  6. use Modules\ContentManage\Models\Book;
  7. use Modules\ContentManage\Models\BookCategories;
  8. use Modules\ContentManage\Models\BookChapterContents;
  9. use Modules\ContentManage\Models\BookChapters;
  10. use Modules\ContentManage\Models\Output\OutputChannel;
  11. use Modules\ContentManage\Models\Output\OutputMapping;
  12. /**
  13. * 书籍输出
  14. */
  15. final class OutputController extends Controller{
  16. private $output_channel_id = 0;
  17. private $channel_name = '';
  18. public function __construct(protected readonly Book $book,
  19. protected readonly OutputChannel $outputChannel,
  20. protected readonly OutputMapping $outputMapping,
  21. protected readonly BookChapters $bookChapters,
  22. protected readonly BookChapterContents $bookChapterContents,
  23. protected readonly BookCategories $bookCategories,
  24. Request $request)
  25. {
  26. $this->checkAccess($request);
  27. }
  28. /**
  29. * 渠道参数检验
  30. *
  31. * @param Request $request
  32. * @return void
  33. */
  34. private function checkAccess(Request $request){
  35. $channel_name = $request->get('channel_name');
  36. $channel_key = $request->get('channel_key');
  37. if(empty($channel_name) || empty($channel_key)){
  38. throw new FailedException("缺少参数",11001);
  39. }
  40. $output_channel_info = $this->outputChannel->active()
  41. ->where('channel_name',$channel_name)->select('id','api_key')->first();
  42. if(!$output_channel_info || $output_channel_info->api_key != $channel_key){
  43. throw new FailedException("渠道参数错误",11002);
  44. }
  45. $this->output_channel_id = $output_channel_info->id;
  46. $this->channel_name = $channel_name;
  47. return ;
  48. }
  49. /**
  50. * bid校验
  51. *
  52. * @param [type] $bid
  53. * @return void
  54. */
  55. private function checkBid($bid){
  56. if(!is_numeric($bid)){
  57. throw new FailedException("参数错误",11003);
  58. }
  59. if($this->channel_name == 'zhuishuyun'){
  60. return true;
  61. }
  62. $map_exists = $this->outputMapping->active()->where('bid',$bid)->count();
  63. if(!$map_exists){
  64. throw new FailedException("书籍不存在",11004);
  65. }
  66. }
  67. /**
  68. * 书籍列表
  69. *
  70. * @return void
  71. */
  72. public final function bookList(Request $request){
  73. $cp_name = $request->get('cp_name');
  74. if($cp_name ){
  75. return $this->outputMapping->active()
  76. ->join('books','books.id','=','book_output_mappings.bid')
  77. ->where('books.cp_name',$cp_name)
  78. ->where('output_channel_id',$this->output_channel_id)
  79. ->select('book_output_mappings.bid','books.name as book_name')->get();
  80. }
  81. return $this->outputMapping->active()
  82. ->join('books','books.id','=','book_output_mappings.bid')
  83. ->where('output_channel_id',$this->output_channel_id)
  84. ->where('chapter_count','>',0)
  85. ->select('book_output_mappings.bid','books.name as book_name')->get();
  86. }
  87. /**
  88. * 书籍详情
  89. *
  90. * @param integer $bid
  91. * @return void
  92. */
  93. public final function bookDetail($bid){
  94. $this->checkBid($bid);
  95. return $this->book->firstBy($bid,'id',[
  96. 'id as bid',
  97. 'name as book_name','author','intro as Introduction','cover','status','channel as channelid','cp_name as cp',
  98. 'keyword','size as booklength','category_name','category_id','updated_at','end_date as copyright_end_date'
  99. ]);
  100. }
  101. /**
  102. * 章节列表
  103. *
  104. * @param integer $bid
  105. * @return void
  106. */
  107. public final function chapterList($bid){
  108. $this->checkBid($bid);
  109. return $this->bookChapters->where('bid',$bid)
  110. ->select('id as chapter_id','bid','name as chapter_name','sequence','is_vip','size','updated_at')
  111. ->get();
  112. }
  113. /**
  114. * 章节内容
  115. *
  116. * @param integer $bid
  117. * @param integer $chapter_id
  118. * @return void
  119. */
  120. public final function chapterContent($bid,$chapter_id){
  121. if(!is_numeric($chapter_id)){
  122. throw new FailedException("参数错误",11003);
  123. }
  124. $this->checkBid($bid);
  125. $chapter_info = $this->bookChapters->firstBy($chapter_id,'id',['bid','chapter_content_id','name']);
  126. if(!$chapter_info || $chapter_info->bid != $bid || !$chapter_info->chapter_content_id){
  127. throw new FailedException("章节不存在",11004);
  128. }
  129. $content_info = $this->bookChapterContents->firstBy($chapter_info->chapter_content_id,'id',['content']);
  130. return [
  131. 'chapter_id'=>$chapter_id,
  132. 'chapter_name'=>$chapter_info->name,
  133. 'chapter_content'=>$content_info->content
  134. ];
  135. }
  136. public final function listCategories(){
  137. $category_list = $this->bookCategories->show()
  138. ->select('id','category_name','channel_id','channel_name','pid')->get();
  139. $result = [
  140. ['channel_id'=>1,'channel_name'=>'男频','list'=>[]],
  141. ['channel_id'=>2,'channel_name'=>'女频','list'=>[]],
  142. ];
  143. foreach ($category_list as $item){
  144. //if($item->pid == 0) continue;
  145. $temp = ['category_id'=>$item->id,'category_name'=>$item->category_name];
  146. if($item->channel_id == 1){
  147. $result[0]['list'][] = $temp;
  148. }
  149. if($item->channel_id == 2){
  150. $result[1]['list'][] = $temp;
  151. }
  152. }
  153. return $result;
  154. }
  155. }