UserShelfBooksController.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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'=>1,
  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_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. $recent_reading_chapter = ChapterService::getChapterNameById($recent_reading_cid,$bid);
  97. $v['recent_reading_chapter'] = "最近阅读:{$recent_reading_chapter['name']}";
  98. break;
  99. }
  100. }
  101. }
  102. return response()->collection(new UserShelfBooksTransformer(),$res);
  103. }
  104. /**
  105. * @apiVersion 1.0.0
  106. * @apiDescription 添加书架
  107. * @api {post} userShelfBooks 添加书架
  108. * @apiParam {String} [token] token
  109. * @apiHeader {String} [Authorization] token 两个token任选其一
  110. * @apiGroup UserShelfBooks
  111. * @apiName addShelf
  112. * @apiParam {int} bid bid
  113. * @apiSuccess {int} code 状态码
  114. * @apiSuccess {String} msg 信息
  115. * @apiSuccess {object} data 结果集
  116. * @apiSuccessExample {json} Success-Response:
  117. * HTTP/1.1 200 OK
  118. * {
  119. * code: 0,
  120. * msg: "",
  121. * data: {}
  122. */
  123. public function addShelf(Request $request){
  124. $param = $request->except('_url');
  125. if(checkParam($param,['bid'])){
  126. return response()->error('LACK_PARAM');
  127. }
  128. $param['uid'] = $this->uid;
  129. $param['bid'] = Hashids::decode($param['bid'])[0];
  130. $param['distribution_channel_id'] = $this->distribution_channel_id;
  131. $res = null;
  132. try{
  133. $res = UserShelfBooksService::create($param);
  134. }catch (\Exception $e){
  135. return response()->error('QAPP_PARAM_ERROR');
  136. }
  137. if($res){
  138. return response()->success($res);
  139. }
  140. return response()->error('QAPP_SYS_ERROR');
  141. }
  142. /**
  143. * @apiVersion 1.0.0
  144. * @apiDescription 删除书架
  145. * @api {get} userShelfBooks/delete 删除书架
  146. * @apiParam {String} [token] token
  147. * @apiHeader {String} [Authorization] token 两个token任选其一
  148. * @apiGroup UserShelfBooks
  149. * @apiName delShelf
  150. * @apiParam {int} bid bid
  151. * @apiSuccess {int} code 状态码
  152. * @apiSuccess {String} msg 信息
  153. * @apiSuccess {object} data 结果集
  154. * @apiSuccessExample {json} Success-Response:
  155. * HTTP/1.1 200 OK
  156. * {
  157. * code: 0,
  158. * msg: "",
  159. * data: {}
  160. */
  161. public function delShelf(Request $request){
  162. $bid = $request->input('bid');
  163. if(empty($bid)) return response()->error('LACK_PARAM');
  164. $param['uid'] = $this->uid;
  165. $param['bid'] = Hashids::decode($bid)[0];
  166. $res = UserShelfBooksService::del($this->uid,$param['bid']);
  167. if($res){
  168. return response()->success();
  169. }
  170. return response()->error('QAPP_SYS_ERROR');
  171. }
  172. /**
  173. * @apiVersion 1.0.0
  174. * @apiDescription 是否在书架上
  175. * @api {get} userShelfBooks/isonshelf 是否在书架上
  176. * @apiParam {String} [token] token
  177. * @apiHeader {String} [Authorization] token 两个token任选其一
  178. * @apiGroup UserShelfBooks
  179. * @apiName isOnshelf
  180. * @apiParam {int} bid bid
  181. * @apiSuccess {int} code 状态码
  182. * @apiSuccess {String} msg 信息
  183. * @apiSuccess {object} data 结果集
  184. * @apiSuccess {Int} data.is_on 是否在书架上(0|1)
  185. * @apiSuccessExample {json} Success-Response:
  186. * HTTP/1.1 200 OK
  187. * {
  188. * code: 0,
  189. * msg: "",
  190. * data: {
  191. * is_on:0
  192. * }
  193. */
  194. public function isOnshelf(Request $request){
  195. $bid = $request->input('bid');
  196. if(!$bid) return response()->error('LACK_PARAM');
  197. $bid = Hashids::decode($bid)[0];
  198. $res = UserShelfBooksService::getUserShelfBooksListByUidAndBid($this->uid,$bid);
  199. if($res){
  200. $data['is_on'] = 1;
  201. }else{
  202. $data['is_on'] = 0;
  203. }
  204. return response()->success($data);
  205. }
  206. }