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