ReadRecordController.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. $is_check_from_db = (time() - strtotime($user->created_at) > 5 * 30 * SysConsts::ONE_DAY_SECONDS);
  62. $res = ReadRecordService::getReadRecord($this->uid, $is_check_from_db);
  63. if ($res) {
  64. $id_arr = [];
  65. foreach ($res as $key => $value) {
  66. $id_arr[] = $value['bid'];
  67. }
  68. $book = BookConfigService::getBooksByIds($id_arr);
  69. foreach ($res as $key => &$value) {
  70. $value['cover'] = '';
  71. $value['last_chapter'] = 0;
  72. foreach ($book as $val) {
  73. if ($value['bid'] == $val->bid) {
  74. $value['cover'] = $val->cover;
  75. $value['last_chapter'] = $val->last_chapter;
  76. break;
  77. }
  78. }
  79. }
  80. $shelf = UserShelfBooksService::getUserShelfBooksListByUid($this->uid);
  81. foreach ($res as &$v) {
  82. $v['is_on_user_shelf'] = 0;
  83. foreach ($shelf as $val) {
  84. if ($v['bid'] == $val->bid) {
  85. $v['is_on_user_shelf'] = 1;
  86. break;
  87. }
  88. }
  89. }
  90. }
  91. usort($res, function ($a, $b) {
  92. if ($a['time'] >= $b['time']) return -1;
  93. return 1;
  94. });
  95. $res = json_encode($res);
  96. $res = json_decode($res);
  97. foreach ($res as $key => &$each) {
  98. $rec = ShareUsersService::getUnlocked($this->uid, $each->bid);
  99. if ($rec) {
  100. $each->is_advertise_sub = 1;
  101. } else {
  102. $each->is_advertise_sub = 0;
  103. }
  104. }
  105. return response()->collection(new ReadRecordTransformer(), $res);
  106. }
  107. /**
  108. * @apiVersion 1.0.0
  109. * @apiDescription 添加阅读记录
  110. * @api {post} readrecord 添加阅读记录
  111. * @apiGroup ReadRecord
  112. * @apiName addReadRecord
  113. * @apiHeader {String} [Authorization] token
  114. * @apiParam {Int} book_name 书名
  115. * @apiParam {String} chapter_name 章节名
  116. * @apiParam {Int} bid bid
  117. * @apiParam {Int} cid 章节id
  118. * @apiSuccess {int} code 状态码
  119. * @apiSuccess {String} msg 信息
  120. * @apiSuccess {object} data 结果集
  121. * @apiSuccessExample {json} Success-Response:
  122. * HTTP/1.1 200 OK
  123. * {
  124. * code: 0,
  125. * msg: "",
  126. * data:{}
  127. *
  128. */
  129. public function addReadRecord(Request $request)
  130. {
  131. $param = $request->except('_url');
  132. if (checkParam($param, ['bid', 'cid', 'chapter_name'])) {
  133. return response()->error('LACK_PARAM');
  134. }
  135. $param['uid'] = $this->uid;
  136. $param['bid'] = BookService::decodeBidStatic($param['bid']);
  137. Redis::hget('book_read:' . $this->uid, $param['bid']);
  138. $param['book_name'] = '';
  139. ReadRecordService::addReadRecord($param);
  140. return response()->success();
  141. }
  142. /**
  143. * @apiVersion 1.0.0
  144. * @apiDescription 删除阅读记录
  145. * @api {get} readrecord/delete 删除阅读记录
  146. * @apiGroup ReadRecord
  147. * @apiHeader {String} [Authorization] token
  148. * @apiName delReadRecord
  149. * @apiParam {String} bid 多个bid以,分隔
  150. * @apiSuccess {int} code 状态码
  151. * @apiSuccess {String} msg 信息
  152. * @apiSuccess {object} data 结果集
  153. * @apiSuccessExample {json} Success-Response:
  154. * HTTP/1.1 200 OK
  155. * {
  156. * code: 0,
  157. * msg: "",
  158. * data:{}
  159. *
  160. */
  161. public function delReadRecord(Request $request)
  162. {
  163. $param = $request->except('_url');
  164. if (checkParam($param, ['bid'])) {
  165. return response()->error('LACK_PARAM');
  166. }
  167. $bids = explode(',', $param['bid']);
  168. array_walk($bids, function (&$item) {
  169. $item = BookService::decodeBidStatic($item);
  170. });
  171. ReadRecordService::delReadRecordStatic($this->uid, $bids);
  172. return response()->success();
  173. }
  174. }