UserShelfBooksController.php 7.3 KB

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