ReadRecordController.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. if(!$year_account && !in_array($val->is_on_shelf,[1,2]) ){
  84. //获取书籍充值类型
  85. $charge_type = $val->charge_type;
  86. if($charge_type == 'BOOK'){
  87. //是否购买过该书,购买过则不删除
  88. $result = BookOrderService::getRecordByuidBid($this->uid,$val->bid);
  89. }elseif($charge_type == 'CHAPTER'){
  90. //是否购买过该书章节,购买过则不删除
  91. $result = ChapterOrderService::checkBookIsOrdered($this->uid,$val->bid);
  92. }else{
  93. $result = false;
  94. }
  95. if ($result) {
  96. ReadRecordService::delReadRecordStatic($this->uid,[$val->bid]);
  97. }
  98. }
  99. $value['book_name'] = $val->book_name;
  100. $value['cover'] = $val->cover;
  101. $value['last_chapter'] = $val->last_chapter;
  102. $value['intro'] = $val->intro;
  103. $value['status'] = $val->status;
  104. $value['size'] = $val->size;
  105. $value['author'] = $val->author;
  106. break;
  107. }
  108. }
  109. }
  110. }
  111. if(count($res) > 0){
  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. return response();
  119. }
  120. /**
  121. * @apiVersion 1.0.0
  122. * @apiDescription 添加阅读记录
  123. * @api {post} readrecord 添加阅读记录
  124. * @apiGroup ReadRecord
  125. * @apiName addReadRecord
  126. * @apiHeader {String} [Authorization] token
  127. * @apiParam {Int} book_name 书名
  128. * @apiParam {String} chapter_name 章节名
  129. * @apiParam {Int} bid bid
  130. * @apiParam {Int} cid 章节id
  131. * @apiSuccess {int} code 状态码
  132. * @apiSuccess {String} msg 信息
  133. * @apiSuccess {object} data 结果集
  134. * @apiSuccessExample {json} Success-Response:
  135. * HTTP/1.1 200 OK
  136. * {
  137. * code: 0,
  138. * msg: "",
  139. * data:{}
  140. *
  141. */
  142. public function addReadRecord(Request $request)
  143. {
  144. $param = $request->except('_url');
  145. if (checkParam($param, ['bid', 'cid', 'chapter_name'])) {
  146. return response()->error('LACK_PARAM');
  147. }
  148. $param['uid'] = $this->uid;
  149. $param['bid'] = BookService::decodeBidStatic($param['bid']);
  150. Redis::hget('book_read:' . $this->uid, $param['bid']);
  151. $param['book_name'] = '';
  152. ReadRecordService::addReadRecord($param);
  153. return response()->success();
  154. }
  155. /**
  156. * @apiVersion 1.0.0
  157. * @apiDescription 删除阅读记录
  158. * @api {get} readrecord/delete 删除阅读记录
  159. * @apiGroup ReadRecord
  160. * @apiHeader {String} [Authorization] token
  161. * @apiName delReadRecord
  162. * @apiParam {String} bid 多个bid以,分隔
  163. * @apiSuccess {int} code 状态码
  164. * @apiSuccess {String} msg 信息
  165. * @apiSuccess {object} data 结果集
  166. * @apiSuccessExample {json} Success-Response:
  167. * HTTP/1.1 200 OK
  168. * {
  169. * code: 0,
  170. * msg: "",
  171. * data:{}
  172. *
  173. */
  174. public function delReadRecord(Request $request)
  175. {
  176. $param = $request->except('_url');
  177. if (checkParam($param, ['bid'])) {
  178. return response()->error('LACK_PARAM');
  179. }
  180. $bids = explode(',', $param['bid']);
  181. array_walk($bids, function (&$item) {
  182. $item = BookService::decodeBidStatic($item);
  183. });
  184. ReadRecordService::delReadRecordStatic($this->uid, $bids);
  185. return response()->success();
  186. }
  187. }