ChapterController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace App\Http\Controllers\Channel\Book;
  3. use App\Http\Controllers\Channel\BaseController;
  4. use App\Http\Controllers\Channel\Book\Transformers\ChapterListTransformer;
  5. use App\Http\Controllers\Channel\Book\Transformers\ChapterTransformer;
  6. use App\Modules\Book\Services\ChapterService;
  7. use Hashids;
  8. use Illuminate\Http\Request;
  9. class ChapterController extends BaseController
  10. {
  11. /**
  12. * @apiDefine book 图书模块
  13. */
  14. /**
  15. * @apiVersion 1.0.0
  16. * @apiDescription 获取章节内容
  17. * @api {get} book/getChapterContent 获取章节内容
  18. * @apiGroup book
  19. * @apiParam{Number}bid 图书id
  20. * @apiParam{Number}cid 章节id
  21. * @apiName getChapterContent
  22. * @apiSuccess{int}code 状态码
  23. * @apiSuccess{String}msg 信息
  24. * @apiSuccess{object}data 结果集
  25. * @apiSuccess{Int}data.chapter_id 章节id
  26. * @apiSuccess{String}data.chapter_name 章节名称
  27. * @apiSuccess{Int}data.chapter_sequence 序号
  28. * @apiSuccess{Int}data.chapter_is_vip 是否vip
  29. * @apiSuccess{Int}data.chapter_size 章节大小
  30. * @apiSuccess{Int}data.prev_cid 上一章节id
  31. * @apiSuccess{Int}data.next_cid 下一章节
  32. * @apiSuccess{String}data.recent_update_at 更新时间
  33. * @apiSuccess{String}data.chapter_content 章节内容
  34. * @apiSuccess{Int}data.is_need_subscirbe 是否强制关注(删除)
  35. * @apiSuccessExample {json} Success-Response:
  36. * HTTP/1.1 200 OK
  37. * {
  38. * "code": 0,
  39. * "msg": "",
  40. * "data": {
  41. * "chapter_id": 5,
  42. * "chapter_name": "第1240章 不是我",
  43. * "chapter_sequence": 1239,
  44. * "chapter_is_vip": 1,
  45. * "chapter_size": 2422,
  46. * "prev_cid": 0,
  47. * "next_cid": 0,
  48. * "recent_update_at": 2017-11-20 15:01:56,
  49. * "chapter_content": "叶妩被司行霈的阴阳怪气一吓,思路偏得太远了。 她张口结舌,忘记了自己要说什么。",
  50. * "is_need_subscirbe": 1,(删除)
  51. * }
  52. * }
  53. */
  54. function getChapterContent(Request $request)
  55. {
  56. $bid = $request->has('bid') ? $request->input('bid') : '';
  57. $cid = $request->has('cid') ? $request->input('cid') : '';
  58. if(empty($bid) || empty($cid)) return response()->error("PARAM_EMPTY");
  59. $bid = Hashids::decode($bid)[0];
  60. $chapter = ChapterService::getChapterFromDb($bid, $cid);
  61. if($chapter->is_vip == 1) $chapter->content = '该章节不可预览';
  62. return response()->item(new ChapterTransformer(), $chapter);
  63. }
  64. /**
  65. * @apiVersion 1.0.0
  66. * @apiDescription 获取前五章章节内容
  67. * @api {get} book/getTopFiveChapterContents 获取前五章章节内容
  68. * @apiGroup book
  69. * @apiParam{Number}bid 图书id
  70. * @apiName getTopFiveChapterContents
  71. * @apiSuccess {int}code状态码
  72. * @apiSuccess {String}msg信息
  73. * @apiSuccess {object}data结果集
  74. * @apiSuccess {Int}data.chapter_id 章节id
  75. * @apiSuccess {String}data.chapter_name 章节名称
  76. * @apiSuccess {Int}data.chapter_sequence序号
  77. * @apiSuccess {Int}data.chapter_is_vip是否vip
  78. * @apiSuccess {Int}data.chapter_size章节大小
  79. * @apiSuccess {Int}data.prev_cid上一章节id
  80. * @apiSuccess {Int}data.next_cid下一章节
  81. * @apiSuccess {String}data.recent_update_at更新时间
  82. * @apiSuccess {String}data.chapter_content章节内容
  83. * @apiSuccessExample {json} Success-Response:
  84. * HTTP/1.1 200 OK
  85. * {
  86. * "code": 0,
  87. * "msg": "",
  88. * "data": [{
  89. * "chapter_id": 5,
  90. * "chapter_name": "第1240章 不是我",
  91. * "chapter_sequence": 1239,
  92. * "chapter_is_vip": 1,
  93. * "chapter_size": 2422,
  94. * "prev_cid": 0,
  95. * "next_cid": 0,
  96. * "recent_update_at": 2017-11-20 15:01:56,
  97. * "chapter_content": "叶妩被司行霈的阴阳怪气一吓,思路偏得太远了。 她张口结舌,忘记了自己要说什么。",
  98. * },
  99. * ]
  100. * }
  101. */
  102. function getTopFiveChapterContents(Request $request)
  103. {
  104. $bid = $request->has('bid') ? $request->input('bid') : '';
  105. if (empty($bid)) return response()->error("PARAM_EMPTY");
  106. $bid = Hashids::decode($bid)[0];
  107. $hidden_book = env('HIDE_BOOKS');
  108. if($hidden_book){
  109. $hidden_book_array = explode(',',$hidden_book);
  110. if(in_array($bid,$hidden_book_array)){
  111. return response()->error("PARAM_ERROR");
  112. }
  113. }
  114. $data = ChapterService::getTopFiveChapter($bid);
  115. return response()->collection(new ChapterListTransformer(), $data);
  116. }
  117. }