ReadRecordController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace App\Http\Controllers\QuickApp\User;
  3. use App\Consts\SysConsts;
  4. use Illuminate\Http\Request;
  5. use App\Http\Controllers\QuickApp\BaseController;
  6. use App\Modules\Book\Services\BookConfigService;
  7. use App\Modules\Book\Services\UserShelfBooksService;
  8. use App\Http\Controllers\QuickApp\User\Transformers\ReadRecordTransformer;
  9. use App\Modules\Book\Services\BookService;
  10. use App\Modules\ShareFree\Services\ShareUsersService;
  11. use App\Modules\User\Services\ReadRecordService;
  12. use Redis;
  13. class ReadRecordController extends BaseController
  14. {
  15. /**
  16. * @apiDefine ReadRecord 阅读记录
  17. */
  18. /**
  19. * @apiVersion 1.0.0
  20. * @apiDescription 获取阅读记录
  21. * @api {get} readrecord 获取阅读记录
  22. * @apiHeader {String} [Authorization] token
  23. * @apiGroup ReadRecord
  24. * @apiName index
  25. * @apiSuccess {int} code 状态码
  26. * @apiSuccess {String} msg 信息
  27. * @apiSuccess {object} data 结果集
  28. * @apiSuccess {Int} data.book_name 书名
  29. * @apiSuccess {String} data.chapter_name 章节名
  30. * @apiSuccess {Int} data.bid bid
  31. * @apiSuccess {Int} data.time 时间
  32. * @apiSuccess {Int} data.cid 章节id
  33. * @apiSuccess {Int} data.chapter_name 章节名
  34. * @apiSuccess {Int} data.cover 封面
  35. * @apiSuccess {Int} data.last_chapter 最后一张
  36. * @apiSuccessExample {json} Success-Response:
  37. * HTTP/1.1 200 OK
  38. * {
  39. * code: 0,
  40. * msg: "",
  41. * data:[
  42. * {
  43. * book_name: "我来好好爱你",
  44. * bid: 2,
  45. * cid: 10402,
  46. * time: 1511783120,
  47. * chapter_name: "你言重了"
  48. * },
  49. * {
  50. * book_name: "京华烟云",
  51. * bid: 1,
  52. * cid: 4,
  53. * time: 1511783068,
  54. * chapter_name: "背水一战"
  55. * }
  56. * ]
  57. */
  58. public function index(Request $request)
  59. {
  60. $user = $this->user_info;
  61. $res = ReadRecordService::getReadRecord($this->uid);
  62. if ($res) {
  63. $id_arr = [];
  64. foreach ($res as $key => $value) {
  65. $id_arr[] = $value['bid'];
  66. }
  67. $book = BookConfigService::getBooksByIds($id_arr);
  68. foreach ($res as $key => &$value) {
  69. $value['cover'] = '';
  70. $value['last_chapter'] = 0;
  71. foreach ($book as $val) {
  72. if ($value['bid'] == $val->bid) {
  73. $value['cover'] = $val->cover;
  74. $value['last_chapter'] = $val->last_chapter;
  75. break;
  76. }
  77. }
  78. }
  79. }
  80. usort($res, function ($a, $b) {
  81. if ($a['time'] >= $b['time']) return -1;
  82. return 1;
  83. });
  84. return response()->collection(new ReadRecordTransformer(), array_to_object($res));
  85. }
  86. /**
  87. * @apiVersion 1.0.0
  88. * @apiDescription 添加阅读记录
  89. * @api {post} readrecord 添加阅读记录
  90. * @apiGroup ReadRecord
  91. * @apiName addReadRecord
  92. * @apiHeader {String} [Authorization] token
  93. * @apiParam {Int} book_name 书名
  94. * @apiParam {String} chapter_name 章节名
  95. * @apiParam {Int} bid bid
  96. * @apiParam {Int} cid 章节id
  97. * @apiSuccess {int} code 状态码
  98. * @apiSuccess {String} msg 信息
  99. * @apiSuccess {object} data 结果集
  100. * @apiSuccessExample {json} Success-Response:
  101. * HTTP/1.1 200 OK
  102. * {
  103. * code: 0,
  104. * msg: "",
  105. * data:{}
  106. *
  107. */
  108. public function addReadRecord(Request $request)
  109. {
  110. $param = $request->except('_url');
  111. if (checkParam($param, ['bid', 'cid', 'chapter_name'])) {
  112. return response()->error('LACK_PARAM');
  113. }
  114. $param['uid'] = $this->uid;
  115. $param['bid'] = BookService::decodeBidStatic($param['bid']);
  116. Redis::hget('book_read:' . $this->uid, $param['bid']);
  117. $param['book_name'] = '';
  118. ReadRecordService::addReadRecord($param);
  119. return response()->success();
  120. }
  121. /**
  122. * @apiVersion 1.0.0
  123. * @apiDescription 删除阅读记录
  124. * @api {get} readrecord/delete 删除阅读记录
  125. * @apiGroup ReadRecord
  126. * @apiHeader {String} [Authorization] token
  127. * @apiName delReadRecord
  128. * @apiParam {String} bid 多个bid以,分隔
  129. * @apiSuccess {int} code 状态码
  130. * @apiSuccess {String} msg 信息
  131. * @apiSuccess {object} data 结果集
  132. * @apiSuccessExample {json} Success-Response:
  133. * HTTP/1.1 200 OK
  134. * {
  135. * code: 0,
  136. * msg: "",
  137. * data:{}
  138. *
  139. */
  140. public function delReadRecord(Request $request)
  141. {
  142. $param = $request->except('_url');
  143. if (checkParam($param, ['bid'])) {
  144. return response()->error('LACK_PARAM');
  145. }
  146. $bids = explode(',', $param['bid']);
  147. array_walk($bids, function (&$item) {
  148. $item = BookService::decodeBidStatic($item);
  149. });
  150. ReadRecordService::delReadRecordStatic($this->uid, $bids);
  151. return response()->success();
  152. }
  153. }