UserShelfBooksController.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. * @apiSuccess {String} data.recent_cid 最近阅读章节id
  38. * @apiSuccessExample {json} Success-Response:
  39. * HTTP/1.1 200 OK
  40. * {
  41. * code: 0,
  42. * msg: "",
  43. * data: {
  44. * [
  45. * {
  46. * id: 6,
  47. * uid: 4,
  48. * distribution_channel_id: 1,
  49. * bid: 1,
  50. * book_name: "京华烟云",
  51. * cover: "https://leyue-bucket.oss-cn-hangzhou.aliyuncs.com/ycsd_cover/covermiddle/0/1.jpg",
  52. * updated_at: 1511783068,
  53. * last_cid: 4,
  54. * recent_cid:5,
  55. * last_chapter:"最新 第122章 大事不妙",
  56. * recent_reading_chapter:"最近阅读:第5章 又来?"
  57. *
  58. * },
  59. * {
  60. * id: 7,
  61. * uid: 4,
  62. * distribution_channel_id: 1,
  63. * bid: 1,
  64. * book_name: "我来好好爱你",
  65. * cover: "https://leyue-bucket.oss-cn-hangzhou.aliyuncs.com/ycsd_cover/covermiddle/0/11.jpg",
  66. * updated_at: 1511783068,
  67. * last_cid: 4,
  68. * recent_cid:5,
  69. * last_chapter:"最新 第2556章 又来了个张老三",
  70. * recent_reading_chapter:"最近阅读:第2551章 又来了个猪八戒"
  71. * }
  72. * ]
  73. * }
  74. */
  75. public function index(Request $request){
  76. $res = UserShelfBooksService::getUserShelfBooksListByUid($this->uid);
  77. if($res->isEmpty()){
  78. return response()->success();
  79. }
  80. $record = ReadRecordService::getReadRecord($this->uid);
  81. // $record=array([
  82. // 'bid'=>1,
  83. // 'time'=>3,
  84. // 'cid'=>1,
  85. // ],[
  86. // 'bid'=>2,
  87. // 'time'=>3,
  88. // 'cid'=>4,
  89. // ]);
  90. foreach ($res as &$v){
  91. $bid=$v['bid'];
  92. $last_cid = $v['last_cid'];
  93. $last_chapter = ChapterService::getChapterNameByID($last_cid,$bid);
  94. $v['last_chapter']="最后章节:{$last_chapter['name']}";
  95. foreach ($record as $val){
  96. if($v['bid'] == $val['bid']){
  97. $v['updated_at'] = $val['time'];
  98. $recent_reading_cid = $val['cid'];
  99. $v['recent_cid'] = $recent_reading_cid;
  100. $recent_reading_chapter = ChapterService::getChapterNameById($recent_reading_cid,$bid);
  101. $v['recent_reading_chapter'] = "最近阅读:{$recent_reading_chapter['name']}";
  102. break;
  103. }
  104. }
  105. }
  106. return response()->collection(new UserShelfBooksTransformer(),$res);
  107. }
  108. /**
  109. * @apiVersion 1.0.0
  110. * @apiDescription 添加书架
  111. * @api {post} userShelfBooks 添加书架
  112. * @apiParam {String} [token] token
  113. * @apiHeader {String} [Authorization] token 两个token任选其一
  114. * @apiGroup UserShelfBooks
  115. * @apiName addShelf
  116. * @apiParam {int} bid bid
  117. * @apiSuccess {int} code 状态码
  118. * @apiSuccess {String} msg 信息
  119. * @apiSuccess {object} data 结果集
  120. * @apiSuccessExample {json} Success-Response:
  121. * HTTP/1.1 200 OK
  122. * {
  123. * code: 0,
  124. * msg: "",
  125. * data: {}
  126. */
  127. public function addShelf(Request $request){
  128. $param = $request->except('_url');
  129. if(checkParam($param,['bid'])){
  130. return response()->error('LACK_PARAM');
  131. }
  132. $param['uid'] = $this->uid;
  133. $param['bid'] = Hashids::decode($param['bid'])[0];
  134. $param['distribution_channel_id'] = $this->distribution_channel_id;
  135. $res = null;
  136. try{
  137. $res = UserShelfBooksService::create($param);
  138. }catch (\Exception $e){
  139. return response()->error('QAPP_PARAM_ERROR');
  140. }
  141. if($res){
  142. return response()->success($res);
  143. }
  144. return response()->error('QAPP_SYS_ERROR');
  145. }
  146. /**
  147. * @apiVersion 1.0.0
  148. * @apiDescription 删除书架
  149. * @api {get} userShelfBooks/delete 删除书架
  150. * @apiParam {String} [token] token
  151. * @apiHeader {String} [Authorization] token 两个token任选其一
  152. * @apiGroup UserShelfBooks
  153. * @apiName delShelf
  154. * @apiParam {int} bid bid
  155. * @apiSuccess {int} code 状态码
  156. * @apiSuccess {String} msg 信息
  157. * @apiSuccess {object} data 结果集
  158. * @apiSuccessExample {json} Success-Response:
  159. * HTTP/1.1 200 OK
  160. * {
  161. * code: 0,
  162. * msg: "",
  163. * data: {}
  164. */
  165. public function delShelf(Request $request){
  166. $bid = $request->input('bid');
  167. if(empty($bid)) return response()->error('LACK_PARAM');
  168. $param['uid'] = $this->uid;
  169. $param['bid'] = Hashids::decode($bid)[0];
  170. $res = UserShelfBooksService::del($this->uid,$param['bid']);
  171. if($res){
  172. return response()->success();
  173. }
  174. return response()->error('QAPP_SYS_ERROR');
  175. }
  176. /**
  177. * @apiVersion 1.0.0
  178. * @apiDescription 是否在书架上
  179. * @api {get} userShelfBooks/isonshelf 是否在书架上
  180. * @apiParam {String} [token] token
  181. * @apiHeader {String} [Authorization] token 两个token任选其一
  182. * @apiGroup UserShelfBooks
  183. * @apiName isOnshelf
  184. * @apiParam {int} bid bid
  185. * @apiSuccess {int} code 状态码
  186. * @apiSuccess {String} msg 信息
  187. * @apiSuccess {object} data 结果集
  188. * @apiSuccess {Int} data.is_on 是否在书架上(0|1)
  189. * @apiSuccessExample {json} Success-Response:
  190. * HTTP/1.1 200 OK
  191. * {
  192. * code: 0,
  193. * msg: "",
  194. * data: {
  195. * is_on:0
  196. * }
  197. */
  198. public function isOnshelf(Request $request){
  199. $bid = $request->input('bid');
  200. if(!$bid) return response()->error('LACK_PARAM');
  201. $bid = Hashids::decode($bid)[0];
  202. $res = UserShelfBooksService::getUserShelfBooksListByUidAndBid($this->uid,$bid);
  203. if($res){
  204. $data['is_on'] = 1;
  205. }else{
  206. $data['is_on'] = 0;
  207. }
  208. return response()->success($data);
  209. }
  210. }