ReadRecordController.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. $user = $this->user_info;
  64. $res = ReadRecordService::getReadRecord($this->uid);
  65. //补充操作:如果该用户未订阅该下架的书籍则删除其阅读记录(书架不予显示)
  66. //判断是否属于包年用户
  67. $year_account = YearOrderService::getRecord($this->uid);
  68. if ($res) {
  69. $id_arr = [];
  70. foreach ($res as $key => $value) {
  71. $id_arr[] = $value['bid'];
  72. }
  73. $book = BookConfigService::getBooksByIds($id_arr, [], false);//下架图书最近阅读可看到
  74. foreach ($res as $key => &$value) {
  75. $value['cover'] = '';
  76. $value['last_chapter'] = 0;
  77. $value['intro'] = '';
  78. $value['status'] = '';
  79. $value['size'] = 0;
  80. $value['author'] = '';
  81. foreach ($book as $val) {
  82. if ($value['bid'] == $val->bid) {
  83. $value['book_name'] = $val->book_name;
  84. $value['cover'] = $val->cover;
  85. $value['last_chapter'] = $val->last_chapter;
  86. $value['intro'] = $val->intro;
  87. $value['status'] = $val->status;
  88. $value['size'] = $val->size;
  89. $value['author'] = $val->author;
  90. if(!$year_account && !in_array($val->is_on_shelf,[1,2]) ){
  91. //获取书籍充值类型
  92. $charge_type = $val->charge_type;
  93. if($charge_type == 'BOOK'){
  94. //是否购买过该书,购买过则不删除
  95. $result = BookOrderService::getRecordByuidBid($this->uid,$val->bid);
  96. }elseif($charge_type == 'CHAPTER'){
  97. //是否购买过该书章节,购买过则不删除
  98. $result = ChapterOrderService::checkBookIsOrdered($this->uid,$val->bid);
  99. }else{
  100. $result = false;
  101. }
  102. if (!$result) {
  103. unset($res[$key]);
  104. ReadRecordService::delReadRecordStatic($this->uid,[$val->bid]);
  105. }
  106. }
  107. break;
  108. }
  109. }
  110. }
  111. }
  112. usort($res, function ($a, $b) {
  113. if ($a['time'] >= $b['time']) return -1;
  114. return 1;
  115. });
  116. return response()->collection(new ReadRecordTransformer(), array_to_object($res));
  117. }
  118. /**
  119. * @apiVersion 1.0.0
  120. * @apiDescription 添加阅读记录
  121. * @api {post} readrecord 添加阅读记录
  122. * @apiGroup ReadRecord
  123. * @apiName addReadRecord
  124. * @apiHeader {String} [Authorization] token
  125. * @apiParam {Int} book_name 书名
  126. * @apiParam {String} chapter_name 章节名
  127. * @apiParam {Int} bid bid
  128. * @apiParam {Int} cid 章节id
  129. * @apiSuccess {int} code 状态码
  130. * @apiSuccess {String} msg 信息
  131. * @apiSuccess {object} data 结果集
  132. * @apiSuccessExample {json} Success-Response:
  133. * HTTP/1.1 200 OK
  134. * {
  135. * code: 0,
  136. * msg: "",
  137. * data:{}
  138. *
  139. */
  140. public function addReadRecord(Request $request)
  141. {
  142. $param = $request->except('_url');
  143. if (checkParam($param, ['bid', 'cid', 'chapter_name'])) {
  144. return response()->error('LACK_PARAM');
  145. }
  146. $param['uid'] = $this->uid;
  147. $param['bid'] = BookService::decodeBidStatic($param['bid']);
  148. Redis::hget('book_read:' . $this->uid, $param['bid']);
  149. $param['book_name'] = '';
  150. ReadRecordService::addReadRecord($param);
  151. return response()->success();
  152. }
  153. /**
  154. * @apiVersion 1.0.0
  155. * @apiDescription 删除阅读记录
  156. * @api {get} readrecord/delete 删除阅读记录
  157. * @apiGroup ReadRecord
  158. * @apiHeader {String} [Authorization] token
  159. * @apiName delReadRecord
  160. * @apiParam {String} bid 多个bid以,分隔
  161. * @apiSuccess {int} code 状态码
  162. * @apiSuccess {String} msg 信息
  163. * @apiSuccess {object} data 结果集
  164. * @apiSuccessExample {json} Success-Response:
  165. * HTTP/1.1 200 OK
  166. * {
  167. * code: 0,
  168. * msg: "",
  169. * data:{}
  170. *
  171. */
  172. public function delReadRecord(Request $request)
  173. {
  174. $param = $request->except('_url');
  175. if (checkParam($param, ['bid'])) {
  176. return response()->error('LACK_PARAM');
  177. }
  178. $bids = explode(',', $param['bid']);
  179. array_walk($bids, function (&$item) {
  180. $item = BookService::decodeBidStatic($item);
  181. });
  182. ReadRecordService::delReadRecordStatic($this->uid, $bids);
  183. return response()->success();
  184. }
  185. }