ReadRecordController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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['book_name'] = $val->book_name;
  74. $value['cover'] = $val->cover;
  75. $value['last_chapter'] = $val->last_chapter;
  76. break;
  77. }
  78. }
  79. }
  80. }
  81. usort($res, function ($a, $b) {
  82. if ($a['time'] >= $b['time']) return -1;
  83. return 1;
  84. });
  85. return response()->collection(new ReadRecordTransformer(), array_to_object($res));
  86. }
  87. /**
  88. * @apiVersion 1.0.0
  89. * @apiDescription 添加阅读记录
  90. * @api {post} readrecord 添加阅读记录
  91. * @apiGroup ReadRecord
  92. * @apiName addReadRecord
  93. * @apiHeader {String} [Authorization] token
  94. * @apiParam {Int} book_name 书名
  95. * @apiParam {String} chapter_name 章节名
  96. * @apiParam {Int} bid bid
  97. * @apiParam {Int} cid 章节id
  98. * @apiSuccess {int} code 状态码
  99. * @apiSuccess {String} msg 信息
  100. * @apiSuccess {object} data 结果集
  101. * @apiSuccessExample {json} Success-Response:
  102. * HTTP/1.1 200 OK
  103. * {
  104. * code: 0,
  105. * msg: "",
  106. * data:{}
  107. *
  108. */
  109. public function addReadRecord(Request $request)
  110. {
  111. $param = $request->except('_url');
  112. if (checkParam($param, ['bid', 'cid', 'chapter_name'])) {
  113. return response()->error('LACK_PARAM');
  114. }
  115. $param['uid'] = $this->uid;
  116. $param['bid'] = BookService::decodeBidStatic($param['bid']);
  117. Redis::hget('book_read:' . $this->uid, $param['bid']);
  118. $param['book_name'] = '';
  119. ReadRecordService::addReadRecord($param);
  120. return response()->success();
  121. }
  122. /**
  123. * @apiVersion 1.0.0
  124. * @apiDescription 删除阅读记录
  125. * @api {get} readrecord/delete 删除阅读记录
  126. * @apiGroup ReadRecord
  127. * @apiHeader {String} [Authorization] token
  128. * @apiName delReadRecord
  129. * @apiParam {String} bid 多个bid以,分隔
  130. * @apiSuccess {int} code 状态码
  131. * @apiSuccess {String} msg 信息
  132. * @apiSuccess {object} data 结果集
  133. * @apiSuccessExample {json} Success-Response:
  134. * HTTP/1.1 200 OK
  135. * {
  136. * code: 0,
  137. * msg: "",
  138. * data:{}
  139. *
  140. */
  141. public function delReadRecord(Request $request)
  142. {
  143. $param = $request->except('_url');
  144. if (checkParam($param, ['bid'])) {
  145. return response()->error('LACK_PARAM');
  146. }
  147. $bids = explode(',', $param['bid']);
  148. array_walk($bids, function (&$item) {
  149. $item = BookService::decodeBidStatic($item);
  150. });
  151. ReadRecordService::delReadRecordStatic($this->uid, $bids);
  152. return response()->success();
  153. }
  154. }