BookController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. namespace App\Http\Controllers\QuickApp\Book;
  3. use App\Modules\RecommendBook\Services\RecommendService;
  4. use App\Modules\Book\Services\RecoBannerService;
  5. use Illuminate\Http\Request;
  6. use App\Http\Controllers\QuickApp\BaseController;
  7. use App\Http\Controllers\QuickApp\Book\Transformers\BookTransformer;
  8. use App\Modules\Book\Services\BookConfigService;
  9. use App\Modules\Book\Services\BookService;
  10. use App\Modules\Book\Services\BookUrgeUpdateService;
  11. use App\Modules\Book\Services\UserShelfBooksService;
  12. use App\Modules\Book\Services\ChapterService;
  13. use App\Modules\Subscribe\Services\BookOrderService;
  14. use App\Modules\Subscribe\Services\ChapterOrderService;
  15. use App\Modules\Subscribe\Services\YearOrderService;
  16. use App\Modules\User\Services\ReadRecordService;
  17. class BookController extends BaseController
  18. {
  19. public function index(Request $request, $bid)
  20. {
  21. $bid = BookService::decodeBidStatic($bid);
  22. $book_info = BookConfigService::getBookById($bid);
  23. if (!$book_info) {
  24. return response()->error('QAPP_SYS_ERROR');
  25. }
  26. if (!in_array($book_info->is_on_shelf, [2])) {
  27. return response()->error('QAPP_OFF_SHELF');
  28. }
  29. $is_on_shelf = UserShelfBooksService::getUserShelfBooksListByUidAndBid($this->uid, $bid);
  30. $book_info['is_on_user_shelf'] = 0;
  31. if ($is_on_shelf) {
  32. $book_info['is_on_user_shelf'] = 1;
  33. }
  34. $last_chapter = ChapterService::getChapterNameById($book_info['last_cid'], $bid);
  35. $book_info['last_chapter_is_vip'] = $last_chapter['is_vip'];
  36. $book_info['is_need_charge'] = $this->isNeedCharge($bid, $last_chapter, $book_info);
  37. $record = ReadRecordService::getBookReadRecordStatic($this->uid, $bid);
  38. if ($record) {
  39. $book_info['record_chapter_id'] = $record['record_chapter_id'];
  40. $book_info['record_chapter_name'] = $record['record_chapter_name'];
  41. }
  42. return response()->item(new BookTransformer(), $book_info);
  43. }
  44. /**
  45. * 获取订购记录
  46. * @param $book_info
  47. * @param $chapter_id
  48. * @return bool
  49. */
  50. protected function getOrderRecord($bid, $chapter_id)
  51. {
  52. //包年记录
  53. $uid = $this->uid;
  54. $res = YearOrderService::getRecord($uid);
  55. if ($res) return true;
  56. $res = null;
  57. //单本订购记录
  58. $res = BookOrderService::getRecordByuidBid($uid, $bid);
  59. if ($res) return true;
  60. $res = null;
  61. //章节订购记录
  62. $chapterOrder = new ChapterOrderService();
  63. if ($chapterOrder->checkIsOrdered($uid, $bid, $chapter_id)) return true;
  64. return false;
  65. }
  66. /**
  67. * 判断是否需要充值
  68. */
  69. private function isBookNeedCharge(int $bid, float $price)
  70. {
  71. $book_order = $this->getOrderRecord($bid, 0);
  72. if ($book_order) {
  73. return false;
  74. } else {
  75. $user_info = $this->user_info;
  76. return $user_info['balance'] < $price;
  77. }
  78. }
  79. /**
  80. * 判断章节是否需要充值
  81. */
  82. private function isChapterNeedCharge(int $bid, int $cid, float $price)
  83. {
  84. $book_order = $this->getOrderRecord($bid, $cid);
  85. if ($book_order) {
  86. return false;
  87. } else {
  88. $user_info = $this->user_info;
  89. return $user_info['balance'] < $price;
  90. }
  91. }
  92. /**
  93. * 判断是否需要充值
  94. */
  95. private function isNeedCharge(int $bid, $last_chapter, $book_info)
  96. {
  97. switch ($book_info->charge_type) {
  98. case 'BOOK':
  99. $price = $this->getPrice($book_info);
  100. return $this->isBookNeedCharge($bid, $price);
  101. default:
  102. $price = $last_chapter->is_vip ? $this->getPrice($book_info, $last_chapter->size) : 0;
  103. return $last_chapter->is_vip ? $this->isChapterNeedCharge($bid, $last_chapter->id, $price) : false;
  104. }
  105. }
  106. /**
  107. * 计算价格
  108. * @param $book_info
  109. * @param $chapter_size
  110. * @return float
  111. */
  112. protected function getPrice($book_info, $chapter_size = 0)
  113. {
  114. if ($book_info->charge_type == 'BOOK')
  115. return $book_info->price * 100;
  116. return ceil($chapter_size / 100);
  117. }
  118. /**
  119. * 首页
  120. */
  121. public function getBookLists(Request $request, $sex)
  122. {
  123. if ($sex == 'male') {
  124. $channel = 1;
  125. $reco_banner_type = ['MALE', 'PUBLIC'];
  126. } else {
  127. $reco_banner_type = ['FEMALE', 'PUBLIC'];
  128. $channel = 2;
  129. }
  130. $books = (new RecoBannerService)->getByType($reco_banner_type, 2);
  131. $books->transform(function ($item) {
  132. $result = $this->getBidCidFromUrl($item->redirect_url);
  133. $item->bid = $result['bid'];
  134. $item->cid = $result['cid'];
  135. if ($result['cid']) {
  136. $item->redirect_url = "views/Reader";
  137. } else {
  138. $item->redirect_url = "views/Detail";
  139. }
  140. return $item;
  141. });
  142. $package = $request->header('x-package', '');
  143. $checkOpen = env('CHECK_OPEN', false);
  144. if ($checkOpen && $package === 'com.juyu.kuaiying.rmyq') {
  145. $hotBids = config('home.hot.' . $sex);
  146. $liveBids = config('home.live.' . $sex);
  147. $recomBids = config('home.recom.' . $sex);
  148. $newBids = config('home.new_recom.' . $sex);
  149. } else {
  150. $hotBids = RecommendService::getRecommendIdsStatic($channel, 'hot');
  151. $liveBids = RecommendService::getRecommendIdsStatic($channel, 'live');
  152. $recomBids = RecommendService::getRecommendIdsStatic($channel, 'recom');
  153. $newBids = RecommendService::getRecommendIdsStatic($channel, 'new_recom');
  154. }
  155. $result = [
  156. ['type' => 'reco_banner', 'lable' => '首页banner', 'books' => $books],
  157. ['type' => 'hot', 'lable' => '热门推荐', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($hotBids))],
  158. ['type' => 'zhibo', 'lable' => '神书直播', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($liveBids))],
  159. ['type' => 'recom', 'lable' => '编辑推荐', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($recomBids))],
  160. ['type' => 'new_recom', 'lable' => '新书推荐', 'books' => collectionTransform(new BookTransformer, BookConfigService::getBooksByIds($newBids))],
  161. ];
  162. return response()->success($result);
  163. }
  164. private function getBidCidFromUrl(string $url)
  165. {
  166. if (preg_match('/\?bid=(\w+)\S+cid=(\w+)/', $url, $matches) || preg_match('/\?id=(\w+)/', $url, $matches)) {
  167. return [
  168. 'bid' => $matches[1],
  169. 'cid' => isset($matches[2]) ? $matches[2] : 0,
  170. ];
  171. } else {
  172. return [
  173. 'bid' => '',
  174. 'cid' => 0,
  175. ];
  176. }
  177. }
  178. public function library(Request $request)
  179. {
  180. $where = [];
  181. $order = [];
  182. $where['is_on_shelf'] = [2];
  183. $category_id = $request->input('category_id');
  184. if ($category_id) {
  185. if ($category_id == 1) {
  186. $where['channel_name'] = '男频';
  187. } elseif ($category_id == 2) {
  188. $where['channel_name'] = '女频';
  189. } else {
  190. $where['category_id'] = $category_id;
  191. }
  192. }
  193. $key = $request->input('key');
  194. $where['key'] = $key;
  195. $order_field = $request->input('order_field');
  196. $order_seq = $request->input('order_seq');
  197. if ($order_field != '' && in_array($order_field, ['recommend_index', 'click_count', 'update', 'size', 'create'])) {
  198. if ($order_field == 'update') {
  199. $order = ['book_configs.updated_at', 'desc'];
  200. } elseif ($order_field == 'create') {
  201. $order = ['book_configs.created_at', 'desc'];
  202. } else {
  203. $order = [$order_field, 'desc'];
  204. }
  205. if ($order_seq == 'asc') {
  206. $order = [$order_field, 'asc'];
  207. }
  208. if ($order_seq == 'desc') {
  209. $order = [$order_field, 'desc'];
  210. }
  211. }
  212. $status = $request->input('status');
  213. if ($status != '') {
  214. $where['status'] = $status;
  215. }
  216. $page_size = $request->input('page_size', 15);
  217. $books = BookConfigService::getBooks($where, $order, $page_size);
  218. return response()->pagination(new BookTransformer, $books);
  219. }
  220. public function similarRecom(Request $request)
  221. {
  222. $category_id = $request->input('category_id');
  223. $bid = $request->input('bid');
  224. if (empty($bid) || empty($category_id)) {
  225. return response()->error('PARAM_ERROR');
  226. }
  227. $bid = BookService::decodeBidStatic($bid);
  228. $where = ['category_id' => $category_id, 'is_on_shelf' => [2]];
  229. $books = BookConfigService::getBooks($where, [], 4);
  230. $data = [];
  231. foreach ($books as $v) {
  232. if ($v->bid != $bid && count($data) < 3) {
  233. $data[] = $v;
  234. }
  235. }
  236. return response()->collection(new BookTransformer(), $data);
  237. }
  238. public function readOverRecommend(Request $request)
  239. {
  240. $bid = $request->input('bid');
  241. if (empty($bid)) {
  242. return response()->error('PARAM_ERROR');
  243. }
  244. $bid = BookService::decodeBidStatic($bid);
  245. $book_info = BookConfigService::getBookById($bid);
  246. $res = BookConfigService::getRecommendBooks($bid, $book_info->channel_name);
  247. $urge_status = 0;
  248. if ($book_info->status == 0 && !BookUrgeUpdateService::isHadUrged($this->uid, $bid)) {
  249. $urge_status = 1;
  250. }
  251. $recommend_result = collectionTransform(new BookTransformer(), $res);
  252. $book_status = [
  253. 'status' => $book_info->status,
  254. 'urge_status' => $urge_status
  255. ];
  256. $data = [
  257. 'recommend_result' => $recommend_result,
  258. 'book_status' => $book_status
  259. ];
  260. return response()->success($data);
  261. }
  262. public function rank(Request $request)
  263. {
  264. $sex = $request->input('sex');
  265. if ($sex == 1) {
  266. $books = collectionTransform(new BookTransformer, BookConfigService::getBooksByIds([11601, 11529, 3365, 10377, 11457, 8102, 6464, 7287, 2563, 10419]));
  267. } elseif ($sex == 2) {
  268. $books = collectionTransform(new BookTransformer, BookConfigService::getBooksByIds([10823, 10479, 10467, 10139, 9990, 9973, 9479, 9423, 1148, 8693, 8497, 8148, 8129, 7857, 7854, 7629, 7362, 5748, 5362, 4811, 4470, 4135, 3759, 3696, 3418, 3401, 3369, 2698, 1634, 1479]));
  269. } else {
  270. return response()->error('PARAM_ERROR');
  271. }
  272. return response()->success($books);
  273. }
  274. /**
  275. * 推荐书
  276. */
  277. public function recommen()
  278. {
  279. $reco_banner_type = ['FEMALE', 'PUBLIC'];
  280. $books = (new RecoBannerService)->getByType($reco_banner_type, 2);
  281. $books->transform(function ($item) {
  282. $result = $this->getBidCidFromUrl($item->redirect_url);
  283. $item->bid = $result['bid'];
  284. $item->cid = $result['cid'];
  285. if ($result['cid']) {
  286. $item->redirect_url = "views/Reader";
  287. } else {
  288. $item->redirect_url = "views/Detail";
  289. }
  290. return $item;
  291. });
  292. return response()->success($books);
  293. }
  294. }