ReadRecordController.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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($value);
  104. ReadRecordService::delReadRecordStatic($this->uid,[$val->bid]);
  105. }
  106. }
  107. break;
  108. }
  109. }
  110. }
  111. }
  112. if(count($res) > 0){
  113. \Log::info($res);
  114. usort($res, function ($a, $b) {
  115. if ($a['time'] >= $b['time']) return -1;
  116. return 1;
  117. });
  118. return response()->collection(new ReadRecordTransformer(), array_to_object($res));
  119. }
  120. \Log::info("$res");
  121. \Log::info($res);
  122. return response();
  123. }
  124. /**
  125. * @apiVersion 1.0.0
  126. * @apiDescription 添加阅读记录
  127. * @api {post} readrecord 添加阅读记录
  128. * @apiGroup ReadRecord
  129. * @apiName addReadRecord
  130. * @apiHeader {String} [Authorization] token
  131. * @apiParam {Int} book_name 书名
  132. * @apiParam {String} chapter_name 章节名
  133. * @apiParam {Int} bid bid
  134. * @apiParam {Int} cid 章节id
  135. * @apiSuccess {int} code 状态码
  136. * @apiSuccess {String} msg 信息
  137. * @apiSuccess {object} data 结果集
  138. * @apiSuccessExample {json} Success-Response:
  139. * HTTP/1.1 200 OK
  140. * {
  141. * code: 0,
  142. * msg: "",
  143. * data:{}
  144. *
  145. */
  146. public function addReadRecord(Request $request)
  147. {
  148. $param = $request->except('_url');
  149. if (checkParam($param, ['bid', 'cid', 'chapter_name'])) {
  150. return response()->error('LACK_PARAM');
  151. }
  152. $param['uid'] = $this->uid;
  153. $param['bid'] = BookService::decodeBidStatic($param['bid']);
  154. Redis::hget('book_read:' . $this->uid, $param['bid']);
  155. $param['book_name'] = '';
  156. ReadRecordService::addReadRecord($param);
  157. return response()->success();
  158. }
  159. /**
  160. * @apiVersion 1.0.0
  161. * @apiDescription 删除阅读记录
  162. * @api {get} readrecord/delete 删除阅读记录
  163. * @apiGroup ReadRecord
  164. * @apiHeader {String} [Authorization] token
  165. * @apiName delReadRecord
  166. * @apiParam {String} bid 多个bid以,分隔
  167. * @apiSuccess {int} code 状态码
  168. * @apiSuccess {String} msg 信息
  169. * @apiSuccess {object} data 结果集
  170. * @apiSuccessExample {json} Success-Response:
  171. * HTTP/1.1 200 OK
  172. * {
  173. * code: 0,
  174. * msg: "",
  175. * data:{}
  176. *
  177. */
  178. public function delReadRecord(Request $request)
  179. {
  180. $param = $request->except('_url');
  181. if (checkParam($param, ['bid'])) {
  182. return response()->error('LACK_PARAM');
  183. }
  184. $bids = explode(',', $param['bid']);
  185. array_walk($bids, function (&$item) {
  186. $item = BookService::decodeBidStatic($item);
  187. });
  188. ReadRecordService::delReadRecordStatic($this->uid, $bids);
  189. return response()->success();
  190. }
  191. }