UserShelfBooksController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace App\Http\Controllers\QuickApp\User;
  3. use Illuminate\Http\Request;
  4. use App\Http\Controllers\QuickApp\BaseController;
  5. use Redis;
  6. use App\Modules\Book\Services\UserShelfBooksService;
  7. use App\Modules\Book\Services\ChapterService;
  8. use App\Http\Controllers\QuickApp\User\Transformers\UserShelfBooksTransformer;
  9. use App\Modules\User\Services\ReadRecordService;
  10. use Hashids;
  11. use Log;
  12. class UserShelfBooksController extends BaseController
  13. {
  14. public function index(Request $request)
  15. {
  16. $res = UserShelfBooksService::getUserShelfBooksListByUid($this->uid);
  17. if ($res->isEmpty()) {
  18. return response()->success();
  19. }
  20. $record = ReadRecordService::getReadRecord($this->uid);
  21. foreach ($res as &$v) {
  22. $bid = $v['bid'];
  23. $last_cid = $v['last_cid'];
  24. $last_chapter = ChapterService::getChapterNameByID($last_cid, $bid);
  25. $v['last_chapter'] = "最后章节:{$last_chapter['name']}";
  26. foreach ($record as $val) {
  27. if ($v['bid'] == $val['bid']) {
  28. $v['updated_at'] = $val['time'];
  29. $recent_reading_cid = $val['cid'];
  30. $v['recent_cid'] = $recent_reading_cid;
  31. $recent_reading_chapter = ChapterService::getChapterNameById($recent_reading_cid, $bid);
  32. $v['recent_reading_chapter'] = "最近阅读:{$recent_reading_chapter['name']}";
  33. break;
  34. }
  35. }
  36. }
  37. return response()->collection(new UserShelfBooksTransformer(), $res);
  38. }
  39. /**
  40. * @apiVersion 1.0.0
  41. * @apiDescription 添加书架
  42. * @api {post} userShelfBooks 添加书架
  43. * @apiParam {String} [token] token
  44. * @apiHeader {String} [Authorization] token 两个token任选其一
  45. * @apiGroup UserShelfBooks
  46. * @apiName addShelf
  47. * @apiParam {int} bid bid
  48. * @apiSuccess {int} code 状态码
  49. * @apiSuccess {String} msg 信息
  50. * @apiSuccess {object} data 结果集
  51. * @apiSuccessExample {json} Success-Response:
  52. * HTTP/1.1 200 OK
  53. * {
  54. * code: 0,
  55. * msg: "",
  56. * data: {}
  57. */
  58. public function addShelf(Request $request)
  59. {
  60. $param = $request->except('_url');
  61. if (checkParam($param, ['bid'])) {
  62. return response()->error('LACK_PARAM');
  63. }
  64. $param['uid'] = $this->uid;
  65. $param['bid'] = Hashids::decode($param['bid'])[0];
  66. $param['distribution_channel_id'] = $this->distribution_channel_id;
  67. $res = UserShelfBooksService::create($param);
  68. try {
  69. } catch (\Exception $e) {
  70. return response()->error('QAPP_PARAM_ERROR');
  71. }
  72. if ($res) {
  73. return response()->success($res);
  74. }
  75. return response()->error('QAPP_SYS_ERROR');
  76. }
  77. /**
  78. * @apiVersion 1.0.0
  79. * @apiDescription 删除书架
  80. * @api {get} userShelfBooks/delete 删除书架
  81. * @apiParam {String} [token] token
  82. * @apiHeader {String} [Authorization] token 两个token任选其一
  83. * @apiGroup UserShelfBooks
  84. * @apiName delShelf
  85. * @apiParam {int} bid bid
  86. * @apiSuccess {int} code 状态码
  87. * @apiSuccess {String} msg 信息
  88. * @apiSuccess {object} data 结果集
  89. * @apiSuccessExample {json} Success-Response:
  90. * HTTP/1.1 200 OK
  91. * {
  92. * code: 0,
  93. * msg: "",
  94. * data: {}
  95. */
  96. public function delShelf(Request $request)
  97. {
  98. $bid = $request->input('bid');
  99. if (empty($bid)) return response()->error('LACK_PARAM');
  100. $param['uid'] = $this->uid;
  101. $param['bid'] = Hashids::decode($bid)[0];
  102. $res = UserShelfBooksService::del($this->uid, $param['bid']);
  103. if ($res) {
  104. return response()->success();
  105. }
  106. return response()->error('QAPP_SYS_ERROR');
  107. }
  108. /**
  109. * @apiVersion 1.0.0
  110. * @apiDescription 是否在书架上
  111. * @api {get} userShelfBooks/isonshelf 是否在书架上
  112. * @apiParam {String} [token] token
  113. * @apiHeader {String} [Authorization] token 两个token任选其一
  114. * @apiGroup UserShelfBooks
  115. * @apiName isOnshelf
  116. * @apiParam {int} bid bid
  117. * @apiSuccess {int} code 状态码
  118. * @apiSuccess {String} msg 信息
  119. * @apiSuccess {object} data 结果集
  120. * @apiSuccess {Int} data.is_on 是否在书架上(0|1)
  121. * @apiSuccessExample {json} Success-Response:
  122. * HTTP/1.1 200 OK
  123. * {
  124. * code: 0,
  125. * msg: "",
  126. * data: {
  127. * is_on:0
  128. * }
  129. */
  130. public function isOnshelf(Request $request)
  131. {
  132. $bid = $request->input('bid');
  133. if (!$bid) return response()->error('LACK_PARAM');
  134. $bid = Hashids::decode($bid)[0];
  135. $res = UserShelfBooksService::getUserShelfBooksListByUidAndBid($this->uid, $bid);
  136. if ($res) {
  137. $data['is_on'] = 1;
  138. } else {
  139. $data['is_on'] = 0;
  140. }
  141. return response()->success($data);
  142. }
  143. }