ReadRecordController.php 6.4 KB

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