BookController.php 12 KB

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