UserShelfBooksController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. return response()->success();
  61. $param = $request->except('_url');
  62. if (checkParam($param, ['bid'])) {
  63. return response()->error('LACK_PARAM');
  64. }
  65. $param['uid'] = $this->uid;
  66. $param['bid'] = Hashids::decode($param['bid'])[0];
  67. $param['distribution_channel_id'] = $this->distribution_channel_id;
  68. $res = null;
  69. $res = UserShelfBooksService::create($param);
  70. try {
  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. $bid = $request->input('bid');
  101. if (empty($bid)) return response()->error('LACK_PARAM');
  102. $param['uid'] = $this->uid;
  103. $param['bid'] = Hashids::decode($bid)[0];
  104. $res = UserShelfBooksService::del($this->uid, $param['bid']);
  105. if ($res) {
  106. return response()->success();
  107. }
  108. return response()->error('QAPP_SYS_ERROR');
  109. }
  110. /**
  111. * @apiVersion 1.0.0
  112. * @apiDescription 是否在书架上
  113. * @api {get} userShelfBooks/isonshelf 是否在书架上
  114. * @apiParam {String} [token] token
  115. * @apiHeader {String} [Authorization] token 两个token任选其一
  116. * @apiGroup UserShelfBooks
  117. * @apiName isOnshelf
  118. * @apiParam {int} bid bid
  119. * @apiSuccess {int} code 状态码
  120. * @apiSuccess {String} msg 信息
  121. * @apiSuccess {object} data 结果集
  122. * @apiSuccess {Int} data.is_on 是否在书架上(0|1)
  123. * @apiSuccessExample {json} Success-Response:
  124. * HTTP/1.1 200 OK
  125. * {
  126. * code: 0,
  127. * msg: "",
  128. * data: {
  129. * is_on:0
  130. * }
  131. */
  132. public function isOnshelf(Request $request)
  133. {
  134. $bid = $request->input('bid');
  135. if (!$bid) return response()->error('LACK_PARAM');
  136. $bid = Hashids::decode($bid)[0];
  137. $res = UserShelfBooksService::getUserShelfBooksListByUidAndBid($this->uid, $bid);
  138. if ($res) {
  139. $data['is_on'] = 1;
  140. } else {
  141. $data['is_on'] = 0;
  142. }
  143. return response()->success($data);
  144. }
  145. }