ReadRecordController.php 5.9 KB

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