UserShelfBooksController.php 6.9 KB

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