ReadRecordController.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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\User\Services\ReadRecordService;
  9. use Hashids;
  10. use Redis;
  11. use Log;
  12. class ReadRecordController extends BaseController
  13. {
  14. /**
  15. * @apiDefine ReadRecord 阅读记录
  16. */
  17. /**
  18. * @apiVersion 1.0.0
  19. * @apiDescription 获取阅读记录
  20. * @api {get} readrecord 获取阅读记录
  21. * @apiParam {String} [token] token
  22. * @apiHeader {String} [Authorization] token 两个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. $res = ReadRecordService::getReadRecord($this->uid);
  60. if($res){
  61. $id_arr = [];
  62. foreach ($res as $key => $value) {
  63. $id_arr[] = $value['bid'];
  64. }
  65. $book = BookConfigService::getBooksByIds($id_arr);
  66. foreach ($res as $key => &$value) {
  67. foreach ($book as $val) {
  68. if($value['bid'] == $val->bid){
  69. $value['cover'] = $val->cover;
  70. $value['last_chapter'] = $val->last_chapter;
  71. break;
  72. }
  73. }
  74. }
  75. $shelf = UserShelfBooksService::getUserShelfBooksListByUid($this->uid);
  76. foreach ($res as &$v){
  77. $v['is_on_user_shelf'] = 0;
  78. foreach ($shelf as $val){
  79. if($v['bid'] == $val->bid){
  80. $v['is_on_user_shelf'] = 1;
  81. break;
  82. }
  83. }
  84. }
  85. }
  86. usort($res,function($a,$b){
  87. if($a['time'] >= $b['time']) return -1;
  88. return 1;
  89. });
  90. $res = json_encode($res);
  91. $res = json_decode($res);
  92. return response()->collection(new ReadRecordTransformer(),$res);
  93. }
  94. /**
  95. * @apiVersion 1.0.0
  96. * @apiDescription 添加阅读记录
  97. * @api {post} readrecord 添加阅读记录
  98. * @apiGroup ReadRecord
  99. * @apiName addReadRecord
  100. * @apiParam {String} [token] token
  101. * @apiHeader {String} [Authorization] token 两个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. $param = $request->except('_url');
  119. if(checkParam($param,['bid','cid','chapter_name'])){
  120. return response()->error('LACK_PARAM');
  121. }
  122. //Redis::hset('book_read:' . $uid, $bid, "{$cid}_{$book_name}_{$chapter_name}_" . time());
  123. $param['uid'] = $this->uid;
  124. $param['bid'] = Hashids::decode($param['bid'])[0];
  125. $record_info = Redis::hget('book_read:'.$this->uid,$param['bid']);
  126. $param['book_name'] = 'unknown';
  127. if($record_info){
  128. $param['book_name'] = explode('_',$record_info)[1];
  129. }
  130. ReadRecordService::addReadRecord($param);
  131. return response()->success();
  132. }
  133. /**
  134. * @apiVersion 1.0.0
  135. * @apiDescription 删除阅读记录
  136. * @api {get} readrecord/delete 删除阅读记录
  137. * @apiGroup ReadRecord
  138. * @apiParam {String} [token] token
  139. * @apiHeader {String} [Authorization] token 两个token任选其一
  140. * @apiName delReadRecord
  141. * @apiParam {Int} bid bid
  142. * @apiSuccess {int} code 状态码
  143. * @apiSuccess {String} msg 信息
  144. * @apiSuccess {object} data 结果集
  145. * @apiSuccessExample {json} Success-Response:
  146. * HTTP/1.1 200 OK
  147. * {
  148. * code: 0,
  149. * msg: "",
  150. * data:{}
  151. *
  152. */
  153. public function delReadRecord(Request $request){
  154. $param = $request->except('_url');
  155. if(checkParam($param,['bid'])){
  156. return response()->error('LACK_PARAM');
  157. }
  158. $param['bid'] = Hashids::decode($param['bid'])[0];
  159. ReadRecordService::delReadRecord($this->uid,$param['bid']);
  160. return response()->success();
  161. }
  162. }