UserShelfBooksController.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. if(!isset($v['recent_cid']))
  39. {
  40. $v['recent_cid'] = $v['first_cid'];
  41. }
  42. }
  43. return response()->collection(new UserShelfBooksTransformer(), $res);
  44. }
  45. /**
  46. * @apiVersion 1.0.0
  47. * @apiDescription 添加书架
  48. * @api {post} userShelfBooks 添加书架
  49. * @apiParam {String} [token] token
  50. * @apiHeader {String} [Authorization] token 两个token任选其一
  51. * @apiGroup UserShelfBooks
  52. * @apiName addShelf
  53. * @apiParam {int} bid bid
  54. * @apiSuccess {int} code 状态码
  55. * @apiSuccess {String} msg 信息
  56. * @apiSuccess {object} data 结果集
  57. * @apiSuccessExample {json} Success-Response:
  58. * HTTP/1.1 200 OK
  59. * {
  60. * code: 0,
  61. * msg: "",
  62. * data: {}
  63. */
  64. public function addShelf(Request $request)
  65. {
  66. $param = $request->except('_url');
  67. if (checkParam($param, ['bid'])) {
  68. return response()->error('LACK_PARAM');
  69. }
  70. $res = true;
  71. try {
  72. $param['uid'] = $this->uid;
  73. $param['bid'] = Hashids::decode($param['bid'])[0];
  74. $param['distribution_channel_id'] = $this->distribution_channel_id;
  75. $res = UserShelfBooksService::create($param);
  76. } catch (\Exception $e) {
  77. return response()->error('QAPP_PARAM_ERROR');
  78. }
  79. if ($res) {
  80. return response()->success($res);
  81. }
  82. return response()->error('QAPP_SYS_ERROR');
  83. }
  84. /**
  85. * @apiVersion 1.0.0
  86. * @apiDescription 删除书架
  87. * @api {get} userShelfBooks/delete 删除书架
  88. * @apiParam {String} [token] token
  89. * @apiHeader {String} [Authorization] token 两个token任选其一
  90. * @apiGroup UserShelfBooks
  91. * @apiName delShelf
  92. * @apiParam {int} bid bid
  93. * @apiSuccess {int} code 状态码
  94. * @apiSuccess {String} msg 信息
  95. * @apiSuccess {object} data 结果集
  96. * @apiSuccessExample {json} Success-Response:
  97. * HTTP/1.1 200 OK
  98. * {
  99. * code: 0,
  100. * msg: "",
  101. * data: {}
  102. */
  103. public function delShelf(Request $request)
  104. {
  105. $uid = $this->uid;
  106. $bid = $request->input('bid');
  107. $decodeBid = Utils::getDecodeId($bid);
  108. if (empty($bid) || empty($decodeBid)) {
  109. return response()->error('LACK_PARAM');
  110. }
  111. // 删除书架中的书籍
  112. UserShelfBooksService::del($uid, $decodeBid);
  113. // 删除最近阅读记录
  114. ReadRecordService::delReadRecordStatic($uid, [$decodeBid]);
  115. return response()->success();
  116. }
  117. /**
  118. * @apiVersion 1.0.0
  119. * @apiDescription 是否在书架上
  120. * @api {get} userShelfBooks/isonshelf 是否在书架上
  121. * @apiParam {String} [token] token
  122. * @apiHeader {String} [Authorization] token 两个token任选其一
  123. * @apiGroup UserShelfBooks
  124. * @apiName isOnshelf
  125. * @apiParam {int} bid bid
  126. * @apiSuccess {int} code 状态码
  127. * @apiSuccess {String} msg 信息
  128. * @apiSuccess {object} data 结果集
  129. * @apiSuccess {Int} data.is_on 是否在书架上(0|1)
  130. * @apiSuccessExample {json} Success-Response:
  131. * HTTP/1.1 200 OK
  132. * {
  133. * code: 0,
  134. * msg: "",
  135. * data: {
  136. * is_on:0
  137. * }
  138. */
  139. public function isOnshelf(Request $request)
  140. {
  141. $bid = $request->input('bid');
  142. if (!$bid) return response()->error('LACK_PARAM');
  143. $bid = Hashids::decode($bid)[0];
  144. $res = UserShelfBooksService::getUserShelfBooksListByUidAndBid($this->uid, $bid);
  145. if ($res) {
  146. $data['is_on'] = 1;
  147. } else {
  148. $data['is_on'] = 0;
  149. }
  150. return response()->success($data);
  151. }
  152. }