BookController.php 12 KB

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