ReadRecordController.php 5.8 KB

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