UserShelfBooksController.php 5.4 KB

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