ReadRecordController.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. namespace App\Http\Controllers\KuaiYingYong\User;
  3. use Illuminate\Http\Request;
  4. use App\Http\Controllers\KuaiYingYong\BaseController;
  5. use App\Modules\Book\Services\BookConfigService;
  6. use App\Modules\Book\Services\UserShelfBooksService;
  7. use App\Http\Controllers\KuaiYingYong\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. if(!$this->checkUid()){
  60. return response()->error('XCX_NOT_LOGIN');
  61. }
  62. $res = ReadRecordService::getReadRecord($this->uid);
  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. foreach ($book as $val) {
  71. if($value['bid'] == $val->bid){
  72. $value['cover'] = $val->cover;
  73. $value['last_chapter'] = $val->last_chapter;
  74. break;
  75. }
  76. }
  77. }
  78. $shelf = UserShelfBooksService::getUserShelfBooksListByUid($this->uid);
  79. foreach ($res as &$v){
  80. $v['is_on_user_shelf'] = 0;
  81. foreach ($shelf as $val){
  82. if($v['bid'] == $val->bid){
  83. $v['is_on_user_shelf'] = 1;
  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. $res = json_encode($res);
  94. $res = json_decode($res);
  95. return response()->collection(new ReadRecordTransformer(),$res);
  96. }
  97. /**
  98. * @apiVersion 1.0.0
  99. * @apiDescription 添加阅读记录
  100. * @api {post} readrecord 添加阅读记录
  101. * @apiGroup ReadRecord
  102. * @apiName addReadRecord
  103. * @apiParam {String} [token] token
  104. * @apiHeader {String} [Authorization] token 两个token任选其一
  105. * @apiParam {Int} book_name 书名
  106. * @apiParam {String} chapter_name 章节名
  107. * @apiParam {Int} bid bid
  108. * @apiParam {Int} cid 章节id
  109. * @apiSuccess {int} code 状态码
  110. * @apiSuccess {String} msg 信息
  111. * @apiSuccess {object} data 结果集
  112. * @apiSuccessExample {json} Success-Response:
  113. * HTTP/1.1 200 OK
  114. * {
  115. * code: 0,
  116. * msg: "",
  117. * data:{}
  118. *
  119. */
  120. public function addReadRecord(Request $request){
  121. if(!$this->checkUid()){
  122. return response()->error('XCX_NOT_LOGIN');
  123. }
  124. $param = $request->except('_url');
  125. if(checkParam($param,['bid','cid','chapter_name'])){
  126. return response()->error('LACK_PARAM');
  127. }
  128. //Redis::hset('book_read:' . $uid, $bid, "{$cid}_{$book_name}_{$chapter_name}_" . time());
  129. $param['uid'] = $this->uid;
  130. $param['bid'] = Hashids::decode($param['bid'])[0];
  131. $record_info = Redis::hget('book_read:'.$this->uid,$param['bid']);
  132. $param['book_name'] = 'unknown';
  133. if($record_info){
  134. $param['book_name'] = explode('_',$record_info)[1];
  135. }
  136. ReadRecordService::addReadRecord($param);
  137. return response()->success();
  138. }
  139. /**
  140. * @apiVersion 1.0.0
  141. * @apiDescription 删除阅读记录
  142. * @api {get} readrecord/delete 删除阅读记录
  143. * @apiGroup ReadRecord
  144. * @apiParam {String} [token] token
  145. * @apiHeader {String} [Authorization] token 两个token任选其一
  146. * @apiName delReadRecord
  147. * @apiParam {Int} bid bid
  148. * @apiSuccess {int} code 状态码
  149. * @apiSuccess {String} msg 信息
  150. * @apiSuccess {object} data 结果集
  151. * @apiSuccessExample {json} Success-Response:
  152. * HTTP/1.1 200 OK
  153. * {
  154. * code: 0,
  155. * msg: "",
  156. * data:{}
  157. *
  158. */
  159. public function delReadRecord(Request $request){
  160. if(!$this->checkUid()){
  161. return response()->error('XCX_NOT_LOGIN');
  162. }
  163. $param = $request->except('_url');
  164. if(checkParam($param,['bid'])){
  165. return response()->error('LACK_PARAM');
  166. }
  167. $param['bid'] = Hashids::decode($param['bid'])[0];
  168. ReadRecordService::delReadRecord($this->uid,$param['bid']);
  169. return response()->success();
  170. }
  171. }