UserShelfBooksController.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. namespace App\Http\Controllers\WapAlipay\User;
  3. use Illuminate\Http\Request;
  4. use App\Http\Controllers\WapAlipay\BaseController;
  5. use Redis;
  6. use App\Modules\Book\Services\UserShelfBooksService;
  7. use App\Http\Controllers\WapAlipay\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. * @apiGroup UserShelfBooks
  21. * @apiName getChapter
  22. * @apiSuccess {int} code 状态码
  23. * @apiSuccess {String} msg 信息
  24. * @apiSuccess {object} data 结果集
  25. * @apiSuccess {Int} data.id 编号
  26. * @apiSuccess {String} data.distribution_channel_id 分销
  27. * @apiSuccess {Int} data.uid uid
  28. * @apiSuccess {Int} data.bid bid
  29. * @apiSuccess {Int} data.book_name 书名
  30. * @apiSuccess {Int} data.cover 封面
  31. * @apiSuccess {Int} data.last_cid 章节id
  32. * @apiSuccessExample {json} Success-Response:
  33. * HTTP/1.1 200 OK
  34. * {
  35. * code: 0,
  36. * msg: "",
  37. * data: {
  38. * [
  39. * {
  40. * id: 6,
  41. * uid: 4,
  42. * distribution_channel_id: 1,
  43. * bid: 1,
  44. * book_name: "京华烟云",
  45. * cover: "https://leyue-bucket.oss-cn-hangzhou.aliyuncs.com/ycsd_cover/covermiddle/0/1.jpg",
  46. * updated_at: 1511783068,
  47. * last_cid: 4
  48. * },
  49. * {
  50. * id: 7,
  51. * uid: 4,
  52. * distribution_channel_id: 1,
  53. * bid: 1,
  54. * book_name: "我来好好爱你",
  55. * cover: "https://leyue-bucket.oss-cn-hangzhou.aliyuncs.com/ycsd_cover/covermiddle/0/11.jpg",
  56. * updated_at: 1511783068,
  57. * last_cid: 4
  58. * }
  59. * ]
  60. * }
  61. */
  62. public function index(Request $request){
  63. if(!$this->checkUid()){
  64. return response()->error('WAP_NOT_LOGIN');
  65. }
  66. $res = UserShelfBooksService::getUserShelfBooksListByUid($this->uid);
  67. if($res->isEmpty()){
  68. return response()->success();
  69. }
  70. $record = ReadRecordService::getReadRecord($this->uid);
  71. //if(empty($record)) return response()->success($res);
  72. foreach ($res as &$v){
  73. $v['last_cid'] = $v['first_cid'];
  74. foreach ($record as $val){
  75. if($v['bid'] == $val['bid']){
  76. $v['updated_at'] = $val['time'];
  77. $v['last_cid'] = $val['cid'];
  78. break;
  79. }
  80. }
  81. }
  82. /*
  83. usort($res,function($a,$b){
  84. if($a['updated_at'] >= $b['updated_at']) return -1;
  85. return 1;
  86. });*/
  87. return response()->collection(new UserShelfBooksTransformer(),$res);
  88. }
  89. /**
  90. * @apiVersion 1.0.0
  91. * @apiDescription 添加书架
  92. * @api {post} userShelfBooks 添加书架
  93. * @apiGroup UserShelfBooks
  94. * @apiName addShelf
  95. * @apiParam {int} bid bid
  96. * @apiSuccess {int} code 状态码
  97. * @apiSuccess {String} msg 信息
  98. * @apiSuccess {object} data 结果集
  99. * @apiSuccessExample {json} Success-Response:
  100. * HTTP/1.1 200 OK
  101. * {
  102. * code: 0,
  103. * msg: "",
  104. * data: {}
  105. */
  106. public function addShelf(Request $request){
  107. if(!$this->checkUid()){
  108. return response()->error('WAP_NOT_LOGIN');
  109. }
  110. $param = $request->except('_url');
  111. if(checkParam($param,['bid'])){
  112. return response()->error('WAP_LACK_PARAM');
  113. }
  114. $param['uid'] = $this->uid;
  115. $param['bid'] = Hashids::decode($param['bid'])[0];
  116. $param['distribution_channel_id'] = $this->distribution_channel_id;
  117. $res = null;
  118. try{
  119. $res = UserShelfBooksService::create($param);
  120. }catch (\Exception $e){
  121. return response()->error('WAP_HAD_ON_SHELF');
  122. }
  123. if($res){
  124. return response()->success($res);
  125. }
  126. return response()->error('WAP_SYS_ERROR');
  127. }
  128. /**
  129. * @apiVersion 1.0.0
  130. * @apiDescription 删除书架
  131. * @api {get} userShelfBooks/delete 删除书架
  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. if(!$this->checkUid()){
  147. return response()->error('WAP_NOT_LOGIN');
  148. }
  149. $bid = $request->input('bid');
  150. if(empty($bid)) return response()->error('WAP_LACK_PARAM');
  151. $param['uid'] = $this->uid;
  152. $param['bid'] = Hashids::decode($bid)[0];
  153. $res = UserShelfBooksService::del($this->uid,$param['bid']);
  154. if($res){
  155. return response()->success();
  156. }
  157. return response()->error('WAP_SYS_ERROR');
  158. }
  159. /**
  160. * @apiVersion 1.0.0
  161. * @apiDescription 是否在书架上
  162. * @api {get} userShelfBooks/isonshelf 是否在书架上
  163. * @apiGroup UserShelfBooks
  164. * @apiName isOnshelf
  165. * @apiParam {int} bid bid
  166. * @apiSuccess {int} code 状态码
  167. * @apiSuccess {String} msg 信息
  168. * @apiSuccess {object} data 结果集
  169. * @apiSuccess {Int} data.is_on 是否在书架上(0|1)
  170. * @apiSuccessExample {json} Success-Response:
  171. * HTTP/1.1 200 OK
  172. * {
  173. * code: 0,
  174. * msg: "",
  175. * data: {
  176. * is_on:0
  177. * }
  178. */
  179. public function isOnshelf(Request $request){
  180. if(!$this->checkUid()){
  181. return response()->error('NOT_LOGIN');
  182. }
  183. $bid = $request->input('bid');
  184. if(!$bid) return response()->error('LACK_PARAM');
  185. $bid = Hashids::decode($bid)[0];
  186. //$param['uid'] = $this->uid;
  187. //$res = $this->apiClient->get($this->joinUrl('Read/isonshelf'),$param);
  188. $res = UserShelfBooksService::getUserShelfBooksListByUidAndBid($this->uid,$bid);
  189. if($res){
  190. $data['is_on'] = 1;
  191. }else{
  192. $data['is_on'] = 0;
  193. }
  194. return response()->success($data);
  195. }
  196. }