UserShelfBooksController.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. namespace App\Http\Controllers\QuickApp\User;
  3. use App\Libs\Utils;
  4. use App\Modules\Book\Services\BookConfigService;
  5. use Illuminate\Http\Request;
  6. use App\Http\Controllers\QuickApp\BaseController;
  7. use Redis;
  8. use App\Modules\Book\Services\UserShelfBooksService;
  9. use App\Modules\Book\Services\ChapterService;
  10. use App\Http\Controllers\QuickApp\User\Transformers\UserShelfBooksTransformer;
  11. use App\Modules\User\Services\ReadRecordService;
  12. use Hashids;
  13. use Log;
  14. class UserShelfBooksController extends BaseController
  15. {
  16. /**
  17. * 不用了,于 2020-11-24 15:16废弃
  18. * @deprecated
  19. * @param Request $request
  20. * @return mixed
  21. */
  22. public function index2(Request $request)
  23. {
  24. $res = UserShelfBooksService::getUserShelfBooksListByUid($this->uid);
  25. if ($res->isEmpty()) {
  26. return response()->success();
  27. }
  28. $record = ReadRecordService::getReadRecord($this->uid);
  29. foreach ($res as &$v) {
  30. $bid = $v['bid'];
  31. $last_cid = $v['last_cid'];
  32. $last_chapter = ChapterService::getChapterNameByID($last_cid, $bid);
  33. $v['last_chapter'] = "最后章节:{$last_chapter['name']}";
  34. foreach ($record as $val) {
  35. if ($v['bid'] == $val['bid']) {
  36. $v['updated_at'] = $val['time'];
  37. $recent_reading_cid = $val['cid'];
  38. $v['recent_cid'] = $recent_reading_cid;
  39. $recent_reading_chapter = ChapterService::getChapterNameById($recent_reading_cid, $bid);
  40. $v['recent_reading_chapter'] = "最近阅读:{$recent_reading_chapter['name']}";
  41. break;
  42. }
  43. }
  44. //补充逻辑 书架有、阅读记录没有场景
  45. if(!isset($v['recent_cid']))
  46. {
  47. $v['recent_cid'] = $v['first_cid'];
  48. }
  49. }
  50. return response()->collection(new UserShelfBooksTransformer(), $res);
  51. }
  52. public function index(Request $request){
  53. $record = ReadRecordService::getReadRecord($this->uid);
  54. if(!$record){
  55. return response()->success();
  56. }
  57. $bids = [];
  58. foreach ($record as $item){
  59. $bids[] = $item['bid'];
  60. }
  61. $book_infos = BookConfigService::getBookByField($bids,
  62. ['bid','book_configs.cover','books.author','books.status','books.size','books.intro']);
  63. $cover = [];
  64. $book_base_info = [];
  65. foreach ($book_infos as $book){
  66. $book_base_info[$book->bid] = ['cover'=>$book->cover,'author'=>$book->author,'status'=>$book->status,'intro'=>$book->intro];
  67. $cover[$book->bid] = $book->cover;
  68. }
  69. $result = [];
  70. foreach ($record as $item){
  71. $result[] = [
  72. 'id'=>0,'uid'=>$this->uid,'bid'=>Hashids::encode($item['bid']),
  73. 'book_name'=>$item['book_name'],'cover'=>isset($cover[$item['bid']])?$cover[$item['bid']]:'',
  74. 'first_cid'=>$item['cid'],'last_cid'=>$item['cid'],'last_chapter'=>$item['chapter_name'],
  75. 'recent_reading_chapter'=>$item['chapter_name'],'recent_cid'=>$item['cid'],
  76. 'intro' => isset($book_base_info[$item['bid']]) ? $book_base_info[$item['bid']]['intro']:'',
  77. 'author' => isset($book_base_info[$item['bid']]) ? $book_base_info[$item['bid']]['author']:'',
  78. 'size' => isset($book_base_info[$item['bid']]) ? $book_base_info[$item['bid']]['size']:0,
  79. 'status' =>isset($book_base_info[$item['bid']]) ? $book_base_info[$item['bid']]['status']:0
  80. ];
  81. }
  82. return response()->success($result);
  83. }
  84. /**
  85. * @apiVersion 1.0.0
  86. * @apiDescription 添加书架
  87. * @api {post} userShelfBooks 添加书架
  88. * @apiParam {String} [token] token
  89. * @apiHeader {String} [Authorization] token 两个token任选其一
  90. * @apiGroup UserShelfBooks
  91. * @apiName addShelf
  92. * @apiParam {int} bid bid
  93. * @apiSuccess {int} code 状态码
  94. * @apiSuccess {String} msg 信息
  95. * @apiSuccess {object} data 结果集
  96. * @apiSuccessExample {json} Success-Response:
  97. * HTTP/1.1 200 OK
  98. * {
  99. * code: 0,
  100. * msg: "",
  101. * data: {}
  102. */
  103. public function addShelf(Request $request)
  104. {
  105. $param = $request->except('_url');
  106. if (checkParam($param, ['bid'])) {
  107. return response()->error('LACK_PARAM');
  108. }
  109. $res = true;
  110. try {
  111. $param['uid'] = $this->uid;
  112. $param['bid'] = Hashids::decode($param['bid'])[0];
  113. $param['distribution_channel_id'] = $this->distribution_channel_id;
  114. $res = UserShelfBooksService::create($param);
  115. } catch (\Exception $e) {
  116. return response()->error('QAPP_PARAM_ERROR');
  117. }
  118. if ($res) {
  119. return response()->success($res);
  120. }
  121. return response()->error('QAPP_SYS_ERROR');
  122. }
  123. /**
  124. * @apiVersion 1.0.0
  125. * @apiDescription 删除书架
  126. * @api {get} userShelfBooks/delete 删除书架
  127. * @apiParam {String} [token] token
  128. * @apiHeader {String} [Authorization] token 两个token任选其一
  129. * @apiGroup UserShelfBooks
  130. * @apiName delShelf
  131. * @apiParam {int} bid bid
  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. public function delShelf(Request $request)
  143. {
  144. $uid = $this->uid;
  145. $bid = $request->input('bid');
  146. $decodeBid = Utils::getDecodeId($bid);
  147. if (empty($bid) || empty($decodeBid)) {
  148. return response()->error('LACK_PARAM');
  149. }
  150. // 删除书架中的书籍
  151. //UserShelfBooksService::del($uid, $decodeBid);
  152. // 删除最近阅读记录
  153. ReadRecordService::delReadRecordStatic($uid, [$decodeBid]);
  154. return response()->success();
  155. }
  156. /**
  157. * @apiVersion 1.0.0
  158. * @apiDescription 是否在书架上
  159. * @api {get} userShelfBooks/isonshelf 是否在书架上
  160. * @apiParam {String} [token] token
  161. * @apiHeader {String} [Authorization] token 两个token任选其一
  162. * @apiGroup UserShelfBooks
  163. * @apiName isOnshelf
  164. * @apiParam {int} bid bid
  165. * @apiSuccess {int} code 状态码
  166. * @apiSuccess {String} msg 信息
  167. * @apiSuccess {object} data 结果集
  168. * @apiSuccess {Int} data.is_on 是否在书架上(0|1)
  169. * @apiSuccessExample {json} Success-Response:
  170. * HTTP/1.1 200 OK
  171. * {
  172. * code: 0,
  173. * msg: "",
  174. * data: {
  175. * is_on:0
  176. * }
  177. */
  178. public function isOnshelf(Request $request)
  179. {
  180. $bid = $request->input('bid');
  181. if (!$bid) return response()->error('LACK_PARAM');
  182. $bid = Hashids::decode($bid)[0];
  183. $res = UserShelfBooksService::getUserShelfBooksListByUidAndBid($this->uid, $bid);
  184. if ($res) {
  185. $data['is_on'] = 1;
  186. } else {
  187. $data['is_on'] = 0;
  188. }
  189. return response()->success($data);
  190. }
  191. }