UserShelfBooksController.php 5.3 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. $res = true;
  65. try {
  66. $param['uid'] = $this->uid;
  67. $param['bid'] = Hashids::decode($param['bid'])[0];
  68. $param['distribution_channel_id'] = $this->distribution_channel_id;
  69. $res = UserShelfBooksService::create($param);
  70. } catch (\Exception $e) {
  71. return response()->error('QAPP_PARAM_ERROR');
  72. }
  73. if ($res) {
  74. return response()->success($res);
  75. }
  76. return response()->error('QAPP_SYS_ERROR');
  77. }
  78. /**
  79. * @apiVersion 1.0.0
  80. * @apiDescription 删除书架
  81. * @api {get} userShelfBooks/delete 删除书架
  82. * @apiParam {String} [token] token
  83. * @apiHeader {String} [Authorization] token 两个token任选其一
  84. * @apiGroup UserShelfBooks
  85. * @apiName delShelf
  86. * @apiParam {int} bid bid
  87. * @apiSuccess {int} code 状态码
  88. * @apiSuccess {String} msg 信息
  89. * @apiSuccess {object} data 结果集
  90. * @apiSuccessExample {json} Success-Response:
  91. * HTTP/1.1 200 OK
  92. * {
  93. * code: 0,
  94. * msg: "",
  95. * data: {}
  96. */
  97. public function delShelf(Request $request)
  98. {
  99. $bid = $request->input('bid');
  100. if (empty($bid)) return response()->error('LACK_PARAM');
  101. $param['uid'] = $this->uid;
  102. $param['bid'] = Hashids::decode($bid)[0];
  103. $res = UserShelfBooksService::del($this->uid, $param['bid']);
  104. if ($res) {
  105. return response()->success();
  106. }
  107. return response()->error('QAPP_SYS_ERROR');
  108. }
  109. /**
  110. * @apiVersion 1.0.0
  111. * @apiDescription 是否在书架上
  112. * @api {get} userShelfBooks/isonshelf 是否在书架上
  113. * @apiParam {String} [token] token
  114. * @apiHeader {String} [Authorization] token 两个token任选其一
  115. * @apiGroup UserShelfBooks
  116. * @apiName isOnshelf
  117. * @apiParam {int} bid bid
  118. * @apiSuccess {int} code 状态码
  119. * @apiSuccess {String} msg 信息
  120. * @apiSuccess {object} data 结果集
  121. * @apiSuccess {Int} data.is_on 是否在书架上(0|1)
  122. * @apiSuccessExample {json} Success-Response:
  123. * HTTP/1.1 200 OK
  124. * {
  125. * code: 0,
  126. * msg: "",
  127. * data: {
  128. * is_on:0
  129. * }
  130. */
  131. public function isOnshelf(Request $request)
  132. {
  133. $bid = $request->input('bid');
  134. if (!$bid) return response()->error('LACK_PARAM');
  135. $bid = Hashids::decode($bid)[0];
  136. $res = UserShelfBooksService::getUserShelfBooksListByUidAndBid($this->uid, $bid);
  137. if ($res) {
  138. $data['is_on'] = 1;
  139. } else {
  140. $data['is_on'] = 0;
  141. }
  142. return response()->success($data);
  143. }
  144. }