ReadRecordController.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. namespace App\Http\Controllers\QuickApp\User;
  3. use App\Consts\SysConsts;
  4. use App\Modules\Subscribe\Services\BookOrderService;
  5. use App\Modules\Subscribe\Services\ChapterOrderService;
  6. use App\Modules\Subscribe\Services\YearOrderService;
  7. use Illuminate\Http\Request;
  8. use App\Http\Controllers\QuickApp\BaseController;
  9. use App\Modules\Book\Services\BookConfigService;
  10. use App\Modules\Book\Services\UserShelfBooksService;
  11. use App\Http\Controllers\QuickApp\User\Transformers\ReadRecordTransformer;
  12. use App\Modules\Book\Services\BookService;
  13. use App\Modules\ShareFree\Services\ShareUsersService;
  14. use App\Modules\User\Services\ReadRecordService;
  15. use Redis;
  16. class ReadRecordController extends BaseController
  17. {
  18. /**
  19. * @apiDefine ReadRecord 阅读记录
  20. */
  21. /**
  22. * @apiVersion 1.0.0
  23. * @apiDescription 获取阅读记录
  24. * @api {get} readrecord 获取阅读记录
  25. * @apiHeader {String} [Authorization] token
  26. * @apiGroup ReadRecord
  27. * @apiName index
  28. * @apiSuccess {int} code 状态码
  29. * @apiSuccess {String} msg 信息
  30. * @apiSuccess {object} data 结果集
  31. * @apiSuccess {Int} data.book_name 书名
  32. * @apiSuccess {String} data.chapter_name 章节名
  33. * @apiSuccess {Int} data.bid bid
  34. * @apiSuccess {Int} data.time 时间
  35. * @apiSuccess {Int} data.cid 章节id
  36. * @apiSuccess {Int} data.chapter_name 章节名
  37. * @apiSuccess {Int} data.cover 封面
  38. * @apiSuccess {Int} data.last_chapter 最后一张
  39. * @apiSuccessExample {json} Success-Response:
  40. * HTTP/1.1 200 OK
  41. * {
  42. * code: 0,
  43. * msg: "",
  44. * data:[
  45. * {
  46. * book_name: "我来好好爱你",
  47. * bid: 2,
  48. * cid: 10402,
  49. * time: 1511783120,
  50. * chapter_name: "你言重了"
  51. * },
  52. * {
  53. * book_name: "京华烟云",
  54. * bid: 1,
  55. * cid: 4,
  56. * time: 1511783068,
  57. * chapter_name: "背水一战"
  58. * }
  59. * ]
  60. */
  61. public function index(Request $request)
  62. {
  63. $res = ReadRecordService::getReadRecord($this->uid);
  64. $package = $request->header('x-package', '');
  65. //补充操作:如果该用户未订阅该下架的书籍则删除其阅读记录(书架不予显示)
  66. //判断是否属于包年用户
  67. $year_account = YearOrderService::getRecord($this->uid);
  68. if ($res) {
  69. $bids = array_column($res,'bid');
  70. $channel_id = ($package === 'com.beidao.kuaiying.zsy') ? 7477 : 0;
  71. $book = BookConfigService::getBookLists(compact('bids','channel_id'), [], false);//下架图书最近阅读可看到
  72. foreach ($res as $key => &$value) {
  73. $value['cover'] = '';
  74. $value['last_chapter'] = 0;
  75. $value['intro'] = '';
  76. $value['status'] = '';
  77. $value['size'] = 0;
  78. $value['author'] = '';
  79. foreach ($book as $val) {
  80. if ($value['bid'] == $val->bid) {
  81. $value['book_name'] = $val->book_name;
  82. $value['cover'] = $val->cover;
  83. $value['last_chapter'] = $val->last_chapter;
  84. $value['intro'] = $val->intro;
  85. $value['status'] = $val->status;
  86. $value['size'] = $val->size;
  87. $value['author'] = $val->author;
  88. if(!$year_account && !in_array($val->is_on_shelf,[1,2]) ){
  89. //获取书籍充值类型
  90. $charge_type = $val->charge_type;
  91. if($charge_type == 'BOOK'){
  92. //是否购买过该书,购买过则不删除
  93. $result = BookOrderService::getRecordByuidBid($this->uid,$val->bid);
  94. }elseif($charge_type == 'CHAPTER'){
  95. //是否购买过该书章节,购买过则不删除
  96. $result = ChapterOrderService::checkBookIsOrdered($this->uid,$val->bid);
  97. }else{
  98. $result = false;
  99. }
  100. if (!$result) {
  101. unset($res[$key]);
  102. ReadRecordService::delReadRecordStatic($this->uid,[$val->bid]);
  103. }
  104. }
  105. break;
  106. }
  107. }
  108. }
  109. }
  110. usort($res, function ($a, $b) {
  111. if ($a['time'] >= $b['time']) return -1;
  112. return 1;
  113. });
  114. return response()->collection(new ReadRecordTransformer(), array_to_object($res));
  115. }
  116. /**
  117. * @apiVersion 1.0.0
  118. * @apiDescription 添加阅读记录
  119. * @api {post} readrecord 添加阅读记录
  120. * @apiGroup ReadRecord
  121. * @apiName addReadRecord
  122. * @apiHeader {String} [Authorization] token
  123. * @apiParam {Int} book_name 书名
  124. * @apiParam {String} chapter_name 章节名
  125. * @apiParam {Int} bid bid
  126. * @apiParam {Int} cid 章节id
  127. * @apiSuccess {int} code 状态码
  128. * @apiSuccess {String} msg 信息
  129. * @apiSuccess {object} data 结果集
  130. * @apiSuccessExample {json} Success-Response:
  131. * HTTP/1.1 200 OK
  132. * {
  133. * code: 0,
  134. * msg: "",
  135. * data:{}
  136. *
  137. */
  138. public function addReadRecord(Request $request)
  139. {
  140. $param = $request->except('_url');
  141. if (checkParam($param, ['bid', 'cid', 'chapter_name'])) {
  142. return response()->error('LACK_PARAM');
  143. }
  144. $param['uid'] = $this->uid;
  145. $param['bid'] = BookService::decodeBidStatic($param['bid']);
  146. Redis::hget('book_read:' . $this->uid, $param['bid']);
  147. $param['book_name'] = '';
  148. ReadRecordService::addReadRecord($param);
  149. return response()->success();
  150. }
  151. /**
  152. * @apiVersion 1.0.0
  153. * @apiDescription 删除阅读记录
  154. * @api {get} readrecord/delete 删除阅读记录
  155. * @apiGroup ReadRecord
  156. * @apiHeader {String} [Authorization] token
  157. * @apiName delReadRecord
  158. * @apiParam {String} bid 多个bid以,分隔
  159. * @apiSuccess {int} code 状态码
  160. * @apiSuccess {String} msg 信息
  161. * @apiSuccess {object} data 结果集
  162. * @apiSuccessExample {json} Success-Response:
  163. * HTTP/1.1 200 OK
  164. * {
  165. * code: 0,
  166. * msg: "",
  167. * data:{}
  168. *
  169. */
  170. public function delReadRecord(Request $request)
  171. {
  172. $param = $request->except('_url');
  173. if (checkParam($param, ['bid'])) {
  174. return response()->error('LACK_PARAM');
  175. }
  176. $bids = explode(',', $param['bid']);
  177. array_walk($bids, function (&$item) {
  178. $item = BookService::decodeBidStatic($item);
  179. });
  180. ReadRecordService::delReadRecordStatic($this->uid, $bids);
  181. return response()->success();
  182. }
  183. }