UserShelfBooksController.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. namespace App\Http\Controllers\QuickApp\User;
  3. use Illuminate\Http\Request;
  4. use App\Http\Controllers\QuickApp\BaseController;
  5. use Redis;
  6. use App\Modules\Book\Services\UserShelfBooksService;
  7. use App\Modules\Book\Services\ChapterService;
  8. use App\Http\Controllers\QuickApp\User\Transformers\UserShelfBooksTransformer;
  9. use App\Modules\User\Services\ReadRecordService;
  10. use Hashids;
  11. use Log;
  12. class UserShelfBooksController extends BaseController
  13. {
  14. /**
  15. * @apiDefine UserShelfBooks 书架
  16. */
  17. /**
  18. * @apiVersion 1.0.0
  19. * @apiDescription 获取书架
  20. * @api {get} userShelfBooks 获取书架
  21. * @apiParam {String} [token] token
  22. * @apiHeader {String} [Authorization] token 两个token任选其一
  23. * @apiGroup UserShelfBooks
  24. * @apiName getChapter
  25. * @apiSuccess {int} code 状态码
  26. * @apiSuccess {String} msg 信息
  27. * @apiSuccess {object} data 结果集
  28. * @apiSuccess {Int} data.id 编号
  29. * @apiSuccess {String} data.distribution_channel_id 分销
  30. * @apiSuccess {Int} data.uid uid
  31. * @apiSuccess {Int} data.bid bid
  32. * @apiSuccess {Int} data.book_name 书名
  33. * @apiSuccess {Int} data.cover 封面
  34. * @apiSuccess {Int} data.last_cid 最后章节id
  35. * @apiSuccess {String} data.last_chapter 最新章节
  36. * @apiSuccess {String} data.last_chapter 最近阅读章节
  37. * @apiSuccessExample {json} Success-Response:
  38. * HTTP/1.1 200 OK
  39. * {
  40. * code: 0,
  41. * msg: "",
  42. * data: {
  43. * [
  44. * {
  45. * id: 6,
  46. * uid: 4,
  47. * distribution_channel_id: 1,
  48. * bid: 1,
  49. * book_name: "京华烟云",
  50. * cover: "https://leyue-bucket.oss-cn-hangzhou.aliyuncs.com/ycsd_cover/covermiddle/0/1.jpg",
  51. * updated_at: 1511783068,
  52. * last_cid: 4,
  53. * last_chapter:"最新 第122章 大事不妙",
  54. * recent_reading_chapter:"最近阅读:第5章 又来?"
  55. *
  56. * },
  57. * {
  58. * id: 7,
  59. * uid: 4,
  60. * distribution_channel_id: 1,
  61. * bid: 1,
  62. * book_name: "我来好好爱你",
  63. * cover: "https://leyue-bucket.oss-cn-hangzhou.aliyuncs.com/ycsd_cover/covermiddle/0/11.jpg",
  64. * updated_at: 1511783068,
  65. * last_cid: 4,
  66. * last_chapter:"最新 第2556章 又来了个张老三",
  67. * recent_reading_chapter:"最近阅读:第2551章 又来了个猪八戒"
  68. * }
  69. * ]
  70. * }
  71. */
  72. public function index(Request $request){
  73. $res = UserShelfBooksService::getUserShelfBooksListByUid($this->uid);
  74. if($res->isEmpty()){
  75. return response()->success();
  76. }
  77. $record = ReadRecordService::getReadRecord($this->uid);
  78. // $record=array([
  79. // 'bid'=>1,
  80. // 'time'=>3,
  81. // 'cid'=>4,
  82. // ],[
  83. // 'bid'=>2,
  84. // 'time'=>3,
  85. // 'cid'=>4,
  86. // ]);
  87. foreach ($res as &$v){
  88. $bid=$v['bid'];
  89. $last_cid = $v['last_cid'];
  90. $last_chapter = ChapterService::getChapterNameById($last_cid,$bid);
  91. $v['last_chapter']="最后章节:第{$last_cid}章 {$last_chapter['name']}";
  92. foreach ($record as $val){
  93. if($v['bid'] == $val['bid']){
  94. $v['updated_at'] = $val['time'];
  95. $recent_reading_cid = $val['cid'];
  96. // dd($recent_reading_cid);
  97. $recent_reading_chapter = ChapterService::getChapterNameById($recent_reading_cid,$bid);
  98. $v['recent_reading_chapter'] = "最近阅读:第{$recent_reading_cid}章 {$recent_reading_chapter['name']}";
  99. break;
  100. }
  101. }
  102. }
  103. return response()->collection(new UserShelfBooksTransformer(),$res);
  104. }
  105. /**
  106. * @apiVersion 1.0.0
  107. * @apiDescription 添加书架
  108. * @api {post} userShelfBooks 添加书架
  109. * @apiParam {String} [token] token
  110. * @apiHeader {String} [Authorization] token 两个token任选其一
  111. * @apiGroup UserShelfBooks
  112. * @apiName addShelf
  113. * @apiParam {int} bid bid
  114. * @apiSuccess {int} code 状态码
  115. * @apiSuccess {String} msg 信息
  116. * @apiSuccess {object} data 结果集
  117. * @apiSuccessExample {json} Success-Response:
  118. * HTTP/1.1 200 OK
  119. * {
  120. * code: 0,
  121. * msg: "",
  122. * data: {}
  123. */
  124. public function addShelf(Request $request){
  125. $param = $request->except('_url');
  126. if(checkParam($param,['bid'])){
  127. return response()->error('LACK_PARAM');
  128. }
  129. $param['uid'] = $this->uid;
  130. $param['bid'] = Hashids::decode($param['bid'])[0];
  131. $param['distribution_channel_id'] = $this->distribution_channel_id;
  132. $res = null;
  133. try{
  134. $res = UserShelfBooksService::create($param);
  135. }catch (\Exception $e){
  136. return response()->error('QAPP_PARAM_ERROR');
  137. }
  138. if($res){
  139. return response()->success($res);
  140. }
  141. return response()->error('QAPP_SYS_ERROR');
  142. }
  143. /**
  144. * @apiVersion 1.0.0
  145. * @apiDescription 删除书架
  146. * @api {get} userShelfBooks/delete 删除书架
  147. * @apiParam {String} [token] token
  148. * @apiHeader {String} [Authorization] token 两个token任选其一
  149. * @apiGroup UserShelfBooks
  150. * @apiName delShelf
  151. * @apiParam {int} bid bid
  152. * @apiSuccess {int} code 状态码
  153. * @apiSuccess {String} msg 信息
  154. * @apiSuccess {object} data 结果集
  155. * @apiSuccessExample {json} Success-Response:
  156. * HTTP/1.1 200 OK
  157. * {
  158. * code: 0,
  159. * msg: "",
  160. * data: {}
  161. */
  162. public function delShelf(Request $request){
  163. $bid = $request->input('bid');
  164. if(empty($bid)) return response()->error('LACK_PARAM');
  165. $param['uid'] = $this->uid;
  166. $param['bid'] = Hashids::decode($bid)[0];
  167. $res = UserShelfBooksService::del($this->uid,$param['bid']);
  168. if($res){
  169. return response()->success();
  170. }
  171. return response()->error('QAPP_SYS_ERROR');
  172. }
  173. /**
  174. * @apiVersion 1.0.0
  175. * @apiDescription 是否在书架上
  176. * @api {get} userShelfBooks/isonshelf 是否在书架上
  177. * @apiParam {String} [token] token
  178. * @apiHeader {String} [Authorization] token 两个token任选其一
  179. * @apiGroup UserShelfBooks
  180. * @apiName isOnshelf
  181. * @apiParam {int} bid bid
  182. * @apiSuccess {int} code 状态码
  183. * @apiSuccess {String} msg 信息
  184. * @apiSuccess {object} data 结果集
  185. * @apiSuccess {Int} data.is_on 是否在书架上(0|1)
  186. * @apiSuccessExample {json} Success-Response:
  187. * HTTP/1.1 200 OK
  188. * {
  189. * code: 0,
  190. * msg: "",
  191. * data: {
  192. * is_on:0
  193. * }
  194. */
  195. public function isOnshelf(Request $request){
  196. $bid = $request->input('bid');
  197. if(!$bid) return response()->error('LACK_PARAM');
  198. $bid = Hashids::decode($bid)[0];
  199. $res = UserShelfBooksService::getUserShelfBooksListByUidAndBid($this->uid,$bid);
  200. if($res){
  201. $data['is_on'] = 1;
  202. }else{
  203. $data['is_on'] = 0;
  204. }
  205. return response()->success($data);
  206. }
  207. }