UserShelfBooksController.php 6.6 KB

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