UserShelfBooksController.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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,
  67. 'intro'=>$book->intro,'size'=>$book->size];
  68. $cover[$book->bid] = $book->cover;
  69. }
  70. $result = [];
  71. foreach ($record as $item){
  72. $result[] = [
  73. 'id'=>0,'uid'=>$this->uid,'bid'=>Hashids::encode($item['bid']),
  74. 'book_name'=>$item['book_name'],'cover'=>isset($cover[$item['bid']])?$cover[$item['bid']]:'',
  75. 'first_cid'=>$item['cid'],'last_cid'=>$item['cid'],'last_chapter'=>$item['chapter_name'],
  76. 'recent_reading_chapter'=>$item['chapter_name'],'recent_cid'=>$item['cid'],
  77. 'intro' => isset($book_base_info[$item['bid']]) ? $book_base_info[$item['bid']]['intro']:'',
  78. 'author' => isset($book_base_info[$item['bid']]) ? $book_base_info[$item['bid']]['author']:'',
  79. 'size' => isset($book_base_info[$item['bid']]) ? $book_base_info[$item['bid']]['size']:0,
  80. 'status' =>isset($book_base_info[$item['bid']]) ? $book_base_info[$item['bid']]['status']:0
  81. ];
  82. }
  83. return response()->success($result);
  84. }
  85. /**
  86. * @apiVersion 1.0.0
  87. * @apiDescription 添加书架
  88. * @api {post} userShelfBooks 添加书架
  89. * @apiParam {String} [token] token
  90. * @apiHeader {String} [Authorization] token 两个token任选其一
  91. * @apiGroup UserShelfBooks
  92. * @apiName addShelf
  93. * @apiParam {int} bid bid
  94. * @apiSuccess {int} code 状态码
  95. * @apiSuccess {String} msg 信息
  96. * @apiSuccess {object} data 结果集
  97. * @apiSuccessExample {json} Success-Response:
  98. * HTTP/1.1 200 OK
  99. * {
  100. * code: 0,
  101. * msg: "",
  102. * data: {}
  103. */
  104. public function addShelf(Request $request)
  105. {
  106. $param = $request->except('_url');
  107. if (checkParam($param, ['bid'])) {
  108. return response()->error('LACK_PARAM');
  109. }
  110. $res = true;
  111. try {
  112. $param['uid'] = $this->uid;
  113. $param['bid'] = Hashids::decode($param['bid'])[0];
  114. $param['distribution_channel_id'] = $this->distribution_channel_id;
  115. $res = UserShelfBooksService::create($param);
  116. } catch (\Exception $e) {
  117. return response()->error('QAPP_PARAM_ERROR');
  118. }
  119. if ($res) {
  120. return response()->success($res);
  121. }
  122. return response()->error('QAPP_SYS_ERROR');
  123. }
  124. /**
  125. * @apiVersion 1.0.0
  126. * @apiDescription 删除书架
  127. * @api {get} userShelfBooks/delete 删除书架
  128. * @apiParam {String} [token] token
  129. * @apiHeader {String} [Authorization] token 两个token任选其一
  130. * @apiGroup UserShelfBooks
  131. * @apiName delShelf
  132. * @apiParam {int} bid bid
  133. * @apiSuccess {int} code 状态码
  134. * @apiSuccess {String} msg 信息
  135. * @apiSuccess {object} data 结果集
  136. * @apiSuccessExample {json} Success-Response:
  137. * HTTP/1.1 200 OK
  138. * {
  139. * code: 0,
  140. * msg: "",
  141. * data: {}
  142. */
  143. public function delShelf(Request $request)
  144. {
  145. $uid = $this->uid;
  146. $bid = $request->input('bid');
  147. $decodeBid = Utils::getDecodeId($bid);
  148. if (empty($bid) || empty($decodeBid)) {
  149. return response()->error('LACK_PARAM');
  150. }
  151. // 删除书架中的书籍
  152. //UserShelfBooksService::del($uid, $decodeBid);
  153. // 删除最近阅读记录
  154. ReadRecordService::delReadRecordStatic($uid, [$decodeBid]);
  155. return response()->success();
  156. }
  157. /**
  158. * @apiVersion 1.0.0
  159. * @apiDescription 是否在书架上
  160. * @api {get} userShelfBooks/isonshelf 是否在书架上
  161. * @apiParam {String} [token] token
  162. * @apiHeader {String} [Authorization] token 两个token任选其一
  163. * @apiGroup UserShelfBooks
  164. * @apiName isOnshelf
  165. * @apiParam {int} bid bid
  166. * @apiSuccess {int} code 状态码
  167. * @apiSuccess {String} msg 信息
  168. * @apiSuccess {object} data 结果集
  169. * @apiSuccess {Int} data.is_on 是否在书架上(0|1)
  170. * @apiSuccessExample {json} Success-Response:
  171. * HTTP/1.1 200 OK
  172. * {
  173. * code: 0,
  174. * msg: "",
  175. * data: {
  176. * is_on:0
  177. * }
  178. */
  179. public function isOnshelf(Request $request)
  180. {
  181. $bid = $request->input('bid');
  182. if (!$bid) return response()->error('LACK_PARAM');
  183. $bid = Hashids::decode($bid)[0];
  184. $res = UserShelfBooksService::getUserShelfBooksListByUidAndBid($this->uid, $bid);
  185. if ($res) {
  186. $data['is_on'] = 1;
  187. } else {
  188. $data['is_on'] = 0;
  189. }
  190. return response()->success($data);
  191. }
  192. }