UserShelfBooksController.php 6.8 KB

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